DistanceMetric class
This class provides a uniform interface to fast distance metric functions. The various metrics can be accessed via the get_metric class method and the metric string identifier (see below).
Available Metrics
The following lists the string metric identifiers and the associated distance metric classes:
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 two-dimensional vector spaces: Note that the haversine distance metric requires data in the form of [latitude, longitude] and both inputs and outputs are in units of radians.
identifier
class name
distance function
“haversine”
HaversineDistance
2 arcsin(sqrt(sin^2(0.5*dx) + cos(x1)cos(x2)sin^2(0.5*dy)))
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|))
Metrics intended for boolean-valued vector spaces: Any nonzero entry is evaluated to “True”. In the listings below, the following abbreviations are used:
identifier
class name
distance function
“jaccard”
JaccardDistance
NNEQ / NNZ
“matching”
MatchingDistance
NNEQ / N
“dice”
DiceDistance
NNEQ / (NTT + NNZ)
“kulsinski”
KulsinskiDistance
(NNEQ + N - NTT) / (NNEQ + N)
“rogerstanimoto”
RogersTanimotoDistance
2 * NNEQ / (N + NNEQ)
“russellrao”
RussellRaoDistance
NNZ / N
“sokalmichener”
SokalMichenerDistance
2 * NNEQ / (N + NNEQ)
“sokalsneath”
SokalSneathDistance
NNEQ / (NNEQ + 0.5 * NTT)
User-defined distance:
identifier
class name
args
“pyfunc”
PyFuncDistance
func
Here func is a function which takes two one-dimensional numpy arrays, and returns a distance. Note that in order to be used within the BallTree, the distance must be a true metric: i.e. it must satisfy the following properties
Because of the Python object overhead involved in calling the python function, this will be fairly slow, but it will have the same scaling as other distances.
dist_to_rdist
Convert the true distance to the reduced distance.
get_metric
Get the given distance metric from the string identifier.
pairwise
Compute the pairwise distances between X and Y
rdist_to_dist
Convert the Reduced distance to the true distance.
Convert the true distance to the reduced distance.
The reduced distance, defined for some metrics, is a computationally more efficient measure which preserves the rank of the true distance. For example, in the Euclidean distance metric, the reduced distance is the squared-euclidean distance.
Get the given distance metric from the string identifier.
See the docstring of DistanceMetric for a list of available metrics.
The distance metric to use
additional arguments will be passed to the requested metric
Compute the pairwise distances between X and Y
This is a convenience routine for the sake of testing. For many metrics, the utilities in scipy.spatial.distance.cdist and scipy.spatial.distance.pdist will be faster.
Array of shape (Nx, D), representing Nx points in D dimensions.
Array of shape (Ny, D), representing Ny points in D dimensions. If not specified, then Y=X.
The shape (Nx, Ny) array of pairwise distances between points in X and Y.
Convert the Reduced distance to the true distance.
The reduced distance, defined for some metrics, is a computationally more efficient measure which preserves the rank of the true distance. For example, in the Euclidean distance metric, the reduced distance is the squared-euclidean distance.


Convert the true distance to the reduced distance.
The reduced distance, defined for some metrics, is a computationally more efficient measure which preserves the rank of the true distance. For example, in the Euclidean distance metric, the reduced distance is the squared-euclidean distance.


Get the given distance metric from the string identifier.
See the docstring of DistanceMetric for a list of available metrics.
The distance metric to use
additional arguments will be passed to the requested metric


Compute the pairwise distances between X and Y
This is a convenience routine for the sake of testing. For many metrics, the utilities in scipy.spatial.distance.cdist and scipy.spatial.distance.pdist will be faster.
Array of shape (Nx, D), representing Nx points in D dimensions.
Array of shape (Ny, D), representing Ny points in D dimensions. If not specified, then Y=X.
The shape (Nx, Ny) array of pairwise distances between points in X and Y.


Convert the Reduced distance to the true distance.
The reduced distance, defined for some metrics, is a computationally more efficient measure which preserves the rank of the true distance. For example, in the Euclidean distance metric, the reduced distance is the squared-euclidean distance.


