from sklearn.neighbors import KNeighbors Classifier # 导入KNN模型 K = 5 # 设定初始K值为5 KNN = KNeighbors Classifier(n_neighbors = K) # KNN模型 KNN.fit(X_train, y_train) # 拟合KNN模型 y_pred = KNN.predict(X_test) # 预测心脏病结果 from sklearn.metrics import (f1_score, confusion_matrix) # 导入评估指标 print("{}NN预测准确率: {:.2f}%".format(K, KNN.score(X_test, y_test)*100)) print("{}NN预测F1分数: {:.2f}%".format(K, f1_score(y_test, y_pred)*100)) print('KNN混淆矩阵:\n', confusion_matrix(y_pred, y_test))