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

**`class CosineSimilarityClassifier(config)`**

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

The cosine similarity [`Classifier`](squirro.lib.nlp.steps.classifiers.Classifier.md#squirro.lib.nlp.steps.classifiers.Classifier) uses the [cosine similarity](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html) to decide which text fragment is closest to which class.

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

**Output** - the output field is filled with data of type [`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) }. 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 cosine distance to the closest reference data point.

Parameters

`type` ([`str`](https://docs.python.org/3.11/library/stdtypes.html#str)) – cosine_similarity

**Example**

```json
{
    "step": "classifier",
    "type": "cosine_similarity",
    "label_field": "label",
    "input_field": "embedded_extract",
    "output_field": "prediction",
}
```

Methods SummaryMethods Documentation

**`load()`**

Load a step

**`process(docs)`**

Process 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))

**`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))