KDTree for fast generalized N-point problems
Read more in the User Guide.
n_samples is the number of points in the data set, and n_features is the dimension of the parameter space. Note: if X is a C-contiguous array of doubles then data will not be copied. Otherwise, an internal copy will be made.
Number of points at which to switch to brute-force. Changing leaf_size will not affect the results of a query, but can significantly impact the speed of a query and the memory required to store the constructed tree. The amount of memory needed to store the tree scales as approximately n_samples / leaf_size. For a specified leaf_size, a leaf node is guaranteed to satisfy leaf_size <= n_points <= 2 * leaf_size, except in the case that n_samples < leaf_size.
the distance metric to use for the tree. Default=’minkowski’ with p=2 (that is, a euclidean metric). See the documentation of the DistanceMetric class for a list of available metrics. kd_tree.valid_metrics gives a list of the metrics which are valid for KDTree.
The training data
Query for k-nearest neighbors
Pickle and Unpickle a tree. Note that the state of the tree is saved in the pickle operation: the tree needs not be rebuilt upon unpickling.
Query for neighbors within a given radius
Compute a gaussian kernel density estimate:
Compute a two-point auto-correlation function
get_arrays(self)
Get data and node arrays.
get_n_calls(self)
Get number of calls.
get_tree_stats(self)
Get tree status.
kernel_density(self, X, h[, kernel, atol, …])
Compute the kernel density estimate at points X with the given kernel, using the distance metric specified at tree creation.
query(X[, k, return_distance, dualtree, …])
query the tree for the k nearest neighbors
query_radius(X, r[, return_distance, …])
query the tree for neighbors within a radius r
reset_n_calls(self)
Reset number of calls to 0.
two_point_correlation(X, r[, dualtree])
Compute the two-point correlation function
Get data and node arrays.
Arrays for storing tree data, index, node data and node bounds.
Get number of calls.
number of distance computation calls
Get tree status.
(number of trims, number of leaves, number of splits)
Compute the kernel density estimate at points X with the given kernel, using the distance metric specified at tree creation.
An array of points to query. Last dimension should match dimension of training data.
the bandwidth of the kernel
specify the kernel to use. Options are - ‘gaussian’ - ‘tophat’ - ‘epanechnikov’ - ‘exponential’ - ‘linear’ - ‘cosine’ Default is kernel = ‘gaussian’
Specify the desired relative and absolute tolerance of the result. If the true result is K_true, then the returned result K_ret satisfies abs(K_true - K_ret) < atol + rtol * K_ret The default is zero (i.e. machine precision) for both.
If True, use a breadth-first search. If False (default) use a depth-first search. Breadth-first is generally faster for compact kernels and/or high tolerances.
Return the logarithm of the result. This can be more accurate than returning the result itself for narrow kernels.
The array of (log)-density evaluations
query the tree for the k nearest neighbors
An array of points to query
The number of nearest neighbors to return
if True, return a tuple (d, i) of distances and indices if False, return array i
if True, use the dual tree formalism for the query: a tree is built for the query points, and the pair of trees is used to efficiently search this space. This can lead to better performance as the number of points grows large.
if True, then query the nodes in a breadth-first manner. Otherwise, query the nodes in a depth-first manner.
if True, then distances and indices of each point are sorted on return, so that the first column contains the closest points. Otherwise, neighbors are returned in an arbitrary order.
Each entry gives the list of distances to the neighbors of the corresponding point.
Each entry gives the list of indices of neighbors of the corresponding point.
query the tree for neighbors within a radius r
An array of points to query
r can be a single value, or an array of values of shape x.shape[:-1] if different radii are desired for each point.
if True, return distances to neighbors of each point if False, return only neighbors Note that unlike the query() method, setting return_distance=True here adds to the computation time. Not all distances need to be calculated explicitly for return_distance=False. Results are not sorted by default: see sort_results keyword.
if True, return only the count of points within distance r if False, return the indices of all points within distance r If return_distance==True, setting count_only=True will result in an error.
if True, the distances and indices will be sorted before being returned. If False, the results will not be sorted. If return_distance == False, setting sort_results = True will result in an error.
Each entry gives the number of neighbors within a distance r of the corresponding point.
Each element is a numpy integer array listing the indices of neighbors of the corresponding point. Note that unlike the results of a k-neighbors query, the returned neighbors are not sorted by distance by default.
Each element is a numpy double array listing the distances corresponding to indices in i.
Reset number of calls to 0.
Compute the two-point correlation function
An array of points to query. Last dimension should match dimension of training data.
A one-dimensional array of distances
If True, use a dualtree algorithm. Otherwise, use a single-tree algorithm. Dual tree algorithms can have better scaling for large N.
counts[i] contains the number of pairs of points with distance less than or equal to r[i]


Get data and node arrays.
Arrays for storing tree data, index, node data and node bounds.


Get number of calls.
number of distance computation calls


Get tree status.
(number of trims, number of leaves, number of splits)


Compute the kernel density estimate at points X with the given kernel, using the distance metric specified at tree creation.
An array of points to query. Last dimension should match dimension of training data.
the bandwidth of the kernel
specify the kernel to use. Options are - ‘gaussian’ - ‘tophat’ - ‘epanechnikov’ - ‘exponential’ - ‘linear’ - ‘cosine’ Default is kernel = ‘gaussian’
Specify the desired relative and absolute tolerance of the result. If the true result is K_true, then the returned result K_ret satisfies abs(K_true - K_ret) < atol + rtol * K_ret The default is zero (i.e. machine precision) for both.
If True, use a breadth-first search. If False (default) use a depth-first search. Breadth-first is generally faster for compact kernels and/or high tolerances.
Return the logarithm of the result. This can be more accurate than returning the result itself for narrow kernels.
The array of (log)-density evaluations


