# LogisticRegression

### *class* cuml.linear_model.LogisticRegression(, penalty='l2', tol=0.0001, C=1.0, fit_intercept=True, class_weight=None, max_iter=1000, linesearch_max_iter=50, l1_ratio=None, solver='qn', lbfgs_memory=5, penalty_normalized=True, verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/logistic_regression.py#L22)

Logistic Regression classifier.

LogisticRegression is a linear model that is used to model probability of
occurrence of certain events, for example probability of success or fail of
an event.

* **Parameters:**
  **penalty**
  : Specifies the penalty term to use. `'l1'` and `'l2'` will use an L1 or
    L2 penalty, respectively. `'elasticnet'` will use both an L1 and L2
    penalty. [`None`](https://docs.python.org/3/builtins/constants.html#None) will not use a penalty.

  **tol**
  : Tolerance for stopping criteria.
    The exact stopping conditions depend on the chosen solver.
    Check the solver’s documentation for more details:
    > * `Quasi-Newton (L-BFGS/OWL-QN)`

  **C**
  : Inverse of regularization strength; must be a positive float.

  **fit_intercept**
  : Specifies if a constant (a.k.a bias or intercept) should be added to
    the decision function. Note that, just like in Scikit-learn, the bias
    will not be regularized.

  **class_weight**
  : By default all classes have a weight one. However, a dictionary
    can be provided with weights associated with classes
    in the form `{class_label: weight}`. The “balanced” mode
    uses the values of y to automatically adjust weights
    inversely proportional to class frequencies in the input data
    as `n_samples / (n_classes * np.bincount(y))`. Note that
    these weights will be multiplied with sample_weight
    (passed through the fit method) if sample_weight is specified.

  **max_iter**
  : Maximum number of iterations taken for the solvers to converge.

  **linesearch_max_iter**
  : Max number of linesearch iterations per outer iteration used in the
    lbfgs and owl QN solvers.

  **l1_ratio**
  : The Elastic-Net mixing parameter, with `0 <= l1_ratio <= 1`

  **solver**
  : Algorithm to use in the optimization problem. Currently only `qn` is
    supported, which automatically selects either L-BFGS or OWL-QN
    depending on the conditions of the l1 regularization described
    above.

  **lbfgs_memory: int, default = 5**
  : Rank of the lbfgs inverse-Hessian approximation. Method will use
    O(lbfgs_memory \* n_features) memory.

  **penalty_normalized**
  : By default the penalty term is divided by the sample size. Set to False
    to skip this behavior.

  **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_: array, shape=(n_classes, n_features) or (n_classes, n_features)**
  : The estimated coefficients for the logistic regression model.

  **intercept_: array, shape=(1,) or (n_classes,)**
  : The independent term. If `fit_intercept` is False, will be 0.

  **n_iter_: array, shape (1,)**
  : The number of iterations taken for the solvers to converge.

  **classes_**
  : Array of the class labels.

### Methods

| [`fit`](#cuml.linear_model.LogisticRegression.fit)(X, y[, sample_weight])   | Fit the model with X and y.                              |
|-------------------------------------------------------------------------------|----------------------------------------------------------|
| [`predict`](#cuml.linear_model.LogisticRegression.predict)(X)                   | Predicts the y for X.                                    |
| [`predict_log_proba`](#cuml.linear_model.LogisticRegression.predict_log_proba)(X)         | Predicts the log class probabilities for each class in X |
| [`predict_proba`](#cuml.linear_model.LogisticRegression.predict_proba)(X)             | Predicts the class probabilities for each class in X     |

### Notes

cuML’s LogisticRegression uses a different solver that the equivalent
Scikit-learn, except when there is no penalty and `solver=lbfgs` is
used in Scikit-learn. This can cause (smaller) differences in the
coefficients and predictions of the model, similar to
using different solvers in Scikit-learn.

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

### Examples

```pycon
>>> import cuml
>>> import cupy as cp
>>> X = cp.array([[1, 1], [1, 2], [2, 2], [2, 3]])
>>> y = cp.array([0, 0, 1, 1])
>>> model = cuml.LogisticRegression().fit(X, y)
>>> model.predict(X)
array([0, 0, 1, 1])
```

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

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

Predict confidence scores for samples.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **scores**
  : Confidence scores
    <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 !! -->

#### fit(X, y, sample_weight=None) → [LogisticRegression](#cuml.linear_model.LogisticRegression)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/logistic_regression.py#L307)

Fit the model with X and y.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense 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/logistic_regression.py#L341)

Predicts the y for X.

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense 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 !! -->

#### predict_log_proba(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/logistic_regression.py#L394)

Predicts the log class probabilities for each class in X

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **probs**
  : Log probabilities per class for each sample.
    <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 !! -->

#### predict_proba(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/logistic_regression.py#L365)

Predicts the class probabilities for each class in X

* **Parameters:**
  **X**
  : Dense or sparse matrix with dtype float32 or float64.
    Acceptable dense formats: CUDA array interface compliant objects like
    CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas
    DataFrame/Series.
* **Returns:**
  **probs**
  : Probabilities per class for each sample.
    <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#L101)

Scoring function for classifier estimators based on mean accuracy.

* **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**
  : Accuracy of self.predict(X) wrt. y (fraction where y == pred_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 !! -->
