-
Notifications
You must be signed in to change notification settings - Fork 0
/
Boston.py
37 lines (29 loc) · 986 Bytes
/
Boston.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.datasets import load_boston
from sklearn.preprocessing import scale
from sklearn.model_selection import KFold
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import cross_val_score
boston = load_boston()
X = scale(boston.data)
y = boston.target
kf = KFold(n_splits=5, shuffle=True, random_state=42)
maxScore = -100.0
optimalP = 0
for i in np.linspace(1, 10, 200):
reg = KNeighborsRegressor(n_neighbors=5,
weights='distance',
p=i)
scores = cross_val_score(reg,
X,
y,
scoring='neg_mean_squared_error',
cv=kf
)
print(scores.mean())
if scores.mean() > maxScore:
maxScore = scores.mean()
optimalP = i
fout = open('Answers/Boston.txt', 'w')
print(maxScore, optimalP, file=fout, end='')
fout.close()