query the tree for the k nearest neighbors
An array of points to query
The number of nearest neighbors to return
if True, return a tuple (d, i) of distances and indices if False, return array i
if True, use the dual tree formalism for the query: a tree is built for the query points, and the pair of trees is used to efficiently search this space. This can lead to better performance as the number of points grows large.
if True, then query the nodes in a breadth-first manner. Otherwise, query the nodes in a depth-first manner.
if True, then distances and indices of each point are sorted on return, so that the first column contains the closest points. Otherwise, neighbors are returned in an arbitrary order.
Each entry gives the list of distances to the neighbors of the corresponding point.
Each entry gives the list of indices of neighbors of the corresponding point.


query the tree for neighbors within a radius r
An array of points to query
r can be a single value, or an array of values of shape x.shape[:-1] if different radii are desired for each point.
if True, return distances to neighbors of each point if False, return only neighbors Note that unlike the query() method, setting return_distance=True here adds to the computation time. Not all distances need to be calculated explicitly for return_distance=False. Results are not sorted by default: see sort_results keyword.
if True, return only the count of points within distance r if False, return the indices of all points within distance r If return_distance==True, setting count_only=True will result in an error.
if True, the distances and indices will be sorted before being returned. If False, the results will not be sorted. If return_distance == False, setting sort_results = True will result in an error.
Each entry gives the number of neighbors within a distance r of the corresponding point.
Each element is a numpy integer array listing the indices of neighbors of the corresponding point. Note that unlike the results of a k-neighbors query, the returned neighbors are not sorted by distance by default.
Each element is a numpy double array listing the distances corresponding to indices in i.


Compute the two-point correlation function
An array of points to query. Last dimension should match dimension of training data.
A one-dimensional array of distances
If True, use a dualtree algorithm. Otherwise, use a single-tree algorithm. Dual tree algorithms can have better scaling for large N.
counts[i] contains the number of pairs of points with distance less than or equal to r[i]


Kernel Density Estimation.
Read more in the User Guide.
The bandwidth of the kernel.
The tree algorithm to use.
The kernel to use.
The distance metric to use. Note that not all metrics are valid with all algorithms. Refer to the documentation of BallTree and KDTree for a description of available algorithms. Note that the normalization of the density output is correct only for the Euclidean distance metric. Default is ‘euclidean’.
The desired absolute tolerance of the result. A larger tolerance will generally lead to faster execution.
The desired relative tolerance of the result. A larger tolerance will generally lead to faster execution.
If true (default), use a breadth-first approach to the problem. Otherwise use a depth-first approach.
Specify the leaf size of the underlying tree. See BallTree or KDTree for details.
Additional parameters to be passed to the tree for use with the metric. For more information, see the documentation of BallTree or KDTree.
The tree algorithm for fast generalized N-point problems.
See also
K-dimensional tree for fast generalized N-point problems.
Ball tree for fast generalized N-point problems.
Compute a gaussian kernel density estimate with a fixed bandwidth.
fit(X[, y, sample_weight])
Fit the Kernel Density model on the data.
get_params([deep])
Get parameters for this estimator.
sample([n_samples, random_state])
Generate random samples from the model.
score(X[, y])
Compute the total log probability density under the model.
score_samples(X)
Evaluate the log density model on the data.
set_params(**params)
Set the parameters of this estimator.
Fit the Kernel Density model on the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
List of sample weights attached to the data X.
New in version 0.20.
Returns instance of object.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Generate random samples from the model.
Currently, this is implemented only for gaussian and tophat kernels.
Number of samples to generate.
Determines random number generation used to generate random samples. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
List of samples.
Compute the total log probability density under the model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
Total log-likelihood of the data in X. This is normalized to be a probability density, so the value will be low for high-dimensional data.
Evaluate the log density model on the data.
An array of points to query. Last dimension should match dimension of training data (n_features).
The array of log(density) evaluations. These are normalized to be probability densities, so values will be low for high-dimensional data.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Kernel Density Estimation.
Read more in the User Guide.
The bandwidth of the kernel.
The tree algorithm to use.
The kernel to use.
The distance metric to use. Note that not all metrics are valid with all algorithms. Refer to the documentation of BallTree and KDTree for a description of available algorithms. Note that the normalization of the density output is correct only for the Euclidean distance metric. Default is ‘euclidean’.
The desired absolute tolerance of the result. A larger tolerance will generally lead to faster execution.
The desired relative tolerance of the result. A larger tolerance will generally lead to faster execution.
If true (default), use a breadth-first approach to the problem. Otherwise use a depth-first approach.
Specify the leaf size of the underlying tree. See BallTree or KDTree for details.
Additional parameters to be passed to the tree for use with the metric. For more information, see the documentation of BallTree or KDTree.
The tree algorithm for fast generalized N-point problems.
See also
K-dimensional tree for fast generalized N-point problems.
Ball tree for fast generalized N-point problems.
Compute a gaussian kernel density estimate with a fixed bandwidth.
fit(X[, y, sample_weight])
Fit the Kernel Density model on the data.
get_params([deep])
Get parameters for this estimator.
sample([n_samples, random_state])
Generate random samples from the model.
score(X[, y])
Compute the total log probability density under the model.
score_samples(X)
Evaluate the log density model on the data.
set_params(**params)
Set the parameters of this estimator.
Fit the Kernel Density model on the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
List of sample weights attached to the data X.
New in version 0.20.
Returns instance of object.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Generate random samples from the model.
Currently, this is implemented only for gaussian and tophat kernels.
Number of samples to generate.
Determines random number generation used to generate random samples. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
List of samples.
Compute the total log probability density under the model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
Total log-likelihood of the data in X. This is normalized to be a probability density, so the value will be low for high-dimensional data.
Evaluate the log density model on the data.
An array of points to query. Last dimension should match dimension of training data (n_features).
The array of log(density) evaluations. These are normalized to be probability densities, so values will be low for high-dimensional data.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Kernel Density Estimation
Kernel Density Estimate of Species Distributions
Simple 1D Kernel Density Estimation


