-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Regression_Model.py
More file actions
69 lines (45 loc) · 1.39 KB
/
Copy pathLinear_Regression_Model.py
File metadata and controls
69 lines (45 loc) · 1.39 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 as np
import matplotlib.pyplot as plt
dataset = pd.read_csv('Salary Data.csv')
x = dataset['YearsExperience']
y = dataset['Salary']
class LinearRegression:
def __init__(self):
self.w = 0
self.b = 0
def cost_function(self,x,y,w,b):
n = len(x)
cost_sum = 0
for i in range(n):
f = w * x[i] + b
cost = (f-y[i])**2
cost_sum += cost
return (1/(2*n)) * cost_sum
def gradient_funciton(self,x,y,w,b):
n = len(x)
dc_dw = dc_db = 0
for i in range(n):
f = w*x[i] + b
dc_dw += (f - y[i]) * x[i]
dc_db += (f - y[i])
dc_dw = (1/n) * dc_dw
dc_db = (1/n) * dc_db
return dc_dw, dc_db
def gradient_descent(self,x,y,alpha = 0.01,iterations = 10000):
w = b = 0
for i in range(iterations):
dc_dw, dc_db = self.gradient_funciton(x,y,w,b)
w = w - alpha * dc_dw
b = b - alpha * dc_db
return w,b
def fit(self, x, y):
self.w, self.b = self.gradient_descent(x,y)
def predict(self,x):
return self.w * x + self.b
model = LinearRegression()
model.fit(x,y)
print(model.predict(4))
plt.plot(x, (x*(model.w) + (model.b)))
plt.scatter(x,y)
plt.show()