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))