slearnのKNNドキュメントにおけるコードの実例となるコメント


リンクは:http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html#sklearn.neighbors.NearestNeighbors.radius_neighbors_graph
Example 1.py
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np 
from sklearn.neighbors import NearestNeighbors
samples=[[0,0,2],[1,0,0],[0,0,1]]

#--------------------------------------------------
neigh=NearestNeighbors(2,0.4)
neigh.fit(samples)#     ,Samples      
print neigh
#     :
#NearestNeighbors(algorithm='auto', leaf_size=30, metric='minkowski', metric_params=None, n_jobs=1, n_neighbors=2, p=2, radius=0.4)
print"--------------------------------------------------"
neigh.kneighbors([[0,0,1.3]],2,return_distance=False)
print"--------------------------------------------------"
nbrs = neigh.radius_neighbors([[0, 0, 1.3]], 0.4, return_distance=False)
print nbrs#      (0,0,1.3)     samples     2  ( 0    )
print np.asarray(nbrs[0][0])

#  arrary  ,asarrary        
#    :https://blog.csdn.net/gobsd/article/details/56485177
Example 2.py
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(n_neighbors=1)
neigh.fit(samples) #          ,                (1,1,1)    

print(neigh.kneighbors([[1., 1., 1.]])) 
#(array([[ 0.5]]), array([[2]], dtype=int64))
#        ,      0.5, (1,1,1)           2 (  0     )
#     (1,1,1)          (1,1,0.5)
print"---------------------------------------------------------"
X = [[0., 1., 0.], [1., 0., 1.]]
print neigh.kneighbors(X, return_distance=False) 
#   [[1] [2]]
# (0,1,0)           1   (  0     )
# (1,0,1)           2   (  0     )
Example 3.py
#-*- encoding:Utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import numpy as np
samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(radius=2.6)
neigh.fit(samples) 
rng = neigh.radius_neighbors([[1., 1., 1.]])

print rng
print"rng[0][0]=",rng[0][0]#     [1.5 0.5],  (0-1)²+(0.5-1)²+(0-1)²=2.25,      1.5
print"rng[0][0]=",rng[1][0]#     [1,2]  , (1,1,1)   1.5      ,    1    2  

print(np.asarray(rng[0][0])) 
print(np.asarray(rng[1][0])) 

# The first array returned contains the distances to all points which are closer than 1.6, 
# while the second array returned contains their indices. 
# In general, multiple points can be queried at the same time.

import numpy
print"-------------------          --------------------------------------"
def calEuclideanDistance(vec1,vec2):  
    print"vec1-vec2=",vec1-vec2
    print"numpy.square(vec1-vec2)=",sum(numpy.square(vec1 - vec2))
    dist = numpy.sqrt(numpy.sum(numpy.square(vec1 - vec2)))  
    return dist  
v1 = [0,0.5,0]  
v2 = [1,1,1]  
v1 = numpy.array(v1)  
v2 = numpy.array(v2)  
print calEuclideanDistance(v1,v2)  


Example 4.py
#-*- encoding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

print"-------------------------------------------"
X = [[0], [3], [1]]#    3  
print"X=",X
from sklearn.neighbors import NearestNeighbors
neigh = NearestNeighbors(n_neighbors=2,radius=1)
neigh.fit(X) 

print"-------------------------------------------"
A = neigh.kneighbors_graph(X)
print A.toarray()#            ,      k    ,          1,    0
#      ,           k    
# [[1. 0. 1.]
#  [0. 1. 1.]
#  [1. 0. 1.]]

print"-------------------------------------------"
A = neigh.kneighbors_graph(X,mode='distance')#  mode distance ,     bool          
print A.toarray()
# [[0. 0. 1.]
#  [0. 0. 2.]
#  [1. 0. 0.]]
#                        。
#             K=1     ,       “  1”             




#    :
#http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html#sklearn.neighbors.NearestNeighbors.radius_neighbors_graph
#         
predcatをもう一つ追加しますprobe関数の使い方:
#-*- encoding:utf-8 -*-
import sys
reload(sys)
#         
sys.setdefaultencoding('utf-8')
X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
from sklearn.neighbors import KNeighborsClassifier
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y) 

print(neigh.predict([[1.1]]))
print(neigh.predict_proba([[0]]))#      :0      0   ,0      1   
print(neigh.predict_proba([[3]]))#      :3      0   ,3      1   

注意:上記の例は簡単のために一次元データです。