-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Regression.py
56 lines (38 loc) · 1.28 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
import numpy as np
from sklearn import datasets
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
import pandas as pd
import matplotlib.pyplot as plt
class Linear_Regression:
def __init__(self, num_iter=40000, alpha=0.3):
self.weights = None
self.num_iter = num_iter
self.alpha = alpha
def fit(self, X, y):
X = np.insert(X, 0, 1, axis=1)
m, n = X.shape
self.weights = np.random.random((n, ))
for i in xrange(self.num_iter):
w_grad = X.T.dot(X.dot(self.weights) - y)
self.weights -= w_grad * (self.alpha/m)
def predict(self, X):
X = np.insert(X, 0, 1, axis = 1)
y_pre = X.dot(self.weights)
return y_pre
def main():
X, y = datasets.make_regression(n_features=1, n_samples=200, bias=100, noise=5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
lReg = Linear_Regression()
lReg.fit(X_train, y_train)
y_predict = lReg.predict(X_test)
error = np.mean((y_test-y_predict)**2)
print "Mean Square error : %0.8f"%(error)
score = r2_score(y_test, y_predict)
print "R2 Score : %0.8f"%(score)
plt.scatter(X_test[:, 0], y_test, color='black')
plt.plot(X_test[:, 0], y_predict, color='blue', linewidth=3)
plt.title("Linear Regression (%.8f Score)"%score)
plt.show()
if __name__ == "__main__":
main()