import pandas as pd import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier iris = load_iris() X = iris.data y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) knn = KNeighborsClassifier(n_neighbors=3) knn.fit(X_train, y_train) print("预测结果:", knn.predict(X_test)) print("准确率:", knn.score(X_test, y_test))