def calculate(X, y):
best_p, best_score = 0, -float('inf')
kf = KFold(len(y), n_folds=5, shuffle=True, random_state=42)
for p in numpy.linspace(1, 10, num=200):
knr = KNeighborsRegressor(n_neighbors=5, weights='distance', p=p)
score = max(cross_val_score(knr, X, y, cv=kf, scoring='mean_squared_error'))
if score > best_score:
best_score = score
best_p = p
return best_p, best_score
评论列表
文章目录