Fit the Kernel Density model on the data.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
List of sample weights attached to the data X.
New in version 0.20.
Returns instance of object.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Generate random samples from the model.
Currently, this is implemented only for gaussian and tophat kernels.
Number of samples to generate.
Determines random number generation used to generate random samples. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
List of samples.


Compute the total log probability density under the model.
List of n_features-dimensional data points. Each row corresponds to a single data point.
Ignored. This parameter exists only for compatibility with Pipeline.
Total log-likelihood of the data in X. This is normalized to be a probability density, so the value will be low for high-dimensional data.


Evaluate the log density model on the data.
An array of points to query. Last dimension should match dimension of training data (n_features).
The array of log(density) evaluations. These are normalized to be probability densities, so values will be low for high-dimensional data.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Classifier implementing the k-nearest neighbors vote.
Read more in the User Guide.
Number of neighbors to use by default for kneighbors queries.
weight function used in prediction. Possible values:
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
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.
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 DistanceMetric for a list of available metrics. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
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.
Class labels known to the classifier
The distance metric used. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
False when y’s shape is (n_samples, ) or (n_samples, 1) during fit otherwise True.
See also
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/K-nearest_neighbor_algorithm
fit(X, y)
Fit the k-nearest neighbors classifier from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors 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])
Return the mean accuracy on the given test data and labels.
set_params(**params)
Set the parameters of this estimator.
Fit the k-nearest neighbors classifier from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors classifier.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the class labels for the provided data.
Test samples.
Class labels for each data sample.
Return probability estimates for the test data X.
Test samples.
of such arrays if n_outputs > 1. The class probabilities of the input samples. Classes are ordered by lexicographic order.
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Classifier implementing the k-nearest neighbors vote.
Read more in the User Guide.
Number of neighbors to use by default for kneighbors queries.
weight function used in prediction. Possible values:
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
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.
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 DistanceMetric for a list of available metrics. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
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.
Class labels known to the classifier
The distance metric used. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
False when y’s shape is (n_samples, ) or (n_samples, 1) during fit otherwise True.
See also
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/K-nearest_neighbor_algorithm
fit(X, y)
Fit the k-nearest neighbors classifier from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors 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])
Return the mean accuracy on the given test data and labels.
set_params(**params)
Set the parameters of this estimator.
Fit the k-nearest neighbors classifier from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors classifier.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the class labels for the provided data.
Test samples.
Class labels for each data sample.
Return probability estimates for the test data X.
Test samples.
of such arrays if n_outputs > 1. The class probabilities of the input samples. Classes are ordered by lexicographic order.
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Release Highlights for scikit-learn 0.24
Classifier comparison
Plot the decision boundaries of a VotingClassifier
Nearest Neighbors Classification
Caching nearest neighbors
Comparing Nearest Neighbors with and without Neighborhood Components Analysis
Dimensionality Reduction with Neighborhood Components Analysis
Digits Classification Exercise
Classification of text documents using sparse features


Fit the k-nearest neighbors classifier from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors classifier.


Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:


Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also


Predict the class labels for the provided data.
Test samples.
Class labels for each data sample.


Return probability estimates for the test data X.
Test samples.
of such arrays if n_outputs > 1. The class probabilities of the input samples. Classes are ordered by lexicographic order.


Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.


