Skip to main content

The future of AI in business and its potential to transform industries

Predicting Exam marks according to the study hours

 Predicting Exam marks according to the study hours



Hi.. this another machine learning exercise, this time we will predict the marks fo our exam according to the study hours. 

The machine learning technique which we gonna use is Linear Regression.

This method comes under supervised machine learning. It is one of the basic foundations of machine learning.

Let's quickly recap what is linear regression.

Linear Regression

  • Linear regression comes under the category of supervised machine learning.
  • Simple linear regression is useful for finding the relationship between two continuous variables.
  • It looks for a statistical relationship but the deterministic relationship.


Our main task for this project is:-

What marks I will get if I make a habit to study 9.25 hour every day???  


Our task will be completed on 6 steps.

1. Collecting all data and importing required libraries  

2. Observing or Wrangling Data

3. Preparing Data

4. Creating the model

5. Checking accuracy

6. Plotting model 



1. Collecting all data and importing required libraries  

First of all, we will collect all the required libraries and modules which gonna need for our task.

For this task, the modules we need are pandas, NumPy, sklearn, matplotlib.

After importing the modules now read the data from the dataset (CSV file)

The dataset consists of 25 rows and 2 columns.


code  


import pandas as pd

import numpy as np

from matplotlib import pyplot as plt

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_absolute_error  

read=pd.read_csv('studentperformance.csv')

read.head(5),read.shape   





2. Observing or Wrangling Data  

In this section, we will observe the data and try to find out insight it, and also remove the null values and if there any.

In this section, we will also observe the correlation between the variables. 

Through the correlation, we are able to understand how strongly the variables are related to each other.


code 


read.plot(x='hours',y='scores',style='o')

plt.title('Hours vs Scores',color='red')

plt.xlabel('Hours of study',color='green')

plt.ylabel('marks from class test',color='green')

plt.show() 


read.corr()



3. Preparing Data

In this section, we will prepare the data for our model.

we will use train_test_split built-in function of the sklearn and create the train and test data.

we will create 20 train data and 5 test data.


code  


x=np.array(read[['hours']])

y=np.array(read[['scores']])

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

x_train.size,y_train.size,x_test.size,y_test.size  


4. Creating the model  

In this section, we will create our machine learning model and also train and test the model.

we know that the line equation is 

               y=mx+c,

where m = slope or gradient

           c = intercept on y-axis 

           x and y are the variables

we will also find those values to predict our model.


code  

model=LinearRegression()

model.fit(x_train,y_train)

predict=model.predict(x_test)

model.coef_        

model.intercept_   


5. Checking accuracy

Here we will calculate R squared value and mean absolute error.

R squared value also known as the coefficient of determination. it is a statistical measure that explains how much variance of a dependent variable is explained by the independent variable.

Mean absolute error is the measure of error between the paired observation between the same phenomenon.


code   


r_square=model.score(x_train,y_train)

r_square 


mean_absolute_error=mean_absolute_error(y_test,predict)

mean_absolute_error


6. Plotting model

Now let's plot our model.


code 


line=model.coef_*x + model.intercept_

plt.scatter(x,y)

plt.plot(x,line,color='red')

plt.title('Hours vs Scores',color='red')

plt.xlabel('Hours of study',color='green')

plt.ylabel('marks from class test',color='green')

plt.legend(['slope','score'])

plt.show()



 Now its time to find the answer to our question.

What marks I will get if I make a habit to study 9.25 hour every day??? 


code 

hours=9.25

my_predict=model.predict([[hours]])

my_predict[0][0]

 


our result is 93.691173 marks


Video demonstration  






Follow me on: 

Linkedin - https://www.linkedin.com/in/somen-das-6a933115a/  

Instagram - https://www.instagram.com/somen912/?hl=en

And don't forget to subscribe to the blog.

so...

Thanks for your time and stay creative...


Popular posts from this blog

What is machine learning and it's types?

 What is machine learning(ML)? Machine learning is a subset of artificial intelligence. Machine learning enables computers or machines to make data-driven decisions rather than being explicitly programmed for a certain task. These programs or algorithms are designed in a way that they learn and improve over time when are exposed to new data. Examples:- 1. Product recommendations   While checking for a product did you noticed when it recommends a product similar to what you are looking for? or did you noticed "the person bought this product also bought this" combination of products? How are they doing this recommendation? This is machine learning. 2. Email spam and malware filtering  There are a number of spam filtering approaches that email clients use.  To ascertain that these spam filters are continuously updated they are powered by machine learning. 3. Online customer support A number of websites nowadays offer the option to chat with customer support representati...

PIC18 Timer programming in C

The PIC18 timer is divided into 4 types Timer 0 Timer 1 Timer 2 Timer 3 PIC18 timers can be used to generate a time delay or as a counter to count external event happening outside the microcontroller. In this article, we will see how to generate a time delay by programming the PIC18 timer. Timer 0 The timer 0 module has the following features Software is scalable as an 8 bit or 16-bit timer/counter. Readable and writable Dedicated 8 bit software programmable Prescaler Clock source selectable to be internal or external Edge select for external clock Register required for Timer 0 Control register Each timer has a control register called TCON to set the various timer operation modes. T0CON is an 8-bit register used for control of timer 0. TOCON TMR0ON (Timer0 on/off control bit)                                       1 = Enable timer 0                ...

Interface relay with PIC18 microcontroller

Hi... today we will talk about another important component used in the embedded domain called a relay. Relays are electric switch which uses electromagnetism to either form or breaks the existing circuits. With the help of a relay, you can trigger a high voltage operation by a low voltage input signal. Relay is a highly versatile component that is as effective in a complex circuit as in a simple circuit. In this article, we will talk about how to interface relay with PIC18 controller. We will cover the topics in the following points: About the interfacing task Software tools used Required components Circuit diagram Code for controller Upload the HEX file Run simulation About the interfacing task In the interface relay task, with the help of a push-button, we will trigger the relay to control the lighting of the bulb which is of higher voltage. When we press the push button the microcontroller will trigger the relay to change its state, when the relay changes its state from normally clo...