# CountVectorizer

### *class* cuml.feature_extraction.text.CountVectorizer(\*, lowercase=True, preprocessor=None, tokenizer=None, delimiter=None, stop_words=None, ngram_range=(1, 1), analyzer='word', max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, dtype=<class 'numpy.float32'>, verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L620)

Convert a collection of text documents to a matrix of token counts.

If you do not provide an a-priori dictionary then the number of features
will be equal to the vocabulary size found by analyzing the data.

* **Parameters:**
  **lowercase**
  : Convert all characters to lowercase before tokenizing.

  **preprocessor**
  : Override the preprocessing (string transformation) stage while
    preserving the tokenizing and n-grams generation steps.
    This function receives a `cudf.Series` of strings and should
    return a `cudf.Series` of strings.

  **tokenizer**
  : Override the string tokenization step while preserving the
    preprocessing and n-grams generation steps. This function
    receives a `cudf.Series` of strings and should return
    a `cudf.Series` of lists of strings.
    Only applies if `analyzer == 'word'`.

  **delimiter**
  : String used to delimit tokens in the document. If `None`, then any
    non-alphanumeric (or “ “) character is treated as a delimiter.
    Only applies if `analyzer == "word'`.

  **stop_words**
  : If ‘english’, a built-in stop word list for English is used. If a list,
    that list is assumed to contain stop words, all of which will be
    removed from the resulting tokens. If None, no stop words will be used.
    Only applies if `analyzer == 'word'`.

  **ngram_range**
  : The lower and upper boundary of the range of n-values for different
    n-grams to be extracted. All values of n such that min_n <= n <= max_n
    will be used. For example an `ngram_range` of `(1, 1)` means only
    unigrams, `(1, 2)` means unigrams and bigrams, and `(2, 2)` means
    only bigrams.

  **analyzer**
  : Whether the feature should be made of word or character n-grams.
    Option ‘char_wb’ creates character n-grams only from text inside
    word boundaries; n-grams at the edges of words are padded with space.

  **max_df**
  : When building the vocabulary ignore terms that have a document
    frequency strictly higher than the given threshold (corpus-specific
    stop words).
    If float, the parameter represents a proportion of documents, integer
    absolute counts.
    This parameter is ignored if vocabulary is not None.

  **min_df**
  : When building the vocabulary ignore terms that have a document
    frequency strictly lower than the given threshold. This value is also
    called cut-off in the literature.
    If float, the parameter represents a proportion of documents, integer
    absolute counts.
    This parameter is ignored if vocabulary is not None.

  **max_features**
  : If not None, build a vocabulary that only consider the top
    `max_features` ordered by term frequency across the corpus.
    Otherwise, all features are used.
    <br/>
    This parameter is ignored if vocabulary is not None.

  **vocabulary**
  : Either an array-like of terms, or a mapping where keys are terms and
    values are indices in the feature matrix. If not given, a vocabulary is
    determined from the input documents.

  **binary**
  : If True, all non zero counts are set to 1. This is useful for discrete
    probabilistic models that model binary events rather than integer
    counts.

  **dtype**
  : Type of the matrix returned by fit_transform() or transform().

  **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:**
  **vocabulary_**
  : The vocabulary used to map terms to feature indices.

  **fixed_vocabulary_**
  : True if a fixed vocabulary of term to indices mapping
    is provided by the user.

### Methods

| [`fit`](#cuml.feature_extraction.text.CountVectorizer.fit)(X[, y])                             | Fit the vectorizer.                                   |
|------------------------------------------------------------------------------------------|-------------------------------------------------------|
| [`fit_transform`](#cuml.feature_extraction.text.CountVectorizer.fit_transform)(X[, y])                   | Fit the vectorizer and return a document-term matrix. |
| [`get_feature_names_out`](#cuml.feature_extraction.text.CountVectorizer.get_feature_names_out)([input_features]) | Get output feature names for transformation.          |
| [`inverse_transform`](#cuml.feature_extraction.text.CountVectorizer.inverse_transform)(X)                    | Return terms per document with nonzero entries in X.  |
| [`transform`](#cuml.feature_extraction.text.CountVectorizer.transform)(X)                            | Transform documents to document-term matrix.          |

### Examples

```pycon
>>> from cuml.feature_extraction.text import CountVectorizer
>>> corpus = [
...     'This is the first document.',
...     'This document is the second document.',
...     'And this is the third one.',
...     'Is this the first document?',
... ]
>>> vectorizer = CountVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> vectorizer.get_feature_names_out()
array(['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third',
       'this'], ...)
>>> X.shape
(4, 9)
```

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

#### fit(X, y=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L895)

Fit the vectorizer.

* **Parameters:**
  **X**
  : Training samples. Each sample must be a text document
    which will be tokenized and hashed.

  **y**
  : Ignored. Exists for API compatibility only.
* **Returns:**
  **self**
  : The instance itself.

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

#### fit_transform(X, y=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L918)

Fit the vectorizer and return a document-term matrix.

* **Parameters:**
  **X**
  : Training samples. Each sample must be a text document
    which will be tokenized and hashed.

  **y**
  : Ignored. Exists for API compatibility only.
* **Returns:**
  **X**
  : Document-term matrix.

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

#### get_feature_names_out(input_features=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L981)

Get output feature names for transformation.

* **Parameters:**
  **input_features**
  : Not used, present here for API consistency by convention.
* **Returns:**
  **feature_names_out**
  : Transformed feature names.

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

#### inverse_transform(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L961)

Return terms per document with nonzero entries in X.

* **Parameters:**
  **X**
  : Document-term matrix.
* **Returns:**
  **X_original**
  : List of arrays of terms.

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

#### transform(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/feature_extraction/text.py#L941)

Transform documents to document-term matrix.

* **Parameters:**
  **X**
  : Training samples. Each sample must be a text document
    which will be tokenized and hashed.
* **Returns:**
  **X**
  : Document-term matrix.

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