You are on page 1of 15

11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

LINEAR REGRESSION WITH ONE VARIABLE

IMPORTING REQUIRED LIBRARIES

In [1]: import numpy as np


import matplotlib.pyplot as plt

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 1/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

LOADING THE DATA-SET

In [2]: Data_Set = np.loadtxt("LinearReg_Univariate.txt", dtype = float, delimiter


Data_Set

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 2/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

Out[2]: array([[ 6.1101 , 17.592 ],


[ 5.5277 , 9.1302 ],
[ 8.5186 , 13.662 ],
[ 7.0032 , 11.854 ],
[ 5.8598 , 6.8233 ],
[ 8.3829 , 11.886 ],
[ 7.4764 , 4.3483 ],
[ 8.5781 , 12. ],
[ 6.4862 , 6.5987 ],
[ 5.0546 , 3.8166 ],
[ 5.7107 , 3.2522 ],
[14.164 , 15.505 ],
[ 5.734 , 3.1551 ],
[ 8.4084 , 7.2258 ],
[ 5.6407 , 0.71618],
[ 5.3794 , 3.5129 ],
[ 6.3654 , 5.3048 ],
[ 5.1301 , 0.56077],
[ 6.4296 , 3.6518 ],
[ 7.0708 , 5.3893 ],
[ 6.1891 , 3.1386 ],
[20.27 , 21.767 ],
[ 5.4901 , 4.263 ],
[ 6.3261 , 5.1875 ],
[ 5.5649 , 3.0825 ],
[18.945 , 22.638 ],
[12.828 , 13.501 ],
[10.957 , 7.0467 ],
[13.176 , 14.692 ],
[22.203 , 24.147 ],
[ 5.2524 , -1.22 ],
[ 6.5894 , 5.9966 ],
[ 9.2482 , 12.134 ],
[ 5.8918 , 1.8495 ],
[ 8.2111 , 6.5426 ],
[ 7.9334 , 4.5623 ],
[ 8.0959 , 4.1164 ],
[ 5.6063 , 3.3928 ],
[12.836 , 10.117 ],
[ 6.3534 , 5.4974 ],
[ 5.4069 , 0.55657],
[ 6.8825 , 3.9115 ],
[11.708 , 5.3854 ],
[ 5.7737 , 2.4406 ],
[ 7.8247 , 6.7318 ],
[ 7.0931 , 1.0463 ],
[ 5.0702 , 5.1337 ],
[ 5.8014 , 1.844 ],
[11.7 , 8.0043 ],
[ 5.5416 , 1.0179 ],
[ 7.5402 , 6.7504 ],
[ 5.3077 , 1.8396 ],
[ 7.4239 , 4.2885 ],
[ 7.6031 , 4.9981 ],
[ 6.3328 , 1.4233 ],
[ 6.3589 , -1.4211 ],
[ 6.2742 , 2.4756 ],
[ 5.6397 , 4.6042 ],
[ 9.3102 , 3.9624 ],
[ 9.4536 , 5.4141 ],
[ 8.8254 , 5.1694 ],
localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 3/15
11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook
[ 5.1793 , -0.74279],
[21.279 , 17.929 ],
[14.908 , 12.054 ],
[18.959 , 17.054 ],
[ 7.2182 , 4.8852 ],
[ 8.2951 , 5.7442 ],
[10.236 , 7.7754 ],
[ 5.4994 , 1.0173 ],
[20.341 , 20.992 ],
[10.136 , 6.6799 ],
[ 7.3345 , 4.0259 ],
[ 6.0062 , 1.2784 ],
[ 7.2259 , 3.3411 ],
[ 5.0269 , -2.6807 ],
[ 6.5479 , 0.29678],
[ 7.5386 , 3.8845 ],
[ 5.0365 , 5.7014 ],
[10.274 , 6.7526 ],
[ 5.1077 , 2.0576 ],
[ 5.7292 , 0.47953],
[ 5.1884 , 0.20421],
[ 6.3557 , 0.67861],
[ 9.7687 , 7.5435 ],
[ 6.5159 , 5.3436 ],
[ 8.5172 , 4.2415 ],
[ 9.1802 , 6.7981 ],
[ 6.002 , 0.92695],
[ 5.5204 , 0.152 ],
[ 5.0594 , 2.8214 ],
[ 5.7077 , 1.8451 ],
[ 7.6366 , 4.2959 ],
[ 5.8707 , 7.2029 ],
[ 5.3054 , 1.9869 ],
[ 8.2934 , 0.14454],
[13.394 , 9.0551 ],
[ 5.4369 , 0.61705]])

DIVIDING THE DATA-SET INTO INDIVISUAL X AND Y AND


CONVERTING THEM INTO A VECTOR

In [3]: #Getting the indivisual x and y


a = Data_Set[ : , 0]
b = Data_Set[ : , 1]
#Converting x and y to vectors from array
p = a[:, np.newaxis]
q = b[:, np.newaxis]

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 4/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

PLOTTING THE DATA-POINTS

In [4]: #plotting the given points of dataset


print(plt.plot(p, q, '.'))

[<matplotlib.lines.Line2D object at 0x0000021EACB9CB10>]

DIVIDING THE DATA-POINTS INTO TRAINING AND TESTING


SETS

In [5]: x_Train = p[ :70] #Training set of x


x_Test = p[70:] #Testing set of x
y_Train = q[ :70] #Training set of y
y_Test = q[70:] #Testing set of y

In [6]: s1 = np.size(x_Train) #size of training set


s2 = np.size(x_Test) #size of testing set

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 5/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

TRAINING THE MODEL

GIVING THE RANDOM VALUES FOR T0 AND T1

In [7]: #Training
#Getting random values of T1 and T0
rng = np.random.default_rng()
T0 = rng.random()
T1 = rng.random()

GETTING THE HYPOTHESIS FUNCTION

In [8]: #getting predicted y(hypothesis)


h = ((x_Train * T1) + T0)

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 6/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

PLOTING INITIAL RANDOM LINE

In [23]: #lotting the points and the randomly predicted line


print(plt.plot(p, q, '.', label = 'Given Data-points'))
print(plt.plot(x_Train, h,color = 'red',label = 'Randomly Predicted line'))
print(plt.xlabel("X -values"))
print(plt.legend())
print(plt.ylabel("Y -values"))

[<matplotlib.lines.Line2D object at 0x0000021EAEC16F50>]


[<matplotlib.lines.Line2D object at 0x0000021EAEBBB350>]
Text(0.5, 0, 'X -values')
Legend
Text(0, 0.5, 'Y -values')

GETTING THE MEAN SQUARE ERROR

In [10]: #SE = square error


SE = ((h - y_Train) ** 2)
Sum_SE = np.sum(SE)

In [11]: #mean square error = cost function


Cost_Func = (Sum_SE / (2 * s1))

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 7/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

GIVING THE NO.OF ITERATIONS AND ALPHA(LEARNING RATE)


VALUE

In [12]: #giving the no.of iterations


Iter_No = 200000
#giing the learning rate
Alpha = 0.0001

In [13]: L = list([])
L.append(Cost_Func)

APPLYING GRADIENT DESCENT ALGORITHM

In [14]: #doing the iterations


print(f"Iteration 1:Cost funcion = {Cost_Func}")
Diff_T1 = (np.sum((h - y_Train) * x_Train)) / s1
Diff_T0 = (np.sum(h - y_Train)) / s1
for i in range(2, Iter_No+1):
T1 -= (Alpha)*(Diff_T1)
T0 -= (Alpha)*(Diff_T0)
h1 = ((x_Train * T1) + T0)
SE = ((h1 - y_Train) ** 2)
Sum_SE = np.sum(SE)
Cost_Func = (Sum_SE / (2 * s1))
Diff_T1 = np.sum((h1 - y_Train) * x_Train) / s1
Diff_T0 = np.sum(h1 - y_Train) / s1
L.append(Cost_Func)
if (i % 10000 == 0):
print(f"Iteration {i}:Cost funcion = {Cost_Func}")

Iteration 1:Cost funcion = 6.558596484231858


Iteration 10000:Cost funcion = 6.003137880644956
Iteration 20000:Cost funcion = 5.638839277577604
Iteration 30000:Cost funcion = 5.392851210999644
Iteration 40000:Cost funcion = 5.22675085159866
Iteration 50000:Cost funcion = 5.114593665370311
Iteration 60000:Cost funcion = 5.038860930847485
Iteration 70000:Cost funcion = 4.987723350892271
Iteration 80000:Cost funcion = 4.953193340849012
Iteration 90000:Cost funcion = 4.929877384273991
Iteration 100000:Cost funcion = 4.914133576776481
Iteration 110000:Cost funcion = 4.903502768054512
Iteration 120000:Cost funcion = 4.896324447683099
Iteration 130000:Cost funcion = 4.891477376806147
Iteration 140000:Cost funcion = 4.888204452936696
Iteration 150000:Cost funcion = 4.885994452107912
Iteration 160000:Cost funcion = 4.884502176742507
Iteration 170000:Cost funcion = 4.883494536502206
Iteration 180000:Cost funcion = 4.882814140057003
Iteration 190000:Cost funcion = 4.882354710883648
Iteration 200000:Cost funcion = 4.882044487091595

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 8/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

GETTING THE FINAL COST FUNCION

In [15]: #printing final cost funcion


print(f"The Final Cost Function is :-{Cost_Func}")
#Training completes

The Final Cost Function is :-4.882044487091595

PLOTTNG COST FUNCTION GRAPH VS NO.OF ITERATIONS

In [16]: I = np.arange(1,200001)
fig = plt.figure()
fig.set_figheight(8)
fig.set_figwidth(15)
print(plt.xlabel("No.of Iterations"))
print(plt.ylabel("Cost Function"))
print(plt.plot(I, L, 'green', label = 'Given Data-Points'))
print(plt.title("Cost function vs No of Iterations graph"))
print(plt.legend())

Text(0.5, 0, 'No.of Iterations')


Text(0, 0.5, 'Cost Function')
[<matplotlib.lines.Line2D object at 0x0000021EACB539D0>]
Text(0.5, 1.0, 'Cost function vs No of Iterations graph')
Legend

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 9/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

PLOTING FINAL PREDICTED LINE

TESTING OF MODEL

GETTING THE TESTING COST FUNCTION

VALIDATION

In [17]: h_Valid = ((x_Train * T1) + T0)


loss_Valid = h_Valid - y_Train
loss_Valid

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 10/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

Out[17]: array([[-13.59177115],
[ -5.80517009],
[ -6.86950355],
[ -6.81836563],
[ -3.11325366],
[ -5.25082583],
[ 1.23593351],
[ -5.13852289],
[ -2.16244381],
[ -1.0400533 ],
[ 0.28498892],
[ -2.16757206],
[ 0.40910151],
[ -0.56106269],
[ 2.7398552 ],
[ -0.35980004],
[ -1.00859194],
[ 2.30330679],
[ 0.71883761],
[ -0.27529433],
[ 0.95321662],
[ -1.35064909],
[ -0.98156123],
[ -0.93685396],
[ 0.28565731],
[ -3.75777306],
[ -1.71244875],
[ 2.57272828],
[ -2.49999883],
[ -1.48964711],
[ 4.22586393],
[ -1.44070004],
[ -4.49564993],
[ 1.89764518],
[ -0.10660024],
[ 1.55175136],
[ 2.18604392],
[ 0.02335394],
[ 1.68082596],
[ -1.21510401],
[ 2.62841178],
[ 0.98420217],
[ 5.10469175],
[ 1.16962726],
[ -0.74376877],
[ 4.09355893],
[ -2.33906761],
[ 1.79834095],
[ 2.47651704],
[ 2.32324472],
[ -1.09220067],
[ 1.23037536],
[ 1.23486822],
[ 0.73302174],
[ 2.83511361],
[ 5.70977235],
[ 1.71487635],
[ -1.14932414],
[ 3.74782908],
[ 2.46237827],
[ 1.97878161],
localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 11/15
11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook
[ 3.66390626],
[ 3.6571238 ],
[ 2.14597604],
[ 1.84245769],
[ 0.39969222],
[ 0.78918422],
[ 1.00814497],
[ 2.27492062],
[ -0.49333604]])

VALIDATION GRAPH

In [18]: #Validation plot


h_Valid = np.reshape(h_Valid, newshape = ([70,1]))
y_Train = np.reshape(y_Train, newshape = ([70,1]))
print(plt.plot(h_Valid, y_Train, '.',label = 'Distortion in Validation Data
x = np.arange(25)
y = x
print(plt.plot(x, y, '-',label = 'Y=X line'))
print(plt.ylabel("Predicted Values"))
print(plt.xlabel("Actual Values"))
print(plt.title("Validation Graph"))
plt.legend()

[<matplotlib.lines.Line2D object at 0x0000021EAEA755D0>]


[<matplotlib.lines.Line2D object at 0x0000021EAEAA24D0>]
Text(0, 0.5, 'Predicted Values')
Text(0.5, 0, 'Actual Values')
Text(0.5, 1.0, 'Validation Graph')

Out[18]: <matplotlib.legend.Legend at 0x21eaea74e90>

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 12/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

TESTING

In [19]: #Testing
h_Test = (T1 * x_Test) + T0
loss_Test = h_Test - y_Test
loss_Test

Out[19]: array([[ 1.98771109],


[ 1.39382333],
[ 2.60137354],
[ 1.95271913],
[ 5.42513302],
[ 4.21100739],
[ 1.77184438],
[-2.94583733],
[ 2.07499985],
[ 0.7805076 ],
[ 3.07910669],
[ 2.72745624],
[ 3.60635247],
[ 0.69828593],
[-0.87291145],
[ 2.54937337],
[ 0.76141503],
[ 2.94795432],
[ 3.16456674],
[-0.03928847],
[ 1.6886109 ],
[ 1.47405959],
[-3.48021687],
[ 1.08040888],
[ 6.38687334],
[ 3.38963704],
[ 2.60271194]])

In [20]: ### TESTING GRAPH

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 13/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

In [21]: #Testing plot


h_Test = np.reshape(h_Test, newshape = ([27,1]))
y_Test = np.reshape(y_Test, newshape = ([27,1]))
print(plt.plot(h_Test, y_Test, '.',label = 'Distortion in Testing'))
x = np.arange(12)
y = x
print(plt.plot(x, y, '-',label = 'Y=X line'))
print(plt.ylabel("Predicted Values"))
print(plt.xlabel("Actual Values"))
print(plt.title("Testing Graph"))
plt.legend()

[<matplotlib.lines.Line2D object at 0x0000021EAEAED5D0>]


[<matplotlib.lines.Line2D object at 0x0000021EAEAF9B50>]
Text(0, 0.5, 'Predicted Values')
Text(0.5, 0, 'Actual Values')
Text(0.5, 1.0, 'Predicted values vs Actual values')

Out[21]: <matplotlib.legend.Legend at 0x21eaeaece90>

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 14/15


11/2/23, 11:31 PM LINEAR REGRESSION WITH ONE VARIABLE - Jupyter Notebook

In [22]: #printing final line


print(plt.plot(p, q, '.', label = 'Given Data-Points'))
print(plt.plot(x_Train, h1,color = "purple", label = 'Training'))
print(plt.xlabel("X -values"))
print(plt.ylabel("Y -values"))
print(plt.legend())
print(plt.title("Linear Regression Model(with one Variable)"))

[<matplotlib.lines.Line2D object at 0x0000021EAEBACB50>]


[<matplotlib.lines.Line2D object at 0x0000021EAEB88250>]
Text(0.5, 0, 'X -values')
Text(0, 0.5, 'Y -values')
Legend
Text(0.5, 1.0, 'Linear Regression Model(with one Variable)')

localhost:8890/notebooks/CAL201/SUPERVISED LEARNING/LINEAR REGRESSION WITH ONE VARIABLE.ipynb# 15/15

You might also like