# LinearRegression

### *class* cuml.linear_model.LinearRegression(, algorithm='auto', fit_intercept=True, copy_X=True, verbose=False, output_type=None)

Ordinary least squares Linear Regression.

* **Parameters:**
  **algorithm**
  : The algorithm to use when fitting:
    - ‘auto’: will select ‘eig’ if supported, falling back to ‘lsmr’ if X
      is sparse, and ‘svd’ otherwise.
    - ‘eig’: uses an eigendecomposition of the covariance matrix. It is
      faster than SVD, but potentially unstable. It doesn’t support
      multi-target `y` or sparse `X`.
    - ‘svd’ or ‘svd-jacobi’: uses an SVD decomposition. It’s slower, but
      stable. It doesn’t support sparse `X`.
    - ‘lsmr’: uses `cupyx.scipy.sparse.linalg.lsmr`, an iterative
      algorithm. It supports all input types and is typically very fast.
    - ‘qr’: uses QR decomposition and solves `Rx = Q^T y`. It’s faster
      than SVD, but doesn’t support multi-target `y` or sparse `X`.
    - ‘svd-qr’: computes SVD decomposition using QR algorithm. It’s the
      slowest option. It doesn’t support multi-target `y` or sparse `X`.

  **fit_intercept**
  : If True, LinearRegression tries to correct for the global mean of y.
    If False, the model expects that you have centered the data.

  **copy_X**
  : If True, X will never be mutated. Setting to False may reduce memory
    usage, at the cost of potentially mutating X.

  **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.
* **Attributes:**
  **coef_**
  : The estimated coefficients for the linear regression model.

  **intercept_**
  : The independent term. If `fit_intercept` is False, will be 0. Will be
    an array when fit on multi-target y, otherwise will be a float.

### Methods

| [`fit`](#cuml.linear_model.LinearRegression.fit)(self, X, y[, sample_weight])   | Fit the model with X and y.   |
|-------------------------------------------------------------------------------------|-------------------------------|

### Notes

LinearRegression suffers from multicollinearity (when columns are
correlated with each other), and variance explosions from outliers.
Consider using [`Ridge`](../cuml.linear_model.Ridge/index.html.md#cuml.linear_model.Ridge) to fix the multicollinearity problem, and
consider maybe first `DBSCAN` to remove the outliers, or
statistical analysis to filter possible outliers.

**Applications of LinearRegression**

> LinearRegression is used in regression tasks where one wants to predict
> say sales or house prices. It is also used in extrapolation or time
> series tasks, dynamic systems modelling and many other machine learning
> tasks. This model should be first tried if the machine learning problem
> is a regression task (predicting a continuous variable).

For additional information, see scikit-learn’s documentation for
[`sklearn.linear_model.LinearRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression).

For an additional example see [the OLS notebook](https://github.com/NVIDIA/cuml/blob/main/notebooks/linear_regression_demo.ipynb).

### Examples

```pycon
>>> import cupy as cp
>>> from cuml.linear_model import LinearRegression
>>> X = cp.array([[1, 1], [1, 2], [2, 2], [2, 3]], dtype=cp.float32)
>>> y = cp.array([6.0, 8.0, 9.0, 11.0], dtype=cp.float32)
>>> model = LinearRegression().fit(X, y)
```

```pycon
>>> X_test = cp.array([[3, 5], [2, 5]], dtype=cp.float32)
>>> model.predict(X_test)
array([16.      , 14.999999], 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, sample_weight=None) → 'LinearRegression'[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/linear_regression.pyx#L315)

Fit the model with X and y.

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

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

#### predict(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/base.py#L14)

Predicts `y` values 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:**
  **preds**
  : 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 !! -->
