from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
iris_dataset = load_iris()
print(iris_dataset.keys())
print(iris_dataset["data"])
X_train, X_test, y_train, y_test = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state=0)
print(X_train.shape)
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
x_new = np.array([[5, 2.9, 1, 0.2]])
prediction = knn.predict(x_new)
print(prediction)
y_pred = knn.predict(X_test)
print(np.mean(y_pred == y_test))
用Pyhon sklearn 对鸢尾花的分类和预测(KNN)
最新推荐文章于 2024-08-31 15:58:05 发布