%%time
best_method = ""
best_p = -1
best_score = 0.0
best_k = -1
for method in ["uniform","distance"]:
for k in range(1,10):
for p in range(1,6):
knn_clf = KNeighborsClassifier(n_neighbors = k,weights = method,p=p)
knn_clf.fit(X_train,y_train)
score = knn_clf.score(X_test,y_test)
if score > best_score:
best_k = k
best_method = method
best_p = p
best_score = score
print("best_score = ",best_method)
print("best_p = ",best_p)
print("best_score = ",best_score)
print("best_k = ",best_k)
best_score = uniform
best_p = 2
best_score = 0.9916666666666667
best_k = 4
Wall time: 36.7 s
best_score = distance
best_p = 2
best_score = 0.9888888888888889
best_k = 3
Wall time: 17.4 s
既然p是在weight为distance时才有存在的意义
那2这个结果的理解是不是可以看出,相对而言,选择对的weight比选择对的p更加重要,weight更加超呢??