lang_li_bai_sha
独予幽寂
采纳率20%
2021-05-04 17:59

我使用python实现KNN,不知道为啥运行不出结果,也没有报错

import numpy as np
import pandas as pd
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report

def label(s):
    it={b'0':0, b'1':1 }
    return it[s]
#1.读取数据集  
path='d://SVM.data'
data=np.loadtxt(path, dtype=float, delimiter=',', converters={2:label})

#2.划分数据与标签  
x,y=np.split(data,indices_or_sections=(2,),axis=1) #x为数据,y为标签  
x=x[:,0:2]
train_X,test_X,train_Y,test_Y =train_test_split(x,y, random_state=1, train_size=0.6,test_size=0.4) 

def knn_classifier(train_X, train_Y, test_X, test_Y, title):
    print("KNN分类器", title)
    knn = KNeighborsClassifier(algorithm='auto', leaf_size=10, metric='minkowski',
                               metric_params=None, n_jobs=1, n_neighbors=2, p=2,
                               weights='uniform')  #应该就是这一句话的问题
    knn.fit(train_X, train_Y) # 加载数据集
    predict_Y = knn.predict(test_X)
    print("训练准确率为{:.3f}%".format(accuracy_score(test_Y, predict_Y) * 100))
    print(classification_report(test_Y, predict_Y))
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_34124780 qq_34124780 2天前

    看你的数据量,数据量大的话很慢的,等一等看下?

    点赞 评论 复制链接分享

相关推荐