KNeighborsClassifier#

class cuml.neighbors.KNeighborsClassifier(
*,
n_neighbors=5,
algorithm='auto',
metric='euclidean',
weights='uniform',
p=2,
algo_params=None,
metric_params=None,
n_jobs=None,
verbose=False,
output_type=None,
)#

K-Nearest Neighbors Classifier is an instance-based learning technique, that keeps training samples around for prediction, rather than trying to learn a generalizable set of model parameters.

Parameters:
n_neighborsint (default=5)

Default number of neighbors to query

algorithmstring (default=’auto’)

The query algorithm to use. Currently, only ‘brute’ is supported.

metricstring (default=’euclidean’).

Distance metric to use.

weights{‘uniform’, ‘distance’} or callable, default=’uniform’

Weight function used in prediction. Possible values:

  • ‘uniform’ : uniform weights. All points in each neighborhood are weighted equally.

  • ‘distance’ : weight points by the inverse of their distance. In this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.

  • [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

pfloat (default=2)

Parameter for the Minkowski metric. When p = 1, this is equivalent to manhattan distance (l1), and euclidean distance (l2) for p = 2. For arbitrary p, minkowski distance (lp) is used.

algo_paramsdict, optional (default=None)

Used to configure the nearest neighbor algorithm to be used. If set to None, parameters will be generated automatically. Parameters for algorithm 'brute' when inputs are sparse:

  • batch_size_index : (int) number of rows in each batch of index array

  • batch_size_query : (int) number of rows in each batch of query array

Parameters for algorithm 'ivfflat':

  • nlist: (int) number of cells to partition dataset into

  • nprobe: (int) at query time, number of cells used for search

Parameters for algorithm 'ivfpq':

  • nlist: (int) number of cells to partition dataset into

  • nprobe: (int) at query time, number of cells used for search

  • M: (int) number of subquantizers

  • n_bits: (int) bits allocated per subquantizer

  • usePrecomputedTables : (bool) whether to use precomputed tables

metric_paramsdict, optional (default = None)

Additional keyword arguments for the metric function.

n_jobsint (default = None)

Ignored, here for scikit-learn API compatibility.

verboseint or boolean, default=False

Sets logging level. It must be one of cuml.common.logger.level_*. See Verbosity Levels for more info.

output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None

Return results and set estimator attributes to the indicated output type. If None, the output type set at the module level (cuml.global_settings.output_type) will be used. See Output Data Type Configuration for more info.

Attributes:
outputs_2d_

KNeighborsClassifier.outputs_2d_(self)

Methods

fit(self, X, y, *[, convert_dtype])

Fit a GPU index for k-nearest neighbors classifier model.

predict(self, X, *[, convert_dtype])

Use the trained k-nearest neighbors classifier to predict the labels for X

predict_proba(self, X, *[, convert_dtype])

Use the trained k-nearest neighbors classifier to predict the label probabilities for X

Notes

For additional docs, see scikitlearn’s KNeighborsClassifier.

Examples

>>> from cuml.neighbors import KNeighborsClassifier
>>> from cuml.datasets import make_blobs
>>> from cuml.model_selection import train_test_split

>>> X, y = make_blobs(n_samples=100, centers=5,
...                   n_features=10, random_state=5)
>>> X_train, X_test, y_train, y_test = train_test_split(
...     X, y, train_size=0.80, random_state=5)

>>> knn = KNeighborsClassifier(n_neighbors=10)

>>> knn.fit(X_train, y_train)
KNeighborsClassifier(n_neighbors=10)
>>> knn.predict(X_test)
array([1., 2., 2., 3., 4., 2., 4., 4., 2., 3., 1., 4., 3., 1., 3., 4., 3., # noqa: E501
    4., 1., 3.], dtype=float32)
fit(
self,
X,
y,
*,
convert_dtype='deprecated',
) 'KNeighborsClassifier'[source]#

Fit a GPU index for k-nearest neighbors classifier model.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yarray-like (device or host) shape = (n_samples, 1)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

convert_dtypebool, optional (default = ‘deprecated’)

Deprecated since version 26.08: convert_dtype was deprecated in version 26.08 and will be removed in version 26.10. cuML only copies input arrays when necessary (e.g. to unify dtypes), there is no reason to provide this keyword going forward.

property outputs_2d_#

Whether the output is 2d

predict(self, X, *, convert_dtype='deprecated')[source]#

Use the trained k-nearest neighbors classifier to predict the labels for X

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

convert_dtypebool, optional (default = ‘deprecated’)

Deprecated since version 26.08: convert_dtype was deprecated in version 26.08 and will be removed in version 26.10. cuML only copies input arrays when necessary (e.g. to unify dtypes), there is no reason to provide this keyword going forward.

Returns:
X_newcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, 1)

Labels predicted

For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.

predict_proba(
self,
X,
*,
convert_dtype='deprecated',
)[source]#

Use the trained k-nearest neighbors classifier to predict the label probabilities for X

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

convert_dtypebool, optional (default = ‘deprecated’)

Deprecated since version 26.08: convert_dtype was deprecated in version 26.08 and will be removed in version 26.10. cuML only copies input arrays when necessary (e.g. to unify dtypes), there is no reason to provide this keyword going forward.

Returns:
X_newcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, 1)

Labels probabilities

For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.