Problem Statement: -
Education is very important issue regarding development of a country. The main objective of educational institutions is to provide high quality education to its students. One way to accomplish this is by predicting student’s academic performance and thereby taking early steps to improve student’s performance and teaching quality.
This model aims to predict student’s marks using linear regression. The idea behind this analysis is to predict the marks of students
The goal of this challenge is to build a machine learning model that predicts the marks of other students. It is a good regression problem that will acurately predict the marks of the students.
Dataset: -
The data consists of Marks of students including their study time & number of courses. The dataset is downloaded from UCI Machine Learning Repository.
Attribute Information:
Number of Instances: 100
Number of Attributes: 3 including the target variable.
number_courses (Number of Courses Opted by the student)
time_study (Average Time Studied per day by the student)
Marks (Marks Obtained by the student)
The project is simple yet challenging as it is has very limited features & samples.
Step 1: Import all the required libraries
Pandas : In computer programming, pandas is a software library written for the Python programming language for data manipulation and analysis and storing in a proper way. In particular, it offers data structures and operations for manipulating numerical tables and time series
Sklearn : Scikit-learn (formerly scikits.learn) is a free software machine learning library for the Python programming language. It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerical and scientific libraries NumPy and SciPy. The library is built upon the SciPy (Scientific Python) that must be installed before you can use scikit-learn.
Pickle : Python pickle module is used for serializing and de-serializing a Python object structure. Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.
Seaborn : Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.
Matplotlib : Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK.
#Loading libraries
import pandas as pd
import seaborn as sns
import pickle
import numpy as np
import matplotlib.pyplot as plt
import plotly.express as px
from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression, Ridge, RidgeCV, Lasso, LassoCV
from sklearn.model_selection import KFold, cross_val_score, train_test_split
import warnings
warnings.filterwarnings('ignore')
Step 2 : Read dataset and basic details of dataset
Goal:- In this step we are going to read the dataset, view the dataset and analysis the basic details like total number of rows and columns, what are the column data types and see to need to create new column or not.
In this stage we are going to read our problem dataset and have a look on it.
#loading the dataset
try:
df = pd.read_csv('C:/Users/Sakshi Rohida/Desktop/deepak sir ML projects/Student_Marks_Prediction/data/Student_Marks.csv') #Path for the file
print('Data read done successfully...')
except (FileNotFoundError, IOError):
print("Wrong file or file path")
Data read done successfully...
# To view the content inside the dataset we can use the head() method returns a specified number of rows, string from the top.
# The head() method returns the first 5 rows if a number is not specified.
df.head()
Data Preprocessing
Now before moving forward, let’s have a look at whether this dataset contains any null values or not.
print(df.isnull().sum())
number_courses 0
time_study 0
Marks 0
dtype: int64
As we observed, the data is ready to use because there are no null values in the data. There is a column in the data containing information about the number of courses students have chosen. Let’s look at the number of values of all values of this column:
df["number_courses"].value_counts()
3 22
4 21
6 16
8 16
7 15
5 10
Name: number_courses, dtype: int64
The df.value_counts() method counts the number of types of values a particular column contains.
df.shape
(100, 3)
The df.shape method shows the shape of the dataset.
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 number_courses 100 non-null int64
1 time_study 100 non-null float64
2 Marks 100 non-null float64
dtypes: float64(2), int64(1)
memory usage: 2.5 KB
The df.info() method prints information about a DataFrame including the index dtype and columns, non-null values and memory usage.
df.iloc[1]
number_courses 4.000
time_study 0.096
Marks 7.734
Name: 1, dtype: float64
df.iloc[ ] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array. The iloc property gets, or sets, the value(s) of the specified indexes.
Data Type Check for every column.
objects_cols = ['object']
objects_lst = list(df.select_dtypes(include=objects_cols).columns)
print("Total number of cateogrical columns are ", len(objects_lst))
print("There names are as follows: ", objects_lst)
Total number of cateogrical columns are 0
There names are as follows: []
int64_cols = ['int64']
int64_lst = list(df.select_dtypes(include=int64_cols).columns)
print("Total number of numerical columns are ", len(int64_lst))
print("There names are as follows: ", int64_lst)
Total number of numerical columns are 1
There names are as follows: ['number_courses']
float64_cols = ['float64']
float64_lst = list(df.select_dtypes(include=float64_cols).columns)
print("Total number of float64 columns are ", len(float64_lst))
print("There name are as follow: ", float64_lst)
Total number of float64 columns are 2
There name are as follow: ['time_study', 'Marks']
#count the total number of rows and columns.
print ('The new dataset has {0} rows and {1} columns'.format(df.shape[0],df.shape[1]))
The new dataset has 100 rows and 3 columns
Step 3: Descriptive Analysis
Goal/Purpose: Finding the data distribution of the features. Visualization, it helps to understand data and also to explain the data to another person.
Things we are going to do in this step:
Mean
Median
Mode
Standard Deviation
Variance
Null Values
NaN Values
Min value
Max value
Count Value
Quatilers
Correlation
Skewness
df.describe()
The df.describe() method returns description of the data in the DataFrame. If the DataFrame contains numerical data, the description contains these information for each column: count — The number of not-empty values. mean — The average (mean) value.
Measure the variability of data of the dataset
Variability describes how far apart data points lie from each other and from the center of a distribution.
1. Standard Deviation
The standard deviation is the average amount of variability in your dataset.
It tells you, on average, how far each data point lies from the mean. The larger the standard deviation, the more variable the data set is and if zero variance then there is no variability in the dataset that means there no use of that dataset.
df.std()
number_courses 1.799523
time_study 2.372914
Marks 14.326199
dtype: float64
We can also understand the standard deviation using the below function.
def std_cal(df,float64_lst):
cols = ['normal_value', 'zero_value']
zero_value = 0
normal_value = 0
for value in float64_lst:
rs = round(df[value].std(),6)
if rs > 0:
normal_value = normal_value + 1
elif rs == 0:
zero_value = zero_value + 1
std_total_df = pd.DataFrame([[normal_value, zero_value]], columns=cols)
return std_total_df
std_cal(df, float64_lst)
int64_cols = ['int64']
int64_lst = list(df.select_dtypes(include=int64_cols).columns)
std_cal(df,int64_lst)
2. Variance
The variance is the average of squared deviations from the mean. A deviation from the mean is how far a score lies from the mean.
Variance is the square of the standard deviation. This means that the units of variance are much larger than those of a typical value of a data set.
df.var()
number_courses 3.238283
time_study 5.630722
Marks 205.239965
dtype: float64
We can also understand the Variance using the below function.
zero_cols = []
def var_cal(df,float64_lst):
cols = ['normal_value', 'zero_value']
zero_value = 0
normal_value = 0
for value in float64_lst:
rs = round(df[value].var(),6)
if rs > 0:
normal_value = normal_value + 1
elif rs == 0:
zero_value = zero_value + 1
zero_cols.append(value)
var_total_df = pd.DataFrame([[normal_value, zero_value]], columns=cols)
return var_total_df
var_cal(df, float64_lst)
var_cal(df, int64_lst)
Measure central tendency
A measure of central tendency is a single value that attempts to describe a set of data by identifying the central position within that set of data. As such, measures of central tendency are sometimes called measures of central location. They are also classed as summary statistics.
1. Mean
The mean is the arithmetic average, and it is probably the measure of central tendency that you are most familiar.
df.mean()
number_courses 5.29000
time_study 4.07714
Marks 24.41769
dtype: float64
We can also understand the mean using the below function.
def mean_cal(df,int64_lst):
cols = ['normal_value', 'zero_value']
zero_value = 0
normal_value = 0
for value in int64_lst:
rs = round(df[value].mean(),6)
if rs > 0:
normal_value = normal_value + 1
elif rs == 0:
zero_value = zero_value + 1
mean_total_df = pd.DataFrame([[normal_value, zero_value]], columns=cols)
return mean_total_df
mean_cal(df, int64_lst)
mean_cal(df,float64_lst)
2.Median
The median is the middle value. It is the value that splits the dataset in half.
df.median()
number_courses 5.0000
time_study 4.0220
Marks 20.0595
dtype: float64
We can also understand the median using the below function.
def median_cal(df,int64_lst):
cols = ['normal_value', 'zero_value']
zero_value = 0
normal_value = 0
for value in float64_lst:
rs = round(df[value].mean(),6)
if rs > 0:
normal_value = normal_value + 1
elif rs == 0:
zero_value = zero_value + 1
median_total_df = pd.DataFrame([[normal_value, zero_value]], columns=cols)
return median_total_df
median_cal(df, float64_lst)
3. Mode
The mode is the value that occurs the most frequently in your data set. On a bar chart, the mode is the highest bar. If the data have multiple values that are tied for occurring the most frequently, you have a multimodal distribution. If no value repeats, the data do not have a mode.
df.mode()
def mode_cal(df,int64_lst):
cols = ['normal_value', 'zero_value', 'string_value']
zero_value = 0
normal_value = 0
string_value = 0
for value in float64_lst:
rs = df[value].mode()[0]
if isinstance(rs, str):
string_value = string_value + 1
else:
if rs > 0:
normal_value = normal_value + 1
elif rs == 0:
zero_value = zero_value + 1
mode_total_df = pd.DataFrame([[normal_value, zero_value, string_value]], columns=cols)
return mode_total_df
mode_cal(df, list(df.columns))
Null and Nan values
- Null Values
df.isnull().sum()
number_courses 0
time_study 0
Marks 0
dtype: int64
As we notice that there are no null values in our dataset.
- Nan Values
df.isna().sum()
number_courses 0
time_study 0
Marks 0
dtype: int64
As we notice that there are no nan values in our dataset.
Another way to remove null and nan values is to use the method “df.dropna(inplace=True)”.
Count of unique occurences of every value in all categorical value
for value in objects_lst:
print(f"{value:{10}} {df[value].value_counts()}")
Categorical data are variables that contain label values rather than numeric values.The number of possible values is often limited to a fixed set.
We will use Label Encoder to label the categorical data. Label Encoder is the part of SciKit Learn library in Python and used to convert categorical data, or text data, into numbers, which our predictive models can better understand.
Skewness
Skewness is a measure of the asymmetry of a distribution. A distribution is asymmetrical when its left and right side are not mirror images. A distribution can have right (or positive), left (or negative), or zero skewness
Below is the function to calculate skewness.
def right_nor_left(df, int64_lst):
temp_skewness = ['column', 'skewness_value', 'skewness (+ve or -ve)']
temp_skewness_values = []
temp_total = ["positive (+ve) skewed", "normal distrbution" , "negative (-ve) skewed"]
positive = 0
negative = 0
normal = 0
for value in float64_lst:
rs = round(df[value].skew(),4)
if rs > 0:
temp_skewness_values.append([value,rs , "positive (+ve) skewed"])
positive = positive + 1
elif rs == 0:
temp_skewness_values.append([value,rs,"normal distrbution"])
normal = normal + 1
elif rs < 0:
temp_skewness_values.append([value,rs, "negative (-ve) skewed"])
negative = negative + 1
skewness_df = pd.DataFrame(temp_skewness_values, columns=temp_skewness)
skewness_total_df = pd.DataFrame([[positive, normal, negative]], columns=temp_total)
return skewness_df, skewness_total_df
float64_cols = ['float64']
float64_lst_col = list(df.select_dtypes(include=float64_cols).columns)
skew_df,skew_total_df = right_nor_left(df, float64_lst_col)
skew_df
skew_total_df
We notice with the above results that we have following details:
1 column is positive skewed
1 coulmn is negative skewed
Step 4: Data Exploration
Goal/Purpose:
Graphs we are going to develop in this step
Histogram of all columns to check the distrubution of the columns
Distplot or distribution plot of all columns to check the variation in the data distribution
Heatmap to calculate correlation within feature variables
Boxplot to find out outlier in the feature columns
1. Histogram
A histogram is a bar graph-like representation of data that buckets a range of classes into columns along the horizontal x-axis.The vertical y-axis represents the number count or percentage of occurrences in the data for each column
# Distribution in attributes
%matplotlib inline
import matplotlib.pyplot as plt
df.hist(bins=20, figsize=(10,10))
plt.show()
2. Distplot
A Distplot or distribution plot, depicts the variation in the data distribution. Seaborn Distplot represents the overall distribution of continuous data variables. The Seaborn module along with the Matplotlib module is used to depict the distplot with different variations in it
num = [f for f in df.columns if df.dtypes[f] != 'object']
nd = pd.melt(df, value_vars = num)
n1 = sns.FacetGrid (nd, col='variable', col_wrap=4, sharex=False, sharey = False)
n1 = n1.map(sns.distplot, 'value')
n1
<seaborn.axisgrid.FacetGrid at 0x2d3d8e02850>
Above is the distrution bar graphs to confirm about statistics of the data about the skewness.
Let’s proceed and check the distribution of the target variable.
#+ve skewed
df['Marks'].skew()
0.6582365955082408
The target variable is positively skewed.
3. Heatmap
A heatmap (or heat map) is a graphical representation of data where values are depicted by color.Heatmaps make it easy to visualize complex data and understand it at a glance
Correlation — A positive correlation is a relationship between two variables in which both variables move in the same direction. Therefore, when one variable increases as the other variable increases, or one variable decreases while the other decreases.
Correlation can have a value:
1 is a perfect positive correlation
0 is no correlation (the values don’t seem linked at all)
-1 is a perfect negative correlation
#correlation plot
sns.set(rc = {'figure.figsize':(5,5)})
corr = df.corr().abs()
sns.heatmap(corr,annot=True)
plt.show()
corr
As we know, it is recommended to avoid having correlated features in your dataset. Indeed, a group of highly correlated features will not bring additional information (or just very few), but will increase the complexity of the algorithm, thus increasing the risk of errors.
df.head()
4. Boxplot
A boxplot is a standardized way of displaying the distribution of data based on a five number summary (“minimum”, first quartile [Q1], median, third quartile [Q3] and “maximum”).
features = ['number_courses', 'time_study', 'Marks']
for value in features:
sns.catplot(data=df, x=value, kind="box")
#for target variable
sns.catplot(data=df, x='Marks', kind='box')
<seaborn.axisgrid.FacetGrid at 0x2d3d96f1400>
The dark points are known as Outliers. Outliers are those data points that are significantly different from the rest of the dataset. They are often abnormal observations that skew the data distribution, and arise due to inconsistent data entry, or erroneous observations.
NOTICED ~
Sometimes outliers may be errors in the data and should be removed. In this case these points are correct readings yet they are so different from the other points that they appear to be incorrect.
The best way to decide wether to remove them or not is to train models with and without these data points and- compare their validation accuracy.
So we gonna let is slide as it isn’t gonna be effect our model.
There is a linear relationship between the time studied and the marks obtained. This means the more time students spend studying, the better they can score.
plt.bar(df['time_study'], df['Marks'])
plt.title("Bar Chart")
# Setting the X and Y labels
plt.xlabel('time_study')
plt.ylabel('Marks')
# Adding the legends
plt.show()
plt.bar(df['number_courses'], df['Marks'])
plt.title("Bar Chart")
# Setting the X and Y labels
plt.xlabel('number_courses')
plt.ylabel('Marks')
# Adding the legends
plt.show()
It defines the relation between the number courses opted to marks scored by the student.
Step 2: Data Preparation
Goal:-
Tasks we are going to in this step:
Now we gonna spearate the target variable and feature columns in two different dataframe and will check the shape of the dataset for validation purpose.
Split dataset into train and test dataset.
Scaling on train dataset.
1. Now we spearate the target variable and feature columns in two different dataframe and will check the shape of the dataset for validation purpose.
# Separate target and feature column in X and y variable
target = 'Marks'
# X will be the features
X = df.drop(target,axis=1)
#y will be the target variable
y = df[target]
X.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 number_courses 100 non-null int64
1 time_study 100 non-null float64
dtypes: float64(1), int64(1)
memory usage: 1.7 KB
y
0 19.202
1 7.734
2 13.811
3 53.018
4 55.299
...
95 19.128
96 5.609
97 41.444
98 12.027
99 32.357
Name: Marks, Length: 100, dtype: float64
# Check the shape of X and y variable
X.shape, y.shape
((100, 2), (100,))
# Reshape the y variable
y = y.values.reshape(-1,1)
# Again check the shape of X and y variable
X.shape, y.shape
((100, 2), (100, 1))
2. Spliting the dataset in training and testing data.
Here we are spliting our dataset into 80/20 percentage where 80% dataset goes into the training and 20% goes into testing the part.
# Split the X and y into X_train, X_test, y_train, y_test variables with 80-20% split.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Check shape of the splitted variables
X_train.shape, X_test.shape, y_train.shape, y_test.shape
((80, 2), (20, 2), (80, 1), (20, 1))
Step 3: Model Training
Goal:
In this step we are going to train our dataset on different regression algorithms. As we know that our target variable is in discrete format so we have to apply algorithm.**
Algorithms we are going to use in this step.
LinearRegression
LassoRegression
K-fold cross validation is a procedure used to estimate the skill of the model on new data. There are common tactics that you can use to select the value of k for your dataset. There are commonly used variations on cross-validation, such as stratified and repeated, that are available in scikit-learn
# Define kfold with 10 split
cv = KFold(n_splits=10, shuffle=True, random_state=42)
The goal of cross-validation is to test the model’s ability to predict new data that was not used in estimating it, in order to flag problems like overfitting or selection bias and to give an insight on how the model will generalize to an independent dataset (i.e., an unknown dataset, for instance from a real problem).
1. Linear Regression
Linear regression analysis is used to predict the value of a variable based on the value of another variable. The variable you want to predict is called the dependent variable. The variable you are using to predict the other variable’s value is called the independent variable.
Train set cross-validation
Now I will train a machine learning model using the linear regression algorithm:
#Using Linear Regression Algorithm to the Training Set
model = LinearRegression()
model.fit(X_train, y_train)
model.score(X_test, y_test)
0.9459936100591214
features = np.array([[4.508, 3],[2.513, 8],[7.23, 3],[5.678, 6], [6.567,8]])
model.predict(features)
array([[17.33351544],
[39.44913224],
[22.42787422],
[35.0328441 ],
[47.03639547]])
#Accuracy check of trainig data
from sklearn.metrics import r2_score
#Get R2 score
model.score(X_train, y_train)
0.934351593655942
#Accuracy of test data
model.score(X_test, y_test)
0.9459936100591214
# Getting kfold values
lg_scores = -1 * cross_val_score(model,
X_train,
y_train,
cv=cv,
scoring='neg_root_mean_squared_error')
lg_scores
array([4.67227093, 3.40545358, 4.52860267, 3.68722215, 3.74930852,
3.09090169, 3.42562591, 2.90132685, 3.2654887 , 3.25619774])
# Mean of the train kfold scores
lg_score_train = np.mean(lg_scores)
lg_score_train
3.5982398736808725
Prediction
Now we will perform prediction on the dataset using Logistic Regression.
# Predict the values on X_test_scaled dataset
y_predicted = model.predict(X_test)
rg = r2_score(y_test,y_predicted)*100
rg
94.59936100591214
2. Lasso Regression
Lasso regression algorithm is defined as a regularization algorithm that assists in the elimination of irrelevant parameters, thus helping in the concentration of selection and regularizes the models.
#Using Lasso Regression
from sklearn import linear_model
clf = linear_model.Lasso(alpha=0.1)
#looking for training data
clf.fit(X_train,y_train)
Lasso(alpha=0.1)
#Accuracy check for training data
clf.score(X_train,y_train)
0.9343275256643444
y_predicted1 = clf.predict(X_test)
#Accuracy check of test data
lg = r2_score(y_test,y_predicted1)*100
lg
94.50399228173838
Insight: -
cal_metric=pd.DataFrame([rg,lg],columns=["Score in percentage"])
cal_metric.index=['Linear Regression',
'Lasso Regression']
cal_metric
As we observe that the result of Linear Regression shows well defined accuracy i.e. 94%.
Step 4: Save Model
Goal:- In this step we are going to save our model in pickel format file.
import pickle
pickle.dump(model , open('Student_Marks_LinearRegresssionnew.pkl', 'wb'))
pickle.dump(clf , open('Student_Marks_LassoRegresssion.pkl', 'wb'))
import pickle
def prediction(features):
pickled_model = pickle.load(open('Student_Marks_LinearRegresssionnew.pkl', 'rb'))
Stmarks = str(list(pickled_model.predict(features)))
return str(f'The Student Marks is {Stmarks}')
We can test our model by giving our own parameters or features to predict.
number_courses = 5.00
time_study = 10.00
print(prediction([[number_courses,time_study]]))
The Student Marks is [array([54.44342034])]
Conclusion:
After observing the problem statement we have build an efficient model to overcome it. The above model helps in predicting the marks of the student. The accuracy for the prediction is 94.50% and it signifies the accurate prediction of the marks.
Checkout whole project codehere(github repo).
🚀 Unlock Your Dream Job with HiDevs Community!
🔍 Seeking the perfect job?HiDevs Community is your gateway to career success in the tech industry. Explore free expert courses, job-seeking support, and career transformation tips.
💼 We offer an upskill program in Gen AI, Data Science, Machine Learning, and assist startups in adopting Gen AI at minimal development costs.
🆓 Best of all, everything we offer is completely free! We are dedicated to helping society.
Book free of cost 1:1 mentorship on any topic of your choice —topmate
✨ We dedicate over 30 minutes to each applicant’s resume, LinkedIn profile, mock interview, and upskill program. If you’d like our guidance, check out our services here
💡 Join us now, and turbocharge your career!
Deepak Chawla LinkedIn
Vijendra Singh LinkedIn
Yajendra Prajapati LinkedIn
YouTube Channel
Instagram Page
HiDevs LinkedIn
Project Youtube Link