寻找超参数详解编程语言

best_score = 0.0 
best_k = -1 
for k in range(1,11): 
    knn_clf = KNeighborsClassifier(n_neighbors=k) 
    knn_clf.fit(X_train,y_train) 
    score = knn_clf.score(X_test,y_test) 
    if score > best_score: 
        best_k = k 
        best_score = score 
         
print('best_k =>',best_k) 
print('best_score =>',best_score)

 

是否考虑距离这个参数

best_method='' 
best_score = 0.0 
best_k = -1 
for method in ['uniform','distance']: 
    for k in range(1,11): 
        knn_clf = KNeighborsClassifier(n_neighbors=k,weights = method) 
        knn_clf.fit(X_train,y_train) 
        score = knn_clf.score(X_test,y_test) 
        if score > best_score: 
            best_k = k 
            best_score = score 
            best_method=method 
 
print('best_method =>',best_method) 
print('best_k =>',best_k) 
print('best_score =>',best_score)

 #欧拉距离平方,1次方为曼哈顿距离,3次及以上为明可夫斯基距离

best_p=-1 
best_score = 0.0 
best_k = -1 
for k in range(1,11): 
    for p in range(1,5): 
        knn_clf = KNeighborsClassifier(n_neighbors=k,weights = 'distance',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_score = score 
            best_p=p 
             
 
print('best_p =>',best_p) 
print('best_k =>',best_k) 
print('best_score =>',best_score)

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16216.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论