-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_class.py
More file actions
28 lines (24 loc) · 945 Bytes
/
model_class.py
File metadata and controls
28 lines (24 loc) · 945 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
25
26
27
28
# model_class.py
import numpy as np
class LinearRegressionNumpy:
"""Simple Linear Regression using only NumPy"""
def __init__(self):
self.weights = None
self.bias = None
def fit(self, X, y, learning_rate=0.01, epochs=1000):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(epochs):
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 -= learning_rate * dw
self.bias -= learning_rate * db
def predict(self, X):
return np.dot(X, self.weights) + self.bias
def score(self, X, y):
y_pred = self.predict(X)
ss_tot = np.sum((y - np.mean(y)) ** 2)
ss_res = np.sum((y - y_pred) ** 2)
return 1 - (ss_res / ss_tot)