-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_regression.py
70 lines (53 loc) · 2.14 KB
/
Linear_regression.py
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
70
import numpy as np
#Defining the r2 score function.
def r2_score(y_true, y_pred):
corr_matrix = np.corrcoef(y_true, y_pred)
corr = corr_matrix[0, 1]
return corr ** 2
#Defining the linear regression class:
class LinearRegression:
def __init__(self, learning_rate = 0.001, n_iters =1000):
self.lr = learning_rate
self.n_iters = n_iters
self.weights = None
#Implementing the fit method.
def fit(self, X, y):
n_samples, n_features = X.shape
#init parameters
self.weights = np.zeros(n_features)
self.bias = 0
#gradient descent
for _ in range(self.n_iters):
y_predicted = np.dot(X, self.weights) + self.bias
#compute gradients
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
#update parameters
self.weights -= self.lr * dw
self.bias -= self.lr * db
def predict(self, X):
y_approximated = np.dot(X, self.weights) + self.bias
return y_approximated
#Testing and evaluation.
if __name__ == "__main__":
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import datasets
def mean_squared_error(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
X, y = datasets.make_regression(n_samples=100, n_features=1, noise=20, random_state=4)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state = 1234)
regressor = LinearRegression(learning_rate=0.01, n_iters=1000)
regressor.fit(X_train, y_train)
predictions = regressor.predict(X_test)
mse = mean_squared_error(y_test, predictions)
print("MSE:", mse)
accu = r2_score(y_test, predictions)
print("Accuracy:", accu)
y_pred_line = regressor.predict(X)
cmap = plt.get_cmap("viridis")
fig = plt.figure(figsize=(8, 6))
m1 = plt.scatter(X_train, y_train, color=cmap(0.9), s=10)
m2 = plt.scatter(X_test, y_test, color = cmap(0.5), s=10)
plt.plot(X, y_pred_line, color='black', linewidth=2, label='prediction')
plt.show()