forked from Twahaaa/linear_regression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReg_class.py
More file actions
24 lines (20 loc) · 704 Bytes
/
Reg_class.py
File metadata and controls
24 lines (20 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
class LinearRegression:
def __init__(self,lr=0.001,n_iteration=1000):
self.lr=lr
self.n_iteration=n_iteration
self.weights=None
self.bias=None
def fit(self,X,y):
n_samples,n_features=X.shape
self.weights=np.zeros(n_features)
self.bias=0
for i in range(self.n_iteration):
y_pred = np.dot(X,self.weights)+self.bias
dw=(1/n_samples)*np.dot(X.T,(y_pred-y))
db=(1/n_samples)*np.sum(y_pred-y)
self.weights=self.weights-self.lr*dw
self.bias=self.bias-self.lr*db
def predict(self,X):
y_pred=np.dot(X,self.weights)+self.bias
return y_pred