-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinear_test.py
37 lines (28 loc) · 1.11 KB
/
linear_test.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
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from LinearRegression import LinearRegression
def mse(y_test, predictions):
'''Calculate a Mean-squared error'''
return np.mean((y_test-predictions)**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)
fig = plt.figure(figsize=(8,6))
plt.scatter(X[:], y, color='b', marker='o', s=30)
plt.show()
# Default learning rate doesn't come up with an optimal fit.
reg = LinearRegression(λ=0.1)
reg.fit(X_train, y_train)
predictions = reg.predict(X_test)
mse = mse(y_test, predictions)
print(mse)
y_pred_line = reg.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)
m1 = plt.scatter(X_test, y_test, color=cmap(0.5), s=10)
plt.plot(X, y_pred_line, color='red', linewidth=2, label='Prediction')
plt.show()
print("End of linear_test.py")