zoukankan      html  css  js  c++  java
  • 寻找超参数

    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)
  • 相关阅读:
    Python-迭代器
    Python-if
    Python-赋值
    Python-基础
    Python-元组(tuple),文件
    Python-正则表达式
    Python-字典
    Python-列表
    C结构体之位域(位段)
    SignalTap II应用小实例之触发位置
  • 原文地址:https://www.cnblogs.com/Erick-L/p/9009449.html
Copyright © 2011-2022 走看看