Metrics intended for real-valued vector spaces:

 identifier class name args distance function “euclidean” EuclideanDistance sqrt(sum((x - y)^2)) “manhattan” ManhattanDistance sum(|x - y|) “chebyshev” ChebyshevDistance max(|x - y|) “minkowski” MinkowskiDistance p sum(|x - y|^p)^(1/p) “wminkowski” WMinkowskiDistance p, w sum(|w * (x - y)|^p)^(1/p) “seuclidean” SEuclideanDistance V sqrt(sum((x - y)^2 / V)) “mahalanobis” MahalanobisDistance V or VI sqrt((x - y)' V^-1 (x - y))

Metrics intended for integer-valued vector spaces: Though intended for integer-valued vectors, these are also valid metrics in the case of real-valued vectors.

 identifier class name distance function “hamming” HammingDistance N_unequal(x, y) / N_tot “canberra” CanberraDistance sum(|x - y| / (|x| + |y|)) “braycurtis” BrayCurtisDistance sum(|x - y|) / (sum(|x|) + sum(|y|))

sklearn中的KNeighborsClassifier的weight参数有以下3个取值.

• ‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.
• ‘distance’ : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.
• [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

uniform 代表等权重. sklean中默认取值是uniform。

distance代表用距离的倒数作为权重.

callable代表用户自定义函数.

class sklearn.neighbors.KNeighborsClassifier(n_neighbors=5weights=’uniform’algorithm=’auto’leaf_size=30p=2metric=’minkowski’metric_params=Nonen_jobs=None**kwargs

n_neighbors int, optional (default = 5)

Number of neighbors to use by default for kneighbors queries.

weights str or callable, optional (default = ‘uniform’)

weight function used in prediction. Possible values:

• ‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.
• ‘distance’ : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.
• [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

algorithm {‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional

Algorithm used to compute the nearest neighbors:

Note: fitting on sparse input will override the setting of this parameter, using brute force.

leaf_size int, optional (default = 30)

Leaf size passed to BallTree or KDTree. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

p integer, optional (default = 2)

Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

metric string or callable, default ‘minkowski’

the distance metric to use for the tree. The default metric is minkowski, and with p=2 is equivalent to the standard Euclidean metric. See the documentation of the DistanceMetric class for a list of available metrics.

metric_params dict, optional (default = None)

Additional keyword arguments for the metric function.

n_jobs int or None, optional (default=None)

The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details. Doesn’t affect fit method.

>>> 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]]

$$x_{scale} = \frac {x - x_{min}} {x_{max} - x_{min}}$$

$$x_{scale} = \frac {x - x_{mean}} S$$

Importance of Distance Metrics in Machine Learning Modelling

K-近邻算法（KNN）

KNN算法

（数据科学学习手札29）KNN分类的原理详解&Python与R实现

sklearn.neighbors 最近邻