# MBSGDClassifier

### *class* cuml.linear_model.MBSGDClassifier(, loss='hinge', penalty='l2', alpha=0.0001, l1_ratio=0.15, fit_intercept=True, epochs=1000, tol=0.001, shuffle=True, learning_rate='constant', eta0=0.001, power_t=0.5, batch_size=32, n_iter_no_change=5, verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/mbsgd_classifier.py#L15)

Linear models (linear SVM, logistic regression, or linear regression)
fitted by minimizing a regularized empirical loss with mini-batch SGD.
The MBSGD Classifier implementation is experimental and it uses a
different algorithm than sklearn’s SGDClassifier. In order to improve
the results obtained from cuML’s MBSGDClassifier:

* Reduce the batch size
* Increase the eta0
* Increase the number of iterations

Since cuML is analyzing the data in batches using a small eta0 might
not let the model learn as much as scikit learn does. Furthermore,
decreasing the batch size might seen an increase in the time required
to fit the model.

* **Parameters:**
  **loss**
  : ‘hinge’ uses linear SVM
    <br/>
    ‘log’ uses logistic regression
    <br/>
    ‘squared_loss’ uses linear regression

  **penalty**
  : The penalty (aka regularization term) to apply.
    - ‘l1’: L1 norm (Lasso) regularization
    - ‘l2’: L2 norm (Ridge) regularization (the default)
    - ‘elasticnet’: Elastic Net regularization, a weighted average of L1 and L2
    - None: no penalty is added

  **alpha**
  : The constant value which decides the degree of regularization

  **l1_ratio**
  : The l1_ratio is used only when `penalty = elasticnet`. The value for
    l1_ratio should be `0 <= l1_ratio <= 1`. When `l1_ratio = 0` then the
    `penalty = 'l2'` and if `l1_ratio = 1` then `penalty = 'l1'`

  **batch_size**
  : It sets the number of samples that will be included in each batch.

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

  **epochs**
  : The number of times the model should iterate through the entire dataset
    during training (default = 1000)

  **tol**
  : The training process will stop if current_loss > previous_loss - tol

  **shuffle**
  : True, shuffles the training data after each epoch
    False, does not shuffle the training data after each epoch

  **eta0**
  : Initial learning rate

  **power_t**
  : The exponent used for calculating the invscaling learning rate

  **learning_rate**
  : `constant` keeps the learning rate constant
    <br/>
    `adaptive` changes the learning rate if the training loss or the
    validation accuracy does not improve for `n_iter_no_change` epochs.
    The old learning rate is generally divided by 5

  **n_iter_no_change**
  : the number of epochs to train without any improvement in the model

  **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_features,)**
  : The model coefficients.

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

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

### Methods

| [`fit`](#cuml.linear_model.MBSGDClassifier.fit)(X, y)   | Fit the model with X and y.   |
|--------------------------------------------------------------|-------------------------------|
| [`predict`](#cuml.linear_model.MBSGDClassifier.predict)(X)  | Predicts the y for X.         |

### Notes

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

### Examples

```pycon
>>> import cupy as cp
>>> import cuml
>>> X = cp.array([[1, 1], [1, 2], [2, 2], [2, 3]], dtype=cp.float32)
>>> y = cp.array([1, 1, 2, 2])
>>> X_test = cp.asarray([[3, 5], [2, 5]], dtype=cp.float32)
>>> model = cuml.MBSGDClassifier().fit(X, y)
>>> model.predict(X_test)
array([2, 2])
```

<!-- !! 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) → [MBSGDClassifier](#cuml.linear_model.MBSGDClassifier)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/linear_model/mbsgd_classifier.py#L177)

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.

<!-- !! 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/mbsgd_classifier.py#L208)

Predicts the y 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#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 !! -->
