##使用NumPy从头开始进行K最近邻分类( 三 )


将我们的实现与Sklearn的KNeighborsClassifier进行比较
from sklearn.datasets import load_iris from KNearestNeighbors import KNearestNeighbors from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier import pandas as pd dataset = load_iris() X = dataset.data y = dataset.target mu = np.mean(X, 0) sigma = np.std(X, 0) X = (X - mu ) / sigma X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=45) our_classifier = KNearestNeighbors(X_train, y_train, n_neighbors=3) sklearn_classifier = KNeighborsClassifier(n_neighbors=3).fit(X_train, y_train) our_accuracy = our_classifier.score(X_test, y_test) sklearn_accuracy = sklearn_classifier.score(X_test, y_test) pd.DataFrame([[our_accuracy, sklearn_accuracy]], ['Accuracy'], ['Our Implementation', 'Sklearn's Implementation'])
##使用NumPy从头开始进行K最近邻分类
本文插图

【##使用NumPy从头开始进行K最近邻分类】我们自己的实现和sklearn的实现的准确性看起来是基本相同的 。


推荐阅读