Regression based on k-nearest neighbors.
The target is predicted by local interpolation of the targets associated of the nearest neighbors in the training set.
Read more in the User Guide.
New in version 0.9.
Number of neighbors to use by default for kneighbors queries.
weight function used in prediction. Possible values:
Uniform weights are used by default.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
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.
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 DistanceMetric for a list of available metrics. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
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.
The distance metric to use. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
See also
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/K-nearest_neighbor_algorithm
fit(X, y)
Fit the k-nearest neighbors regressor from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
predict(X)
Predict the target for the provided data
score(X, y[, sample_weight])
Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params)
Set the parameters of this estimator.
Fit the k-nearest neighbors regressor from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors regressor.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the target for the provided data
Test samples.
Target values.
Return the coefficient of determination \(R^2\) of the prediction.
The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Regression based on k-nearest neighbors.
The target is predicted by local interpolation of the targets associated of the nearest neighbors in the training set.
Read more in the User Guide.
New in version 0.9.
Number of neighbors to use by default for kneighbors queries.
weight function used in prediction. Possible values:
Uniform weights are used by default.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
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.
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 DistanceMetric for a list of available metrics. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors.
Additional keyword arguments for the metric function.
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.
The distance metric to use. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
See also
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/K-nearest_neighbor_algorithm
fit(X, y)
Fit the k-nearest neighbors regressor from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
predict(X)
Predict the target for the provided data
score(X, y[, sample_weight])
Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params)
Set the parameters of this estimator.
Fit the k-nearest neighbors regressor from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors regressor.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the target for the provided data
Test samples.
Target values.
Return the coefficient of determination \(R^2\) of the prediction.
The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Face completion with a multi-output estimators
Imputing missing values with variants of IterativeImputer
Nearest Neighbors regression


Fit the k-nearest neighbors regressor from the training dataset.
Training data.
Target values.
The fitted k-nearest neighbors regressor.


Predict the target for the provided data
Test samples.
Target values.


Return the coefficient of determination \(R^2\) of the prediction.
The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.
Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
\(R^2\) of self.predict(X) wrt. y.
The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).


Transform X into a (weighted) graph of k nearest neighbors
The transformed data is a sparse graph as returned by kneighbors_graph.
Read more in the User Guide.
New in version 0.22.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
Number of neighbors for each sample in the transformed sparse graph. For compatibility reasons, as each sample is considered as its own neighbor, one extra neighbor will be computed when mode == ‘distance’. In this case, the sparse graph contains (n_neighbors + 1) neighbors.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics.
Parameter for the Minkowski metric from sklearn.metrics.pairwise.pairwise_distances. 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.
Additional keyword arguments for the metric function.
The number of parallel jobs to run for neighbors search. If -1, then the number of jobs is set to the number of CPU cores.
The distance metric used. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
fit(X[, y])
Fit the k-nearest neighbors transformer from the training dataset.
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
set_params(**params)
Set the parameters of this estimator.
transform(X)
Computes the (weighted) graph of Neighbors for points in X
Fit the k-nearest neighbors transformer from the training dataset.
Training data.
The fitted k-nearest neighbors transformer.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Training set.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Computes the (weighted) graph of Neighbors for points in X
Sample data.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.


Transform X into a (weighted) graph of k nearest neighbors
The transformed data is a sparse graph as returned by kneighbors_graph.
Read more in the User Guide.
New in version 0.22.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
Number of neighbors for each sample in the transformed sparse graph. For compatibility reasons, as each sample is considered as its own neighbor, one extra neighbor will be computed when mode == ‘distance’. In this case, the sparse graph contains (n_neighbors + 1) neighbors.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics.
Parameter for the Minkowski metric from sklearn.metrics.pairwise.pairwise_distances. 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.
Additional keyword arguments for the metric function.
The number of parallel jobs to run for neighbors search. If -1, then the number of jobs is set to the number of CPU cores.
The distance metric used. It will be same as the metric parameter or a synonym of it, e.g. ‘euclidean’ if the metric parameter set to ‘minkowski’ and p parameter set to 2.
Additional keyword arguments for the metric function. For most metrics will be same with metric_params parameter, but may also contain the p parameter value if the effective_metric_ attribute is set to ‘minkowski’.
Number of samples in the fitted data.
fit(X[, y])
Fit the k-nearest neighbors transformer from the training dataset.
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
set_params(**params)
Set the parameters of this estimator.
transform(X)
Computes the (weighted) graph of Neighbors for points in X
Fit the k-nearest neighbors transformer from the training dataset.
Training data.
The fitted k-nearest neighbors transformer.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Training set.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Computes the (weighted) graph of Neighbors for points in X
Sample data.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.
Release Highlights for scikit-learn 0.22
Caching nearest neighbors
Approximate nearest neighbors in TSNE


Fit the k-nearest neighbors transformer from the training dataset.
Training data.
The fitted k-nearest neighbors transformer.


Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Training set.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Computes the (weighted) graph of Neighbors for points in X
Sample data.
Xt[i, j] is assigned the weight of edge that connects i to j. Only the neighbors have an explicit value. The diagonal is always explicit. The matrix is of CSR format.


