-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Regression_From_Scratch.py
More file actions
70 lines (54 loc) · 1.63 KB
/
Copy pathLinear_Regression_From_Scratch.py
File metadata and controls
70 lines (54 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pandas as pd
import numpy
import matplotlib.pyplot as plt
#loading the csv
data= pd.read_csv('datasets/student_scores.csv')
print(data.head())
print(data.shape)
#plotting the data
plt.scatter(data.Hours, data.Scores, color='blue')
plt.title('Hours vs Scores')
plt.xlabel('Hours Studied')
plt.ylabel('Scores Obtained')
plt.show()
#Defining loss function
def loss_function(m, b, points):
total_error = 0
n = len(points)
for i in range(n):
x = points.iloc[i].Hours
y = points.iloc[i].Scores
# Corrected parentheses: (y - (mx + b))^2
total_error += (y - (m * x + b))**2
return total_error / n
#Defining gradient descent function
def gradient_descent(m_now, b_now, points, L):
m_gradient= 0
b_gradient= 0
n= len(points)
for i in range(n):
x= points.iloc[i].Hours
y= points.iloc[i].Scores
# Partial derivatives of MSE: -2/n * sum(y - (mx + b)) * x
m_gradient += -(2/n) * (y - (m_now * x + b_now)) * x
b_gradient += -(2/n) * (y - (m_now * x + b_now))
m = m_now - L * m_gradient
b = b_now - L * b_gradient
return m, b
#Exicution
m= 0
b= 0
l= 0.0001
epochs= 1000
for i in range(epochs):
if i % 50 == 0:
print(f"epoch {i} loss: {loss_function(m,b,data)}")
m,b= gradient_descent(m,b, data, l)
print(f"the weight is {m} and the bias is {b}")
#plotting the regression line
plt.scatter(data.Hours, data.Scores, color='black') #grph points
plt.plot(data.Hours, m*data.Hours + b, color='red') #tend line
plt.title('Hours vs Scores with Regression Line')
plt.xlabel('Hours Studied')
plt.ylabel('Scores Obtained')
plt.show()