機械学習—python+sklearn実現KNN&KDツリーアルゴリズム
python+sklearn KNNおよびKDツリーアルゴリズムの実装
from sklearn import datasets#
from sklearn.neighbors import KNeighborsClassifier# sklearn.neighbors KNN
import numpy as np
from sklearn.neighbors import KDTree# KD
np.random.seed(0)# , ,
iris = datasets.load_iris()# ,iris , ,
iris_x = iris.data# 150*4 , 150 , 4 、
iris_y = iris.target# 150 ,
indices = np.random.permutation(len(iris_x)) # permutation (150), 0-149 , , ,
iris_x_train = iris_x[indices[:-10]]# 140
iris_y_train = iris_y[indices[:-10]]# 140
iris_x_test = iris_x[indices[-10:]]# 10
iris_y_test = iris_y[indices[-10:]]# 10
knn = KNeighborsClassifier()# knn
knn.fit(iris_x_train, iris_y_train)# , :
iris_y_predict = knn.predict(iris_x_test)# , :
probility = knn.predict_proba(iris_x_test)#
neighborpoint = knn.kneighbors(iris_x_test[0].reshape(1,-1),n_neighbors=1,return_distance=True)# 5 ,
score = knn.score(iris_x_test, iris_y_test, sample_weight=None)# ,
tree = KDTree(iris_x_train)
# ind: 3
# dist: 3
X = iris_x_train[0].reshape(1,-1)
dist, ind = tree.query(X, k=3)
print ('ind:',ind)
print ('dist:',dist)
# np.random.seed(0)
# X = np.array([(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2)])
# tree = KDTree(X, leaf_size=2)