Computes the (weighted) graph of k-Neighbors for points in X
Read more in the User Guide.
Sample data, in the form of a numpy array or a precomputed BallTree.
Number of neighbors for each sample.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.
The distance metric used to calculate the k-Neighbors for each sample point. The DistanceMetric class gives a list of available metrics. The default distance is ‘euclidean’ (‘minkowski’ metric with the p param equal to 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.
additional keyword arguments for the metric function.
Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.
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.
Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also


Unsupervised Outlier Detection using Local Outlier Factor (LOF)
The anomaly score of each sample is called Local Outlier Factor. It measures the local deviation of density of a given sample with respect to its neighbors. It is local in that the anomaly score depends on how isolated the object is with respect to the surrounding neighborhood. More precisely, locality is given by k-nearest neighbors, whose distance is used to estimate the local density. By comparing the local density of a sample to the local densities of its neighbors, one can identify samples that have a substantially lower density than their neighbors. These are considered outliers.
New in version 0.19.
Number of neighbors to use by default for kneighbors queries. If n_neighbors is larger than the number of samples provided, all samples will be used.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
metric used for the distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a sparse matrix, in which case only “nonzero” elements may be considered neighbors.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics: https://docs.scipy.org/doc/scipy/reference/spatial.distance.html
Parameter for the Minkowski metric from sklearn.metrics.pairwise.pairwise_distances. 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.
Additional keyword arguments for the metric function.
The amount of contamination of the data set, i.e. the proportion of outliers in the data set. When fitting this is used to define the threshold on the scores of the samples.
Changed in version 0.22: The default value of contamination changed from 0.1 to 'auto'.
By default, LocalOutlierFactor is only meant to be used for outlier detection (novelty=False). Set novelty to True if you want to use LocalOutlierFactor for novelty detection. In this case be aware that that you should only use predict, decision_function and score_samples on new unseen data and not on the training set.
New in version 0.20.
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.
The opposite LOF of the training samples. The higher, the more normal. Inliers tend to have a LOF score close to 1 (negative_outlier_factor_ close to -1), while outliers tend to have a larger LOF score.
The local outlier factor (LOF) of a sample captures its supposed ‘degree of abnormality’. It is the average of the ratio of the local reachability density of a sample and those of its k-nearest neighbors.
The actual number of neighbors used for kneighbors queries.
Offset used to obtain binary labels from the raw scores. Observations having a negative_outlier_factor smaller than offset_ are detected as abnormal. The offset is set to -1.5 (inliers score around -1), except when a contamination parameter different than “auto” is provided. In that case, the offset is defined in such a way we obtain the expected number of outliers in training.
New in version 0.20.
The effective metric used for the distance computation.
The effective additional keyword arguments for the metric function.
It is the number of samples in the fitted data.
Breunig, M. M., Kriegel, H. P., Ng, R. T., & Sander, J. (2000, May). LOF: identifying density-based local outliers. In ACM sigmod record.
fit(X[, y])
Fit the local outlier factor detector from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
set_params(**params)
Set the parameters of this estimator.
Shifted opposite of the Local Outlier Factor of X.
Bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The shift offset allows a zero threshold for being an outlier. The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The shifted opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal. Negative scores represent outliers, positive scores represent inliers.
Fit the local outlier factor detector from the training dataset.
Training data.
Not used, present for API consistency by convention.
The fitted local outlier factor detector.
Fits the model to the training set X and returns the labels.
Not available for novelty detection (when novelty is set to True). Label is 1 for an inlier and -1 for an outlier according to the LOF score and the contamination parameter.
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Not used, present for API consistency by convention.
Returns -1 for anomalies/outliers and 1 for inliers.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the labels (1 inlier, -1 outlier) of X according to LOF.
Only available for novelty detection (when novelty is set to True). This method allows to generalize prediction to new observations (not in the training set).
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Returns -1 for anomalies/outliers and +1 for inliers.
Opposite of the Local Outlier Factor of X.
It is the opposite as bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point. The score_samples on training data is available by considering the the negative_outlier_factor_ attribute.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Unsupervised Outlier Detection using Local Outlier Factor (LOF)
The anomaly score of each sample is called Local Outlier Factor. It measures the local deviation of density of a given sample with respect to its neighbors. It is local in that the anomaly score depends on how isolated the object is with respect to the surrounding neighborhood. More precisely, locality is given by k-nearest neighbors, whose distance is used to estimate the local density. By comparing the local density of a sample to the local densities of its neighbors, one can identify samples that have a substantially lower density than their neighbors. These are considered outliers.
New in version 0.19.
Number of neighbors to use by default for kneighbors queries. If n_neighbors is larger than the number of samples provided, all samples will be used.
Algorithm used to compute the nearest neighbors:
Note: fitting on sparse input will override the setting of this parameter, using brute force.
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.
metric used for the distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is “precomputed”, X is assumed to be a distance matrix and must be square. X may be a sparse matrix, in which case only “nonzero” elements may be considered neighbors.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics: https://docs.scipy.org/doc/scipy/reference/spatial.distance.html
Parameter for the Minkowski metric from sklearn.metrics.pairwise.pairwise_distances. 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.
Additional keyword arguments for the metric function.
The amount of contamination of the data set, i.e. the proportion of outliers in the data set. When fitting this is used to define the threshold on the scores of the samples.
Changed in version 0.22: The default value of contamination changed from 0.1 to 'auto'.
By default, LocalOutlierFactor is only meant to be used for outlier detection (novelty=False). Set novelty to True if you want to use LocalOutlierFactor for novelty detection. In this case be aware that that you should only use predict, decision_function and score_samples on new unseen data and not on the training set.
New in version 0.20.
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.
The opposite LOF of the training samples. The higher, the more normal. Inliers tend to have a LOF score close to 1 (negative_outlier_factor_ close to -1), while outliers tend to have a larger LOF score.
The local outlier factor (LOF) of a sample captures its supposed ‘degree of abnormality’. It is the average of the ratio of the local reachability density of a sample and those of its k-nearest neighbors.
The actual number of neighbors used for kneighbors queries.
Offset used to obtain binary labels from the raw scores. Observations having a negative_outlier_factor smaller than offset_ are detected as abnormal. The offset is set to -1.5 (inliers score around -1), except when a contamination parameter different than “auto” is provided. In that case, the offset is defined in such a way we obtain the expected number of outliers in training.
New in version 0.20.
The effective metric used for the distance computation.
The effective additional keyword arguments for the metric function.
It is the number of samples in the fitted data.
Breunig, M. M., Kriegel, H. P., Ng, R. T., & Sander, J. (2000, May). LOF: identifying density-based local outliers. In ACM sigmod record.
fit(X[, y])
Fit the local outlier factor detector from the training dataset.
get_params([deep])
Get parameters for this estimator.
kneighbors([X, n_neighbors, return_distance])
Finds the K-neighbors of a point.
kneighbors_graph([X, n_neighbors, mode])
Computes the (weighted) graph of k-Neighbors for points in X
set_params(**params)
Set the parameters of this estimator.
Shifted opposite of the Local Outlier Factor of X.
Bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The shift offset allows a zero threshold for being an outlier. The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The shifted opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal. Negative scores represent outliers, positive scores represent inliers.
Fit the local outlier factor detector from the training dataset.
Training data.
Not used, present for API consistency by convention.
The fitted local outlier factor detector.
Fits the model to the training set X and returns the labels.
Not available for novelty detection (when novelty is set to True). Label is 1 for an inlier and -1 for an outlier according to the LOF score and the contamination parameter.
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Not used, present for API consistency by convention.
Returns -1 for anomalies/outliers and 1 for inliers.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:
Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also
Predict the labels (1 inlier, -1 outlier) of X according to LOF.
Only available for novelty detection (when novelty is set to True). This method allows to generalize prediction to new observations (not in the training set).
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Returns -1 for anomalies/outliers and +1 for inliers.
Opposite of the Local Outlier Factor of X.
It is the opposite as bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point. The score_samples on training data is available by considering the the negative_outlier_factor_ attribute.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Comparing anomaly detection algorithms for outlier detection on toy datasets
Outlier detection with Local Outlier Factor (LOF)
Novelty detection with Local Outlier Factor (LOF)


Shifted opposite of the Local Outlier Factor of X.
Bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The shift offset allows a zero threshold for being an outlier. The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The shifted opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal. Negative scores represent outliers, positive scores represent inliers.


Fit the local outlier factor detector from the training dataset.
Training data.
Not used, present for API consistency by convention.
The fitted local outlier factor detector.


Fits the model to the training set X and returns the labels.
Not available for novelty detection (when novelty is set to True). Label is 1 for an inlier and -1 for an outlier according to the LOF score and the contamination parameter.
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Not used, present for API consistency by convention.
Returns -1 for anomalies/outliers and 1 for inliers.


Finds the K-neighbors of a point.
Returns indices of and distances to the neighbors of each point.
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
Number of neighbors required for each sample. The default is the value passed to the constructor.
Whether or not to return the distances.
Array representing the lengths to points, only present if return_distance=True
Indices of the nearest points in the population matrix.
In the following example, we construct a NearestNeighbors class from an array representing our data set and ask who’s the closest point to [1,1,1]
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:


Computes the (weighted) graph of k-Neighbors for points in X
The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor. For metric='precomputed' the shape should be (n_queries, n_indexed). Otherwise the shape should be (n_queries, n_features).
Number of neighbors for each sample. The default is the value passed to the constructor.
Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are Euclidean distance between points.
n_samples_fit is the number of samples in the fitted data A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.
See also


Predict the labels (1 inlier, -1 outlier) of X according to LOF.
Only available for novelty detection (when novelty is set to True). This method allows to generalize prediction to new observations (not in the training set).
The query sample or samples to compute the Local Outlier Factor w.r.t. to the training samples.
Returns -1 for anomalies/outliers and +1 for inliers.


Opposite of the Local Outlier Factor of X.
It is the opposite as bigger is better, i.e. large values correspond to inliers.
Only available for novelty detection (when novelty is set to True). The argument X is supposed to contain new data: if X contains a point from training, it considers the later in its own neighborhood. Also, the samples in X are not considered in the neighborhood of any point. The score_samples on training data is available by considering the the negative_outlier_factor_ attribute.
The query sample or samples to compute the Local Outlier Factor w.r.t. the training samples.
The opposite of the Local Outlier Factor of each input samples. The lower, the more abnormal.


Nearest centroid classifier.
Each class is represented by its centroid, with test samples classified to the class with the nearest centroid.
Read more in the User Guide.
The metric to use when calculating distance between instances in a feature array. If metric is a string or callable, it must be one of the options allowed by metrics.pairwise.pairwise_distances for its metric parameter. The centroids for the samples corresponding to each class is the point from which the sum of the distances (according to the metric) of all samples that belong to that particular class are minimized. If the “manhattan” metric is provided, this centroid is the median and for all other metrics, the centroid is now set to be the mean.
Changed in version 0.19: metric='precomputed' was deprecated and now raises an error
Threshold for shrinking centroids to remove features.
Centroid of each class.
The unique classes labels.
See also
Nearest neighbors classifier.
When used for text classification with tf-idf vectors, this classifier is also known as the Rocchio classifier.
Tibshirani, R., Hastie, T., Narasimhan, B., & Chu, G. (2002). Diagnosis of multiple cancer types by shrunken centroids of gene expression. Proceedings of the National Academy of Sciences of the United States of America, 99(10), 6567-6572. The National Academy of Sciences.
fit(X, y)
Fit the NearestCentroid model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on an array of test vectors X.
score(X, y[, sample_weight])
Return the mean accuracy on the given test data and labels.
set_params(**params)
Set the parameters of this estimator.
Fit the NearestCentroid model according to the given training data.
Training vector, where n_samples is the number of samples and n_features is the number of features. Note that centroid shrinking cannot be used with sparse matrices.
Target values (integers)
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on an array of test vectors X.
The predicted class C for each sample in X is returned.
If the metric constructor parameter is “precomputed”, X is assumed to be the distance matrix between the data to be predicted and self.centroids_.
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Nearest centroid classifier.
Each class is represented by its centroid, with test samples classified to the class with the nearest centroid.
Read more in the User Guide.
The metric to use when calculating distance between instances in a feature array. If metric is a string or callable, it must be one of the options allowed by metrics.pairwise.pairwise_distances for its metric parameter. The centroids for the samples corresponding to each class is the point from which the sum of the distances (according to the metric) of all samples that belong to that particular class are minimized. If the “manhattan” metric is provided, this centroid is the median and for all other metrics, the centroid is now set to be the mean.
Changed in version 0.19: metric='precomputed' was deprecated and now raises an error
Threshold for shrinking centroids to remove features.
Centroid of each class.
The unique classes labels.
See also
Nearest neighbors classifier.
When used for text classification with tf-idf vectors, this classifier is also known as the Rocchio classifier.
Tibshirani, R., Hastie, T., Narasimhan, B., & Chu, G. (2002). Diagnosis of multiple cancer types by shrunken centroids of gene expression. Proceedings of the National Academy of Sciences of the United States of America, 99(10), 6567-6572. The National Academy of Sciences.
fit(X, y)
Fit the NearestCentroid model according to the given training data.
get_params([deep])
Get parameters for this estimator.
predict(X)
Perform classification on an array of test vectors X.
score(X, y[, sample_weight])
Return the mean accuracy on the given test data and labels.
set_params(**params)
Set the parameters of this estimator.
Fit the NearestCentroid model according to the given training data.
Training vector, where n_samples is the number of samples and n_features is the number of features. Note that centroid shrinking cannot be used with sparse matrices.
Target values (integers)
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Perform classification on an array of test vectors X.
The predicted class C for each sample in X is returned.
If the metric constructor parameter is “precomputed”, X is assumed to be the distance matrix between the data to be predicted and self.centroids_.
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Nearest Centroid Classification
Classification of text documents using sparse features


Fit the NearestCentroid model according to the given training data.
Training vector, where n_samples is the number of samples and n_features is the number of features. Note that centroid shrinking cannot be used with sparse matrices.
Target values (integers)


Perform classification on an array of test vectors X.
The predicted class C for each sample in X is returned.
If the metric constructor parameter is “precomputed”, X is assumed to be the distance matrix between the data to be predicted and self.centroids_.


Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
Test samples.
True labels for X.
Sample weights.
Mean accuracy of self.predict(X) wrt. y.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.