# KNeighborsRegressor

### *class* cuml.neighbors.KNeighborsRegressor(, 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 Regressor is an instance-based learning technique,
that keeps training samples around for prediction, rather than trying
to learn a generalizable set of model parameters.

The K-Nearest Neighbors Regressor will compute the average of the
labels for the k closest neighbors and use it as the label.

* **Parameters:**
  **n_neighbors**
  : Default number of neighbors to query

  **algorithm**
  : The query algorithm to use. Valid options are:
    - `'auto'`: to automatically select brute-force or
      random ball cover based on data shape and metric
    - `'rbc'`: for the random ball algorithm, which partitions
      the data space and uses the triangle inequality to lower the
      number of potential distances. Currently, this algorithm
      supports 2d Euclidean and Haversine.
    - `'brute'`: for brute-force, slow but produces exact results
    - `'ivfflat'`: for inverted file, divide the dataset in partitions
      and perform search on relevant partitions only
    - `'ivfpq'`: for inverted file and product quantization,
      same as inverted list, in addition the vectors are broken
      in n_features/M sub-vectors that will be encoded thanks
      to intermediary k-means clusterings. This encoding provide
      partial information allowing faster distances calculations

  **metric**
  : Distance metric to use.

  **weights**
  : 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.

  **p**
  : 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_params**
  : 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
    <br/>
    Parameters for algorithm `'ivfflat'`:
    > - nlist: (int) number of cells to partition dataset into
    > - nprobe: (int) at query time, number of cells used for search
    <br/>
    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, from 4 to 8
    > - usePrecomputedTables : (bool) whether to use precomputed tables
    <br/>
    With the default `nlist=8`, IVF-PQ requires at least
    `2**(n_bits + 1)` rows in `X` (512 for the default `n_bits=8`).
    Reducing `n_bits` reduces this minimum.

  **metric_params**
  : Additional keyword arguments for the metric function.

  **n_jobs**
  : Ignored, here for scikit-learn API compatibility.

  **verbose**
  : Sets logging level. It must be one of `cuml.common.logger.level_*`.
    See [Verbosity Levels](../../cuml/index.html.md#verbosity-levels) for more info.

  **output_type**
  : 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](../../cuml/index.html.md#output-data-type-configuration) for more info.

### Methods

| [`fit`](#cuml.neighbors.KNeighborsRegressor.fit)(self, X, y)   | Fit a GPU index for k-nearest neighbors regression model.                        |
|--------------------------------------------------------------------|----------------------------------------------------------------------------------|
| [`predict`](#cuml.neighbors.KNeighborsRegressor.predict)(self, X)  | Use the trained k-nearest neighbors regression model to predict the labels for X |

### Notes

For additional docs, see [scikitlearn’s KNeighborsRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsRegressor.html).

### Examples

```python
>>> from cuml.neighbors import KNeighborsRegressor
>>> from cuml.datasets import make_regression
>>> from cuml.model_selection import train_test_split

>>> X, y = make_regression(n_samples=100, 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 = KNeighborsRegressor(n_neighbors=10)
>>> knn.fit(X_train, y_train)
KNeighborsRegressor(n_neighbors=10)
>>> knn.predict(X_test)
array([ 14.770798  ,  51.8834    ,  66.15657   ,  46.978275  ,
    21.589611  , -14.519918  , -60.25534   , -20.856869  ,
    29.869623  , -34.83317   ,   0.45447388, 120.39675   ,
    109.94834   ,  63.57794   , -17.956171  ,  78.77663   ,
    30.412262  ,  32.575233  ,  74.72834   , 122.276855  ],
dtype=float32)
```

<!-- !! processed by numpydoc !! -->

#### as_sklearn()[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L206)

Convert this estimator into an equivalent scikit-learn (or scikit-learn
extension) estimator.

* **Returns:**
  sklearn.base.BaseEstimator
  : A scikit-learn compatible estimator instance that mirrors the trained
    state of the current estimator.

<!-- !! processed by numpydoc !! -->

#### fit(self, X, y) → 'KNeighborsRegressor'[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kneighbors_regressor.pyx#L233)

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

* **Parameters:**
  **X**
  : 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.

  **y**
  : 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.

<!-- !! processed by numpydoc !! -->

#### *classmethod* from_sklearn(model)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L222)

Create a cuml estimator from a scikit-learn estimator.

* **Parameters:**
  **model**
  : A compatible scikit-learn (or scikit-learn extension) estimator.
* **Returns:**
  cls
  : A new instance of this cuml estimator class that mirrors the
    state of the input estimator.

### Notes

`output_type` of the estimator is set to “numpy” by default, as these
cannot be inferred from training arguments. If something different is
required, then please use cuml’s output_type configuration utilities.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L191)

Returns a dict of all params owned by this class. If the child class
has appropriately overridden the `_get_param_names` method and does not
need anything other than what is there in this method, then it doesn’t
have to override this method

<!-- !! processed by numpydoc !! -->

#### kneighbors(self, X=None, n_neighbors=None, return_distance=True, , two_pass_precision=False)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/nearest_neighbors.pyx#L701)

Query the GPU index for the k nearest neighbors of column vectors in X.

* **Parameters:**
  **X**
  : 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.

  **n_neighbors**
  : Number of neighbors to search. If not provided, the n_neighbors
    from the model instance is used (default=10)

  **return_distance: Boolean**
  : If False, distances will not be returned

  **two_pass_precision**
  : When set to True, a slow second pass will be used to improve the
    precision of results returned for searches using L2-derived
    metrics. FAISS uses the Euclidean distance decomposition trick to
    compute distances in this case, which may result in numerical
    errors for certain data. In particular, when several samples
    are close to the query sample (relative to typical inter-sample
    distances), numerical instability may cause the computed distance
    between the query and itself to be larger than the computed
    distance between the query and another sample. As a result, the
    query is not returned as the nearest neighbor to itself.  If this
    flag is set to true, distances to the query vectors will be
    recomputed with high precision for all retrieved samples, and the
    results will be re-sorted accordingly. Note that for large values
    of k or large numbers of query vectors, this correction becomes
    impractical in terms of both runtime and memory. It should be used
    with care and only when strictly necessary (when precise results
    are critical and samples may be tightly clustered).
* **Returns:**
  **distances**
  : The distances of the k-nearest neighbors for each column vector
    in X

  **indices**
  : The indices of the k-nearest neighbors for each column vector in X

<!-- !! processed by numpydoc !! -->

#### kneighbors_graph(self, X=None, n_neighbors=None, mode='connectivity')[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/nearest_neighbors.pyx#L942)

Find the k nearest neighbors of column vectors in X and return as
a sparse matrix in CSR format.

* **Parameters:**
  **X**
  : 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.

  **n_neighbors**
  : Number of neighbors to search. If not provided, the n_neighbors
    from the model instance is used

  **mode**
  : Values in connectivity matrix: ‘connectivity’ returns the
    connectivity matrix with ones and zeros, ‘distance’ returns the
    edges as the distances between points with the requested metric.
* **Returns:**
  **A**
  : n_samples_fit is the number of samples in the fitted data where
    A[i, j] is assigned the weight of the edge that connects i to j.
    Values will either be ones/zeros or the selected distance metric.
    Return types are either cupy’s CSR sparse graph (device) or
    numpy’s CSR sparse graph (host)

<!-- !! processed by numpydoc !! -->

#### predict(self, X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/neighbors/kneighbors_regressor.pyx#L252)

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

* **Parameters:**
  **X**
  : 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.
* **Returns:**
  **X_new**
  : Predicted values
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->

#### score(X, y, sample_weight=None, \*\*kwargs)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/mixins.py#L66)

Scoring function for regression estimators

Returns the coefficient of determination R^2 of the prediction.

* **Parameters:**
  **X**
  : 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.

  **y**
  : 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.

  **sample_weight**
  : The weights for each observation in X. If None, all observations
    are assigned equal weight.
    Acceptable formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **score**
  : R^2 of self.predict(X) wrt. y.

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L200)

Accepts a dict of params and updates the corresponding ones owned by
this class. If the child class has appropriately overridden the
`_get_param_names` method and does not need anything other than what is,
there in this method, then it doesn’t have to override this method

<!-- !! processed by numpydoc !! -->
