# Lasso

### *class* cuml.linear_model.Lasso(alpha=1.0, , fit_intercept=True, max_iter=1000, tol=0.001, solver='auto', selection='cyclic', output_type=None, verbose=False)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/lasso.py#L9)

Linear Model trained with L1 prior as regularizer (aka the Lasso).

This is the same as `ElasticNet(l1_ratio=1.0)` (no L2 penalty).

* **Parameters:**
  **alpha**
  : Constant that multiplies the L1 term.
    alpha = 0 is equivalent to an ordinary least square, solved by the
    LinearRegression object.
    For numerical reasons, using alpha = 0 with the Lasso object is not
    advised.
    Given this, you should use the LinearRegression object.

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

  **max_iter**
  : The maximum number of iterations

  **tol**
  : The tolerance for the optimization: if the updates are smaller than
    tol, the optimization code checks the dual gap for optimality and
    continues until it is smaller than tol.

  **solver**
  : The solver to use.
    - ‘auto’: uses ‘cd’ for dense inputs, and ‘qn’ for sparse inputs
    - ‘cd’: uses coordinate descent. Only supports dense inputs.
    - ‘qn’: uses quasi-newton methods. Supports sparse and dense inputs.
    <br/>
    You may find the alternative ‘qn’ algorithm is faster when the number
    of features is sufficiently large but the sample size is small.

  **selection**
  : If set to ‘random’, a random coefficient is updated every iteration
    rather than looping over features sequentially by default.
    This (setting to ‘random’) often leads to significantly faster
    convergence especially when tol is higher than 1e-4.

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

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

  [`sparse_coef_`](#cuml.linear_model.Lasso.sparse_coef_)
  : Sparse representation of the fitted `coef_`.

  **intercept_**
  : The independent term. If `fit_intercept` is False, will be 0.

  **n_iter_**
  : The number of iterations taken by the solver.

### Notes

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

### Examples

```pycon
>>> import numpy as np
>>> import cudf
>>> from cuml.linear_model import Lasso
>>> ls = Lasso(alpha = 0.1, solver='qn')
>>> X = cudf.DataFrame()
>>> X['col1'] = np.array([0, 1, 2], dtype = np.float32)
>>> X['col2'] = np.array([0, 1, 2], dtype = np.float32)
>>> y = cudf.Series( np.array([0.0, 1.0, 2.0], dtype = np.float32) )
>>> result_lasso = ls.fit(X, y)
>>> print(result_lasso.coef_)
0   0.425
1   0.425
dtype: float32
>>> print(result_lasso.intercept_)
0.150000...
```

```pycon
>>> X_new = cudf.DataFrame()
>>> X_new['col1'] = np.array([3,2], dtype = np.float32)
>>> X_new['col2'] = np.array([5,5], dtype = np.float32)
>>> preds = result_lasso.predict(X_new)
>>> print(preds)
0   3.549997
1   3.124997
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(X, y, sample_weight=None) → [ElasticNet](../cuml.linear_model.ElasticNet/index.html.md#cuml.linear_model.ElasticNet)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/elastic_net.py#L224)

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

#### *property* sparse_coef_

Sparse representation of the fitted `coef_`.

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