-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting_Linear_Regression.py
More file actions
58 lines (40 loc) · 1.21 KB
/
Testing_Linear_Regression.py
File metadata and controls
58 lines (40 loc) · 1.21 KB
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
# testing out LinearRegression Method
from Linear_Regression import *
from sklearn.datasets import load_boston
import matplotlib
from matplotlib import pyplot as plt
#%matplotlib inline
import numpy as np
from numpy.linalg import det
from scipy.stats import ortho_group
from numpy.testing import assert_allclose
boston=load_boston()
X_raw=boston.data
Y_raw=boston.target
shuffle=np.random.permutation(len(Y_raw))
X_full=X_raw[shuffle].copy()
Y_full=Y_raw[shuffle].copy()
split=int(0.8*len(Y_full))
X_train=X_full[:split,:]
Y_train=Y_full[:split]
X_test=X_full[split:,:]
Y_test=Y_full[split:]
model=LinearRegression()
model.fit(X_train,Y_train)
def GOF_report(label,model,X,Y):
Y_hat=model.predict(X)
plt.scatter(x=Y,y=Y_hat,label=label,alpha=0.5,color='red')
plt.title('Predicted vs Actual')
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.legend(loc='upper left')
mse=np.mean((Y-Y_hat)**2)
y_bar = np.mean(Y)
r2 = 1 - np.sum( (Y-Y_hat)**2 ) / np.sum( (Y-y_bar)**2 )
print("{label: <16} mse={mse:.2f} r2={r2:.2f}".format(**locals()))
plt.figure(figsize=(16,6))
plt.subplot(1,2,1)
GOF_report("Training Set",model,X_train,Y_train)
plt.subplot(1,2,2)
GOF_report("Test Set",model,X_test,Y_test)
plt.show()