class sklearn.neighbors.KNeighborsClassifier(n_neighbors=5, weights=’uniform’, algorithm=’auto’, leaf_size=30, p=2, metric=’minkowski’, metric_params=None, n_jobs=None, **kwargs)
[source]
Classifier implementing the knearest neighbors vote.
Read more in the User Guide.
Parameters: 


See Nearest Neighbors in the online documentation for a discussion of the choice of algorithm
and leaf_size
.
Warning
Regarding the Nearest Neighbors algorithms, if it is found that two neighbors, neighbor k+1
and k
, have identical distances but different labels, the results will depend on the ordering of the training data.
https://en.wikipedia.org/wiki/Knearest_neighbor_algorithm
>>> X = [[0], [1], [2], [3]] >>> y = [0, 0, 1, 1] >>> from sklearn.neighbors import KNeighborsClassifier >>> neigh = KNeighborsClassifier(n_neighbors=3) >>> neigh.fit(X, y) KNeighborsClassifier(...) >>> print(neigh.predict([[1.1]])) [0] >>> print(neigh.predict_proba([[0.9]])) [[0.66666667 0.33333333]]
fit (X, y)  Fit the model using X as training data and y as target values 
get_params ([deep])  Get parameters for this estimator. 
kneighbors ([X, n_neighbors, return_distance])  Finds the Kneighbors of a point. 
kneighbors_graph ([X, n_neighbors, mode])  Computes the (weighted) graph of kNeighbors for points in X 
predict (X)  Predict the class labels for the provided data 
predict_proba (X)  Return probability estimates for the test data X. 
score (X, y[, sample_weight])  Returns the mean accuracy on the given test data and labels. 
set_params (**params)  Set the parameters of this estimator. 
__init__(n_neighbors=5, weights=’uniform’, algorithm=’auto’, leaf_size=30, p=2, metric=’minkowski’, metric_params=None, n_jobs=None, **kwargs)
[source]
fit(X, y)
[source]
Fit the model using X as training data and y as target values
Parameters: 


get_params(deep=True)
[source]
Get parameters for this estimator.
Parameters: 


Returns: 

kneighbors(X=None, n_neighbors=None, return_distance=True)
[source]
Finds the Kneighbors of a point. Returns indices of and distances to the neighbors of each point.
Parameters: 


Returns: 

In the following example, we construct a NeighborsClassifier class from an array representing our data set and ask who’s the closest point to [1,1,1]
>>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(n_neighbors=1) >>> neigh.fit(samples) NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> print(neigh.kneighbors([[1., 1., 1.]])) (array([[0.5]]), array([[2]]))
As you can see, it returns [[0.5]], and [[2]], which means that the element is at distance 0.5 and is the third element of samples (indexes start at 0). You can also query for multiple points:
>>> X = [[0., 1., 0.], [1., 0., 1.]] >>> neigh.kneighbors(X, return_distance=False) array([[1], [2]]...)
kneighbors_graph(X=None, n_neighbors=None, mode=’connectivity’)
[source]
Computes the (weighted) graph of kNeighbors for points in X
Parameters: 


Returns: 

>>> X = [[0], [3], [1]] >>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(n_neighbors=2) >>> neigh.fit(X) NearestNeighbors(algorithm='auto', leaf_size=30, ...) >>> A = neigh.kneighbors_graph(X) >>> A.toarray() array([[1., 0., 1.], [0., 1., 1.], [1., 0., 1.]])
predict(X)
[source]
Predict the class labels for the provided data
Parameters: 


Returns: 

predict_proba(X)
[source]
Return probability estimates for the test data X.
Parameters: 


Returns: 

score(X, y, sample_weight=None)
[source]
Returns the mean accuracy on the given test data and labels.
In multilabel classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Parameters: 


Returns: 

set_params(**params)
[source]
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter>
so that it’s possible to update each component of a nested object.
Returns: 


sklearn.neighbors.KNeighborsClassifier
© 2007–2018 The scikitlearn developers
Licensed under the 3clause BSD License.
http://scikitlearn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html