Linear Regression Implementation:
In the previous post we saw about the theoretical explanation about the linear regression. Now let's see it's implementation using python.
If you haven't read my post about linear regression, then click here
Data set used : - Student Grades
import numpy as np
import pandas as pd
import sklearn
from sklearn import linear_model
from sklearn.utils import shuffle
data = pd.read_csv("student-mat.csv",sep=';')
data = data[['G1','G2','G3','studytime','failures','absences']]
print(data.head()) ''' head() prints the first tuples of the data default value is 5 '''
predict = "G3"
X = np.array(data.drop([predict],1)) #features
y = np.array(data[predict]) #labels
''' this is gonna split the data into test and train data with 10% as the test data size '''
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X,y,test_size=0.1)
model = linear_model.LinearRegression() #create the model
''' Train the model'''
model.fit(x_train,y_train)
accuracy = model.score(x_test,y_test) ''' This gives the accuracy of the model to predict the values '''
print("Accuraccy {}".format(accuracy))
print("Coefficient :",model.coef_) ''' Gives the slope '''
print("Intercept :",model.intercept_)
predictions = model.predict(x_test)
print('Predictions')
for x in range(len(predictions)):
print(predictions[x],x_test[x],y_test[x])
As you can see form the below outputs, when we executed the program for the second time the accuracy level dropped form 89% to 81%. So, we need to save our model with maximum accuracy.How to save the model with maximum accuracy?
Output:-
G1 G2 G3 studytime failures absences0 5 6 6 2 0 61 5 5 6 2 0 43 15 14 15 3 0 22 7 8 10 2 3 104 6 10 10 2 0 4
Accuraccy 0.8982728958031367
Coefficient : [ 0.16172694 0.98027571 -0.20876813 -0.31050997 0.03495033]Intercept : -1.5754139422756488
Predictions7.8160282292435745 [10 8 2 0 10] 810.638979646447401 [11 11 2 0 2] 106.371563440971993 [8 7 1 0 0] 610.465161852407348 [11 11 3 0 3] 1112.597274977578584 [14 13 4 0 0] 1414.042128123637497 [13 14 1 0 0] 139.505143830469267 [ 7 10 2 1 25] 11 10.339319107958076 [10 11 4 0 10] 1113.928376231032752 [11 14 1 0 6] 1412.081576703155246 [13 12 1 0 0] 12 7.400584945046997 [8 9 1 3 0] 10 11.27294199217762 [ 8 12 1 0 0] 129.196382589739558 [ 9 10 3 0 4] 105.002910063736628 [ 6 6 2 1 13] 8 7.07503809284365 [ 8 8 4 0 10] 8 6.302596642825093 [8 7 2 0 4] 6 12.922984951320702 [13 13 2 0 2] 118.332114861454244 [8 9 1 0 0] 812.733542158634021 [14 12 2 0 20] 13 16.187265966435127 [15 16 2 0 2] 16 11.892133910468566 [14 11 1 0 18] 13 5.792300246767593 [7 7 3 0 0] 83.8568643957562667 [5 5 2 0 4] 618.239643665184136 [16 18 2 0 0] 18 -1.9070772737097068 [5 0 1 3 0] 0 13.834293863414928 [13 14 3 0 6] 14 11.632199268943827 [13 12 2 1 2] 1219.961843377607057 [18 19 1 0 6] 1915.206990256194004 [15 15 2 0 2] 14 8.263148063307344 [8 9 2 0 4] 10 11.98975042488849 [12 12 1 0 2] 14 10.239834861974414 [12 10 2 0 14] 11 8.446800619840857 [10 9 2 0 0] 915.125932606202575 [16 15 4 0 7] 177.550301840769091 [7 9 2 2 6] 8 3.521238854149594 [7 5 3 1 0] 0 11.121983678257424 [ 9 12 3 0 3] 11 -1.0696297249202846 [7 0 3 0 0] 0 8.42487500550287 [9 9 2 0 4] 105.760459064565051 [7 7 2 1 2] 7
Output 2:
G1 G2 G3 studytime failures absences0 5 6 6 2 0 62 7 8 10 2 3 101 5 5 6 2 0 4 3 15 14 15 3 0 24 6 10 10 2 0 4Accuraccy 0.8130042352757954Coefficient : [ 0.14958581 0.98020592 -0.23107325 -0.28833124 0.04045218]Intercept : -1.3617056273559687Predictions12.195249504944599 [10 13 4 0 6] 137.0643128006959675 [8 8 3 0 2] 109.752064477957072 [10 10 2 1 14] 910.696984164357781 [10 11 2 0 6] 1115.677131419596725 [16 15 2 0 10] 158.493859213317853 [10 9 2 0 0] 0 9.465294725293901 [ 9 9 1 0 22] 910.154516368964485 [ 9 11 3 0 2] 113.6863610639193425 [6 5 2 1 0] 0 -1.0078247421768198 [7 0 3 0 0] 0 -0.9263372968086295 [6 0 2 0 0] 011.896040401967113 [11 12 1 0 2] 1115.2039281355093 [15 15 2 0 2] 14 15.688771277982474 [16 15 3 0 16] 15 15.041536320449486 [16 15 3 0 0] 158.000066737921825 [6 9 1 1 4] 89.24299188361126 [10 10 3 0 0] 9 7.001089365007859 [10 8 1 3 3] 7 6.928624546719691 [ 6 8 1 3 16] 8 5.537818209325654 [7 6 1 0 5] 79.820745913586379 [11 10 2 1 12] 1015.2039281355093 [15 15 2 0 2] 16 13.774381722887414 [13 14 3 0 4] 14 10.396646402362787 [10 11 4 0 10] 11 5.335557285096596 [7 6 1 0 0] 09.554969503992005 [10 10 2 0 2] 105.268183807325057 [ 6 6 2 2 22] 4 14.01826098331542 [11 14 1 0 6] 14 4.136501047487263 [6 5 2 0 4] 6 9.94668534271124 [12 10 3 0 10] 12 10.995572699323075 [13 11 3 0 8] 1113.768480763260206 [11 12 2 0 54] 1113.280185095731193 [12 13 2 0 14] 12 12.126530576979667 [12 12 1 0 4] 13 16.40240129744207 [17 16 2 0 0] 17 17.908485621420148 [17 17 2 0 13] 17 10.1417103592256 [11 11 4 0 0] 119.405383698671075 [ 9 10 2 0 2] 98.545951256549415 [10 9 3 0 7] 96.476988869785805 [8 7 2 0 6] 9
Post a Comment
Post a Comment