Class: CountVectorizer
Convert a collection of text documents to a matrix of token counts.
This implementation produces a sparse representation of the counts using scipy.sparse.csr_matrix.
If you do not provide an a-priori dictionary and you do not use an analyzer that does some kind of feature selection then the number of features will be equal to the vocabulary size found by analyzing the data.
For an efficiency comparison of the different feature extractors, see FeatureHasher and DictVectorizer Comparison.
Read more in the User Guide.
Constructors
new CountVectorizer()
new CountVectorizer(
opts
?):CountVectorizer
Parameters
Parameter | Type | Description |
---|---|---|
opts ? | object | - |
opts.analyzer ? | "word" | "char" | "char_wb" | Whether the feature should be made of word n-gram 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. If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input. |
opts.binary ? | boolean | 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. |
opts.decode_error ? | "strict" | "ignore" | "replace" | Instruction on what to do if a byte sequence is given to analyze that contains characters not of the given encoding . By default, it is ‘strict’, meaning that a UnicodeDecodeError will be raised. Other values are ‘ignore’ and ‘replace’. |
opts.dtype ? | any | Type of the matrix returned by fit_transform() or transform(). |
opts.encoding ? | string | If bytes or files are given to analyze, this encoding is used to decode. |
opts.input ? | "filename" | "file" | "content" | If 'filename' , the sequence passed as an argument to fit is expected to be a list of filenames that need reading to fetch the raw content to analyze. |
opts.lowercase ? | boolean | Convert all characters to lowercase before tokenizing. |
opts.max_df ? | number | 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 undefined . |
opts.max_features ? | number | If not undefined , build a vocabulary that only consider the top max_features ordered by term frequency across the corpus. Otherwise, all features are used. This parameter is ignored if vocabulary is not undefined . |
opts.min_df ? | number | 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 undefined . |
opts.ngram_range ? | any | The lower and upper boundary of the range of n-values for different word n-grams or char n-grams to be extracted. All values of n such 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. Only applies if analyzer is not callable. |
opts.preprocessor ? | any | Override the preprocessing (strip_accents and lowercase) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable. |
opts.stop_words ? | any [] | "english" | If ‘english’, a built-in stop word list for English is used. There are several known issues with ‘english’ and you should consider an alternative (see Using stop words). If a list, that list is assumed to contain stop words, all of which will be removed from the resulting tokens. Only applies if analyzer \== 'word' . If undefined , no stop words will be used. In this case, setting max_df to a higher value, such as in the range (0.7, 1.0), can automatically detect and filter stop words based on intra corpus document frequency of terms. |
opts.strip_accents ? | "ascii" | "unicode" | Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have a direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. undefined (default) means no character normalization is performed. Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize . |
opts.token_pattern ? | string | Regular expression denoting what constitutes a “token”, only used if analyzer \== 'word' . The default regexp select tokens of 2 or more alphanumeric characters (punctuation is completely ignored and always treated as a token separator). If there is a capturing group in token_pattern then the captured group content, not the entire match, becomes the token. At most one capturing group is permitted. |
opts.tokenizer ? | any | Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer \== 'word' . |
opts.vocabulary ? | any | Either a Mapping (e.g., a dict) where keys are terms and values are indices in the feature matrix, or an iterable over terms. If not given, a vocabulary is determined from the input documents. Indices in the mapping should not be repeated and should not have any gap between 0 and the largest index. |
Returns CountVectorizer
Defined in generated/feature_extraction/text/CountVectorizer.ts:29
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/feature_extraction/text/CountVectorizer.ts:27 |
_isInitialized | boolean | false | generated/feature_extraction/text/CountVectorizer.ts:26 |
_py | PythonBridge | undefined | generated/feature_extraction/text/CountVectorizer.ts:25 |
id | string | undefined | generated/feature_extraction/text/CountVectorizer.ts:22 |
opts | any | undefined | generated/feature_extraction/text/CountVectorizer.ts:23 |
Accessors
fixed_vocabulary_
Get Signature
get fixed_vocabulary_():
Promise
<boolean
>
True if a fixed vocabulary of term to indices mapping is provided by the user.
Returns Promise
<boolean
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:679
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge
):void
Parameters
Parameter | Type |
---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/feature_extraction/text/CountVectorizer.ts:147
vocabulary_
Get Signature
get vocabulary_():
Promise
<any
>
A mapping of terms to feature indices.
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:654
Methods
build_analyzer()
build_analyzer(
opts
):Promise
<any
>
Return a callable to process input data.
The callable handles preprocessing, tokenization, and n-grams generation.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:218
build_preprocessor()
build_preprocessor(
opts
):Promise
<any
>
Return a function to preprocess the text before tokenization.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:246
build_tokenizer()
build_tokenizer(
opts
):Promise
<any
>
Return a function that splits a string into a sequence of tokens.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:274
decode()
decode(
opts
):Promise
<any
>
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.doc ? | string | The string to decode. |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:304
dispose()
dispose():
Promise
<void
>
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Returns Promise
<void
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:199
fit()
fit(
opts
):Promise
<any
>
Learn a vocabulary dictionary of all tokens in the raw documents.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.raw_documents ? | any | An iterable which generates either str, unicode or file objects. |
opts.y ? | any | This parameter is ignored. |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:336
fit_transform()
fit_transform(
opts
):Promise
<any
[]>
Learn the vocabulary dictionary and return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.raw_documents ? | any | An iterable which generates either str, unicode or file objects. |
opts.y ? | any | This parameter is ignored. |
Returns Promise
<any
[]>
Defined in generated/feature_extraction/text/CountVectorizer.ts:375
get_feature_names_out()
get_feature_names_out(
opts
):Promise
<any
>
Get output feature names for transformation.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.input_features ? | any | Not used, present here for API consistency by convention. |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:412
get_metadata_routing()
get_metadata_routing(
opts
):Promise
<any
>
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.routing ? | any | A MetadataRequest encapsulating routing information. |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:448
get_stop_words()
get_stop_words(
opts
):Promise
<any
>
Build or fetch the effective stop words list.
Parameters
Parameter | Type |
---|---|
opts | object |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:482
init()
init(
py
):Promise
<void
>
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Parameters
Parameter | Type |
---|---|
py | PythonBridge |
Returns Promise
<void
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:160
inverse_transform()
inverse_transform(
opts
):Promise
<any
[]>
Return terms per document with nonzero entries in X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | Document-term matrix. |
Returns Promise
<any
[]>
Defined in generated/feature_extraction/text/CountVectorizer.ts:510
set_fit_request()
set_fit_request(
opts
):Promise
<any
>
Request metadata passed to the fit
method.
Note that this method is only relevant if enable_metadata_routing=True
(see sklearn.set_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.raw_documents ? | string | boolean | Metadata routing for raw_documents parameter in fit . |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:548
set_transform_request()
set_transform_request(
opts
):Promise
<any
>
Request metadata passed to the transform
method.
Note that this method is only relevant if enable_metadata_routing=True
(see sklearn.set_config
). Please see User Guide on how the routing mechanism works.
The options for each parameter are:
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.raw_documents ? | string | boolean | Metadata routing for raw_documents parameter in transform . |
Returns Promise
<any
>
Defined in generated/feature_extraction/text/CountVectorizer.ts:586
transform()
transform(
opts
):Promise
<any
[]>
Transform documents to document-term matrix.
Extract token counts out of raw text documents using the vocabulary fitted with fit or the one provided to the constructor.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.raw_documents ? | any | An iterable which generates either str, unicode or file objects. |
Returns Promise
<any
[]>
Defined in generated/feature_extraction/text/CountVectorizer.ts:622