打印最准确分数对应的 k 值,而不是打印分数
Print k value which corresponds to the most accurate score, instead of printing the scores
如您所见,它当前正在打印分数。我如何简单地打印对应于最高分的 k 值?
k_range = range(1, 26)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test, y_pred))
print(scores)
print( k_range[ scores.index( max(scores) ) ] )
如您所见,它当前正在打印分数。我如何简单地打印对应于最高分的 k 值?
k_range = range(1, 26)
scores = []
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores.append(metrics.accuracy_score(y_test, y_pred))
print(scores)
print( k_range[ scores.index( max(scores) ) ] )