<!-- Source: https://docs.squirro.com/en/latest/api/squirro.lib.nlp.steps.classifiers.SklearnClassifier.html -->
# SklearnClassifier

**`class SklearnClassifier(config)`**

Bases: [`Classifier`](squirro.lib.nlp.steps.classifiers.Classifier.md#squirro.lib.nlp.steps.classifiers.Classifier)

The sklearn [`Classifier`](squirro.lib.nlp.steps.classifiers.Classifier.md#squirro.lib.nlp.steps.classifiers.Classifier) is a generic [scikit-learn](http://scikit-learn.org/stable/index.html) classifier.

**Note - currently we support following scikit-learn classifier:**

- [MultinomialNB](https://scikit-learn.org/stable/modules/naive_bayes.html#multinomial-naive-bayes)
- [BernoulliNB](https://scikit-learn.org/stable/modules/naive_bayes.html#bernoulli-naive-bayes)
- [GaussianNB](https://scikit-learn.org/stable/modules/naive_bayes.html#gaussian-naive-bayes)
- [Perceptron](https://scikit-learn.org/stable/modules/linear_model.html#perceptron)
- [SGDClassifier](https://scikit-learn.org/stable/modules/linear_model.html#stochastic-gradient-descent-sgd)
- [MLPClassifier](https://scikit-learn.org/stable/modules/neural_networks_supervised.html#multi-layer-perceptron)
- [PassiveAggressiveClassifier](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.PassiveAggressiveClassifier.html#sklearn.linear_model.PassiveAggressiveClassifier)
- [PassiveAggressiveRegressor](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.PassiveAggressiveRegressor.html#sklearn.linear_model.PassiveAggressiveRegressor)
- [SVC](https://scikit-learn.org/stable/modules/svm.html#classification)
- [SVR](https://scikit-learn.org/stable/modules/svm.html#regression)

**Input** - the input field need to be of type [`list`](https://docs.python.org/3.11/library/stdtypes.html#list) [ [`float`](https://docs.python.org/3.11/library/functions.html#float) or [`int`](https://docs.python.org/3.11/library/functions.html#int) ] or `numpy.ndarray`. In addition also the types [`str`](https://docs.python.org/3.11/library/stdtypes.html#str), [`float`](https://docs.python.org/3.11/library/functions.html#float),  [`int`](https://docs.python.org/3.11/library/functions.html#int), [`list`](https://docs.python.org/3.11/library/stdtypes.html#list) [ [`str`](https://docs.python.org/3.11/library/stdtypes.html#str) ] and `numpy.sparse` matrices are supported.

**Output** - the output field is filled with data of type [`str`](https://docs.python.org/3.11/library/stdtypes.html#str) or [`dict`](https://docs.python.org/3.11/library/stdtypes.html#dict) { [`str`](https://docs.python.org/3.11/library/stdtypes.html#str): [`float`](https://docs.python.org/3.11/library/functions.html#float) } if the model has the property “predict_proba”. The key of the [`dict`](https://docs.python.org/3.11/library/stdtypes.html#dict) is the predicted class name and the value is the probability/confidence returned by the model.

Parameters

- `type` ([`str`](https://docs.python.org/3.11/library/stdtypes.html#str)) – sklearn
- `model_type` ([`str`](https://docs.python.org/3.11/library/stdtypes.html#str)) – Type of scikit learn model
- `model_kwargs` ([`dict`](https://docs.python.org/3.11/library/stdtypes.html#dict)`, ``{}`) – Keyword arguments for scikit learn model
- `use_sparse` ([`bool`](https://docs.python.org/3.11/library/functions.html#bool)`, ``False`) – Whether or not to use a sparse input matrix

**Example**

```json
{
    "step": "classifier",
    "type": "sklearn",
    "model_type": "GaussianNB",
    "model_kwargs": {},
    "label_field": "label",
    "input_fields": ["embedded_extract"],
    "output_field": "prediction",
}
```

Methods SummaryMethods Documentation

**`load()`**

Load a step

**`process_batch(batch)`**

Process a batch of documents. If not defined will default to using
self.process_doc for each document in the batch.

Parameters

`batch` ([`list`](https://docs.python.org/3.11/library/stdtypes.html#list)`(`[`Document`](../technical/libnlp/base.md#squirro.lib.nlp.document.Document)`)`) – List of documents

Returns

List of processed documents

Return type

[list](https://docs.python.org/3.11/library/stdtypes.html#list)([Document](../technical/libnlp/base.md#squirro.lib.nlp.document.Document))

**`save()`**

Save a step

**`train(docs)`**

Train on a step of a set of documents

Parameters

`docs` (`generator``(`[`Document`](../technical/libnlp/base.md#squirro.lib.nlp.document.Document)`)`) – Generator of documents

Returns

Generator of processed documents

Return type

generator([Document](../technical/libnlp/base.md#squirro.lib.nlp.document.Document))
