def calculate(X, y):
kf = KFold(len(data), n_folds=5, shuffle=True, random_state=42)
best_k, best_score = 0, 0
for k in xrange(1, 51):
knn = KNeighborsClassifier(n_neighbors=k)
score = cross_val_score(knn, X, y, cv=kf, scoring='accuracy').mean()
if score > best_score:
best_score = score
best_k = k
return best_k, best_score
评论列表
文章目录