Skip to content

Commit 8a73b8f

Browse files
committed
Add Linear Regression
1 parent 2f1c5ed commit 8a73b8f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
29.4 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pandas as pd
2+
from matplotlib import pyplot as plt
3+
4+
def train(X,y):
5+
mean_X = X.mean()
6+
mean_y = y.mean()
7+
8+
numerator = 0
9+
denominator = 0
10+
11+
for i in range(0,len(X)):
12+
numerator += (X[i] - mean_X)*(y[i] - mean_y)
13+
denominator += (X[i] - mean_X)**2
14+
15+
m = numerator/denominator
16+
b = mean_y - (m*mean_X)
17+
18+
return m,b
19+
20+
def plot(X,y,predicted):
21+
plt.xlabel("Number of hours spent driving")
22+
plt.ylabel("Risk Score")
23+
plt.scatter(X,y)
24+
plt.plot(X,predicted,color="green")
25+
plt.savefig("Ass1.jpg")
26+
plt.show()
27+
28+
29+
data = pd.read_csv("data.csv")
30+
X = data.iloc[:,0]
31+
y = data.iloc[:,1]
32+
33+
m,b = train(X,y)
34+
predicted = []
35+
36+
for i in range(0,len(X)):
37+
temp = (m*X[i])+b
38+
predicted.append(temp)
39+
print(temp,y[i])
40+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
X,Y
2+
8,3
3+
2,10
4+
11,3
5+
6,6
6+
5,8
7+
4,12
8+
12,1
9+
9,4
10+
6,9
11+
1,14

0 commit comments

Comments
 (0)