Learn a list of feature name -> indices mappings and transform X.
Like fit(X) followed by transform(X), but does not require materializing X in memory.
Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).
Changed in version 0.24: Accepts multiple string values for one categorical feature.
Feature vectors; always 2-d.


Returns a list of feature names, ordered by their indices.
If one-of-K coding is applied to categorical features, this will include the constructed feature names but not the original ones.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Transform array or sparse matrix X back to feature mappings.
X must have been produced by this DictVectorizer’s transform or fit_transform method; it may only have passed through transformers that preserve the number of features and their order.
In the case of one-hot/one-of-K coding, the constructed feature names and values are returned rather than the original ones.
Sample matrix.
Constructor for feature mappings. Must conform to the collections.Mapping API.
Feature mappings for the samples in X.


Restrict the features to those in support using feature selection.
This function modifies the estimator in-place.
Boolean mask or list of indices (as returned by the get_support member of feature selectors).
Whether support is a list of indices.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Transform feature->value dicts to array or sparse matrix.
Named features not encountered during fit or fit_transform will be silently ignored.
Dict(s) or Mapping(s) from feature names (arbitrary Python objects) to feature values (strings or convertible to dtype).
Feature vectors; always 2-d.


Implements feature hashing, aka the hashing trick.
This class turns sequences of symbolic feature names (strings) into scipy.sparse matrices, using a hash function to compute the matrix column corresponding to a name. The hash function employed is the signed 32-bit version of Murmurhash3.
Feature names of type byte string are used as-is. Unicode strings are converted to UTF-8 first, but no Unicode normalization is done. Feature values must be (finite) numbers.
This class is a low-memory alternative to DictVectorizer and CountVectorizer, intended for large-scale (online) learning and situations where memory is tight, e.g. when running prediction code on embedded devices.
Read more in the User Guide.
New in version 0.13.
The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners.
Either “dict” (the default) to accept dictionaries over (feature_name, value); “pair” to accept pairs of (feature_name, value); or “string” to accept single strings. feature_name should be a string, while value should be a number. In the case of “string”, a value of 1 is implied. The feature_name is hashed to find the appropriate column for the feature. The value’s sign might be flipped in the output (but see non_negative, below).
The type of feature values. Passed to scipy.sparse matrix constructors as the dtype argument. Do not set this to bool, np.boolean or any unsigned integer type.
When True, an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection.
alternate_sign replaces the now deprecated non_negative parameter.
See also
Vectorizes string-valued features using a hash table.
Handles nominal/categorical features.
fit([X, y])
No-op.
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
set_params(**params)
Set the parameters of this estimator.
transform(raw_X)
Transform a sequence of instances to a scipy.sparse matrix.
No-op.
This method doesn’t do anything. It exists purely for compatibility with the scikit-learn transformer API.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a sequence of instances to a scipy.sparse matrix.
Samples. Each sample must be iterable an (e.g., a list or tuple) containing/generating feature names (and optionally values, see the input_type constructor argument) which will be hashed. raw_X need not support the len function, so it can be the result of a generator; n_samples is determined on the fly.
Feature matrix, for use with estimators or further transformers.


Implements feature hashing, aka the hashing trick.
This class turns sequences of symbolic feature names (strings) into scipy.sparse matrices, using a hash function to compute the matrix column corresponding to a name. The hash function employed is the signed 32-bit version of Murmurhash3.
Feature names of type byte string are used as-is. Unicode strings are converted to UTF-8 first, but no Unicode normalization is done. Feature values must be (finite) numbers.
This class is a low-memory alternative to DictVectorizer and CountVectorizer, intended for large-scale (online) learning and situations where memory is tight, e.g. when running prediction code on embedded devices.
Read more in the User Guide.
New in version 0.13.
The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners.
Either “dict” (the default) to accept dictionaries over (feature_name, value); “pair” to accept pairs of (feature_name, value); or “string” to accept single strings. feature_name should be a string, while value should be a number. In the case of “string”, a value of 1 is implied. The feature_name is hashed to find the appropriate column for the feature. The value’s sign might be flipped in the output (but see non_negative, below).
The type of feature values. Passed to scipy.sparse matrix constructors as the dtype argument. Do not set this to bool, np.boolean or any unsigned integer type.
When True, an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection.
alternate_sign replaces the now deprecated non_negative parameter.
See also
Vectorizes string-valued features using a hash table.
Handles nominal/categorical features.
fit([X, y])
No-op.
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
set_params(**params)
Set the parameters of this estimator.
transform(raw_X)
Transform a sequence of instances to a scipy.sparse matrix.
No-op.
This method doesn’t do anything. It exists purely for compatibility with the scikit-learn transformer API.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a sequence of instances to a scipy.sparse matrix.
Samples. Each sample must be iterable an (e.g., a list or tuple) containing/generating feature names (and optionally values, see the input_type constructor argument) which will be hashed. raw_X need not support the len function, so it can be the result of a generator; n_samples is determined on the fly.
Feature matrix, for use with estimators or further transformers.
FeatureHasher and DictVectorizer Comparison


No-op.
This method doesn’t do anything. It exists purely for compatibility with the scikit-learn transformer API.


Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.


Transform a sequence of instances to a scipy.sparse matrix.
Samples. Each sample must be iterable an (e.g., a list or tuple) containing/generating feature names (and optionally values, see the input_type constructor argument) which will be hashed. raw_X need not support the len function, so it can be the result of a generator; n_samples is determined on the fly.
Feature matrix, for use with estimators or further transformers.


Reshape a 2D image into a collection of patches
The resulting patches are allocated in a dedicated array.
Read more in the User Guide.
The original image data. For color images, the last dimension specifies the channel: a RGB image would have n_channels=3.
The dimensions of one patch.
The maximum number of patches to extract. If max_patches is a float between 0 and 1, it is taken to be a proportion of the total number of patches.
Determines the random number generator used for random sampling when max_patches is not None. Use an int to make the randomness deterministic. See Glossary.
The collection of patches extracted from the image, where n_patches is either max_patches or the total number of patches that can be extracted.


Graph of the pixel-to-pixel connections
Edges exist if 2 voxels are connected.
Dimension in x axis
Dimension in y axis
Dimension in z axis
An optional mask of the image, to consider only part of the pixels.
The class to use to build the returned adjacency matrix.
The data of the returned sparse matrix. By default it is int
For scikit-learn versions 0.14.1 and prior, return_as=np.ndarray was handled by returning a dense np.matrix instance. Going forward, np.ndarray returns an np.ndarray, as expected.
For compatibility, user code relying on this method should wrap its calls in np.asarray to avoid type issues.


Graph of the pixel-to-pixel gradient connections
Edges are weighted with the gradient values.
Read more in the User Guide.
2D or 3D image.
An optional mask of the image, to consider only part of the pixels.
The class to use to build the returned adjacency matrix.
The data of the returned sparse matrix. By default it is the dtype of img
For scikit-learn versions 0.14.1 and prior, return_as=np.ndarray was handled by returning a dense np.matrix instance. Going forward, np.ndarray returns an np.ndarray, as expected.
For compatibility, user code relying on this method should wrap its calls in np.asarray to avoid type issues.


Extracts patches from a collection of images
Read more in the User Guide.
New in version 0.9.
The dimensions of one patch.
The maximum number of patches per image to extract. If max_patches is a float in (0, 1), it is taken to mean a proportion of the total number of patches.
Determines the random number generator used for random sampling when max_patches is not None. Use an int to make the randomness deterministic. See Glossary.
fit(X[, y])
Do nothing and return the estimator unchanged.
get_params([deep])
Get parameters for this estimator.
set_params(**params)
Set the parameters of this estimator.
transform(X)
Transforms the image samples in X into a matrix of patch data.
Do nothing and return the estimator unchanged.
This method is just there to implement the usual API and hence work in pipelines.
Training data.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transforms the image samples in X into a matrix of patch data.
Array of images from which to extract patches. For color images, the last dimension specifies the channel: a RGB image would have n_channels=3.
The collection of patches extracted from the images, where n_patches is either n_samples * max_patches or the total number of patches that can be extracted.


Do nothing and return the estimator unchanged.
This method is just there to implement the usual API and hence work in pipelines.
Training data.


Transforms the image samples in X into a matrix of patch data.
Array of images from which to extract patches. For color images, the last dimension specifies the channel: a RGB image would have n_channels=3.
The collection of patches extracted from the images, where n_patches is either n_samples * max_patches or the total number of patches that can be extracted.


Reconstruct the image from all of its patches.
Patches are assumed to overlap and the image is constructed by filling in the patches from left to right, top to bottom, averaging the overlapping regions.
Read more in the User Guide.
The complete set of patches. If the patches contain colour information, channels are indexed along the last dimension: RGB patches would have n_channels=3.
The size of the image that will be reconstructed.
The reconstructed image.


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.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (strip_accents and lowercase) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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 None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms.
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.
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.
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.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
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.
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.
If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus.
This parameter is ignored if vocabulary is not None.
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.
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.
Type of the matrix returned by fit_transform() or transform().
A mapping of terms to feature indices.
True if a fixed vocabulary of term to indices mapping is provided by the user
Terms that were ignored because they either:
This is only available if no vocabulary was given.
See also
The stop_words_ attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling.
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(raw_documents[, y])
Learn a vocabulary dictionary of all tokens in the raw documents.
fit_transform(raw_documents[, y])
Learn the vocabulary dictionary and return document-term matrix.
get_feature_names()
Array mapping from feature integer indices to feature name.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
inverse_transform(X)
Return terms per document with nonzero entries in X.
set_params(**params)
Set the parameters of this estimator.
transform(raw_documents)
Transform documents to document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Learn a vocabulary dictionary of all tokens in the raw documents.
An iterable which yields either str, unicode or file objects.
Learn the vocabulary dictionary and return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
Document-term matrix.
Array mapping from feature integer indices to feature name.
A list of feature names.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
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.
An iterable which yields either str, unicode or file objects.
Document-term matrix.


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.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (strip_accents and lowercase) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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 None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms.
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.
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.
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.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
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.
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.
If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus.
This parameter is ignored if vocabulary is not None.
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.
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.
Type of the matrix returned by fit_transform() or transform().
A mapping of terms to feature indices.
True if a fixed vocabulary of term to indices mapping is provided by the user
Terms that were ignored because they either:
This is only available if no vocabulary was given.
See also
The stop_words_ attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling.
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(raw_documents[, y])
Learn a vocabulary dictionary of all tokens in the raw documents.
fit_transform(raw_documents[, y])
Learn the vocabulary dictionary and return document-term matrix.
get_feature_names()
Array mapping from feature integer indices to feature name.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
inverse_transform(X)
Return terms per document with nonzero entries in X.
set_params(**params)
Set the parameters of this estimator.
transform(raw_documents)
Transform documents to document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Learn a vocabulary dictionary of all tokens in the raw documents.
An iterable which yields either str, unicode or file objects.
Learn the vocabulary dictionary and return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
Document-term matrix.
Array mapping from feature integer indices to feature name.
A list of feature names.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
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.
An iterable which yields either str, unicode or file objects.
Document-term matrix.
Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation
Sample pipeline for text feature extraction and evaluation
Semi-supervised Classification on a Text Dataset


Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.


Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.


Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.


Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.


Learn a vocabulary dictionary of all tokens in the raw documents.
An iterable which yields either str, unicode or file objects.


Learn the vocabulary dictionary and return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
Document-term matrix.


Array mapping from feature integer indices to feature name.
A list of feature names.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Build or fetch the effective stop words list.
A list of stop words.


Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


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.
An iterable which yields either str, unicode or file objects.
Document-term matrix.


Convert a collection of text documents to a matrix of token occurrences
It turns a collection of text documents into a scipy.sparse matrix holding token occurrence counts (or binary occurrence information), possibly normalized as token frequencies if norm=’l1’ or projected on the euclidean unit sphere if norm=’l2’.
This text vectorizer implementation uses the hashing trick to find the token string name to feature integer index mapping.
This strategy has several advantages:
There are also a couple of cons (vs using a CountVectorizer with an in-memory vocabulary):
The hash function employed is the signed 32-bit version of Murmurhash3.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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'.
Regular expression denoting what constitutes a “token”, only used if analyzer == 'word'. The default regexp selects 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.
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. Only applies if analyzer is not callable.
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.
If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners.
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.
Norm used to normalize term vectors. None for no normalization.
When True, an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection.
New in version 0.19.
Type of the matrix returned by fit_transform() or transform().
See also
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(X[, y])
Does nothing: this transformer is stateless.
fit_transform(X[, y])
Transform a sequence of documents to a document-term matrix.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
partial_fit(X[, y])
Does nothing: this transformer is stateless.
set_params(**params)
Set the parameters of this estimator.
transform(X)
Transform a sequence of documents to a document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Does nothing: this transformer is stateless.
Training data.
Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Ignored. This parameter exists only for compatibility with sklearn.pipeline.Pipeline.
Document-term matrix.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Does nothing: this transformer is stateless.
This method is just there to mark the fact that this transformer can work in a streaming setup.
Training data.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Document-term matrix.


Convert a collection of text documents to a matrix of token occurrences
It turns a collection of text documents into a scipy.sparse matrix holding token occurrence counts (or binary occurrence information), possibly normalized as token frequencies if norm=’l1’ or projected on the euclidean unit sphere if norm=’l2’.
This text vectorizer implementation uses the hashing trick to find the token string name to feature integer index mapping.
This strategy has several advantages:
There are also a couple of cons (vs using a CountVectorizer with an in-memory vocabulary):
The hash function employed is the signed 32-bit version of Murmurhash3.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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'.
Regular expression denoting what constitutes a “token”, only used if analyzer == 'word'. The default regexp selects 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.
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. Only applies if analyzer is not callable.
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.
If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
The number of features (columns) in the output matrices. Small numbers of features are likely to cause hash collisions, but large numbers will cause larger coefficient dimensions in linear learners.
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.
Norm used to normalize term vectors. None for no normalization.
When True, an alternating sign is added to the features as to approximately conserve the inner product in the hashed space even for small n_features. This approach is similar to sparse random projection.
New in version 0.19.
Type of the matrix returned by fit_transform() or transform().
See also
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(X[, y])
Does nothing: this transformer is stateless.
fit_transform(X[, y])
Transform a sequence of documents to a document-term matrix.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
partial_fit(X[, y])
Does nothing: this transformer is stateless.
set_params(**params)
Set the parameters of this estimator.
transform(X)
Transform a sequence of documents to a document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Does nothing: this transformer is stateless.
Training data.
Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Ignored. This parameter exists only for compatibility with sklearn.pipeline.Pipeline.
Document-term matrix.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Does nothing: this transformer is stateless.
This method is just there to mark the fact that this transformer can work in a streaming setup.
Training data.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Document-term matrix.
Out-of-core classification of text documents
Clustering text documents using k-means
Classification of text documents using sparse features


Does nothing: this transformer is stateless.
Training data.


Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Ignored. This parameter exists only for compatibility with sklearn.pipeline.Pipeline.
Document-term matrix.


Does nothing: this transformer is stateless.
This method is just there to mark the fact that this transformer can work in a streaming setup.
Training data.


Transform a sequence of documents to a document-term matrix.
Samples. Each sample must be a text document (either bytes or unicode strings, file name or file object depending on the constructor argument) which will be tokenized and hashed.
Document-term matrix.


Transform a count matrix to a normalized tf or tf-idf representation
Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification.
The goal of using tf-idf instead of the raw frequencies of occurrence of a token in a given document is to scale down the impact of tokens that occur very frequently in a given corpus and that are hence empirically less informative than features that occur in a small fraction of the training corpus.
The formula that is used to compute the tf-idf for a term t of a document d in a document set is tf-idf(t, d) = tf(t, d) * idf(t), and the idf is computed as idf(t) = log [ n / df(t) ] + 1 (if smooth_idf=False), where n is the total number of documents in the document set and df(t) is the document frequency of t; the document frequency is the number of documents in the document set that contain the term t. The effect of adding “1” to the idf in the equation above is that terms with zero idf, i.e., terms that occur in all documents in a training set, will not be entirely ignored. (Note that the idf formula above differs from the standard textbook notation that defines the idf as idf(t) = log [ n / (df(t) + 1) ]).
If smooth_idf=True (the default), the constant “1” is added to the numerator and denominator of the idf as if an extra document was seen containing every term in the collection exactly once, which prevents zero divisions: idf(t) = log [ (1 + n) / (1 + df(t)) ] + 1.
Furthermore, the formulas used to compute tf and idf depend on parameter settings that correspond to the SMART notation used in IR as follows:
Tf is “n” (natural) by default, “l” (logarithmic) when sublinear_tf=True. Idf is “t” when use_idf is given, “n” (none) otherwise. Normalization is “c” (cosine) when norm='l2', “n” (none) when norm=None.
Read more in the User Guide.
Each output row will have unit norm, either: * ‘l2’: Sum of squares of vector elements is 1. The cosine similarity between two vectors is their dot product when l2 norm has been applied. * ‘l1’: Sum of absolute values of vector elements is 1. See preprocessing.normalize
Enable inverse-document-frequency reweighting.
Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions.
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
The inverse document frequency (IDF) vector; only defined if use_idf is True.
New in version 0.20.
R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.
C.D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.
fit(X[, y])
Learn the idf vector (global term weights).
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
set_params(**params)
Set the parameters of this estimator.
transform(X[, copy])
Transform a count matrix to a tf or tf-idf representation
Learn the idf vector (global term weights).
A matrix of term/token counts.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a count matrix to a tf or tf-idf representation
a matrix of term/token counts
Whether to copy X and operate on the copy or perform in-place operations.


Transform a count matrix to a normalized tf or tf-idf representation
Tf means term-frequency while tf-idf means term-frequency times inverse document-frequency. This is a common term weighting scheme in information retrieval, that has also found good use in document classification.
The goal of using tf-idf instead of the raw frequencies of occurrence of a token in a given document is to scale down the impact of tokens that occur very frequently in a given corpus and that are hence empirically less informative than features that occur in a small fraction of the training corpus.
The formula that is used to compute the tf-idf for a term t of a document d in a document set is tf-idf(t, d) = tf(t, d) * idf(t), and the idf is computed as idf(t) = log [ n / df(t) ] + 1 (if smooth_idf=False), where n is the total number of documents in the document set and df(t) is the document frequency of t; the document frequency is the number of documents in the document set that contain the term t. The effect of adding “1” to the idf in the equation above is that terms with zero idf, i.e., terms that occur in all documents in a training set, will not be entirely ignored. (Note that the idf formula above differs from the standard textbook notation that defines the idf as idf(t) = log [ n / (df(t) + 1) ]).
If smooth_idf=True (the default), the constant “1” is added to the numerator and denominator of the idf as if an extra document was seen containing every term in the collection exactly once, which prevents zero divisions: idf(t) = log [ (1 + n) / (1 + df(t)) ] + 1.
Furthermore, the formulas used to compute tf and idf depend on parameter settings that correspond to the SMART notation used in IR as follows:
Tf is “n” (natural) by default, “l” (logarithmic) when sublinear_tf=True. Idf is “t” when use_idf is given, “n” (none) otherwise. Normalization is “c” (cosine) when norm='l2', “n” (none) when norm=None.
Read more in the User Guide.
Each output row will have unit norm, either: * ‘l2’: Sum of squares of vector elements is 1. The cosine similarity between two vectors is their dot product when l2 norm has been applied. * ‘l1’: Sum of absolute values of vector elements is 1. See preprocessing.normalize
Enable inverse-document-frequency reweighting.
Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions.
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
The inverse document frequency (IDF) vector; only defined if use_idf is True.
New in version 0.20.
R. Baeza-Yates and B. Ribeiro-Neto (2011). Modern Information Retrieval. Addison Wesley, pp. 68-74.
C.D. Manning, P. Raghavan and H. Schütze (2008). Introduction to Information Retrieval. Cambridge University Press, pp. 118-120.
fit(X[, y])
Learn the idf vector (global term weights).
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
set_params(**params)
Set the parameters of this estimator.
transform(X[, copy])
Transform a count matrix to a tf or tf-idf representation
Learn the idf vector (global term weights).
A matrix of term/token counts.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform a count matrix to a tf or tf-idf representation
a matrix of term/token counts
Whether to copy X and operate on the copy or perform in-place operations.
Sample pipeline for text feature extraction and evaluation
Semi-supervised Classification on a Text Dataset
Clustering text documents using k-means


Learn the idf vector (global term weights).
A matrix of term/token counts.


Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.


Transform a count matrix to a tf or tf-idf representation
a matrix of term/token counts
Whether to copy X and operate on the copy or perform in-place operations.


Convert a collection of raw documents to a matrix of TF-IDF features.
Equivalent to CountVectorizer followed by TfidfTransformer.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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.
If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
If a string, it is passed to _check_stop_list and the appropriate stop list is returned. ‘english’ is currently the only supported string value. 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 None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms.
Regular expression denoting what constitutes a “token”, only used if analyzer == 'word'. The default regexp selects 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.
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. Only applies if analyzer is not callable.
When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float in range [0.0, 1.0], the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
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 in range of [0.0, 1.0], the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus.
This parameter is ignored if vocabulary is not None.
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.
If True, all non-zero term counts are set to 1. This does not mean outputs will have only 0/1 values, only that the tf term in tf-idf is binary. (Set idf and normalization to False to get 0/1 outputs).
Type of the matrix returned by fit_transform() or transform().
Each output row will have unit norm, either: * ‘l2’: Sum of squares of vector elements is 1. The cosine similarity between two vectors is their dot product when l2 norm has been applied. * ‘l1’: Sum of absolute values of vector elements is 1. See preprocessing.normalize.
Enable inverse-document-frequency reweighting.
Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions.
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
A mapping of terms to feature indices.
True if a fixed vocabulary of term to indices mapping is provided by the user
The inverse document frequency (IDF) vector; only defined if use_idf is True.
Terms that were ignored because they either:
This is only available if no vocabulary was given.
See also
Transforms text into a sparse matrix of n-gram counts.
Performs the TF-IDF transformation from a provided matrix of counts.
The stop_words_ attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling.
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(raw_documents[, y])
Learn vocabulary and idf from training set.
fit_transform(raw_documents[, y])
Learn vocabulary and idf, return document-term matrix.
get_feature_names()
Array mapping from feature integer indices to feature name.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
inverse_transform(X)
Return terms per document with nonzero entries in X.
set_params(**params)
Set the parameters of this estimator.
transform(raw_documents)
Transform documents to document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Learn vocabulary and idf from training set.
An iterable which yields either str, unicode or file objects.
This parameter is not needed to compute tfidf.
Fitted vectorizer.
Learn vocabulary and idf, return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
This parameter is ignored.
Tf-idf-weighted document-term matrix.
Array mapping from feature integer indices to feature name.
A list of feature names.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform documents to document-term matrix.
Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform).
An iterable which yields either str, unicode or file objects.
Tf-idf-weighted document-term matrix.


Convert a collection of raw documents to a matrix of TF-IDF features.
Equivalent to CountVectorizer followed by TfidfTransformer.
Read more in the User Guide.
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.
If ‘file’, the sequence items must have a ‘read’ method (file-like object) that is called to fetch the bytes in memory.
Otherwise the input is expected to be a sequence of items that can be of type string or byte.
If bytes or files are given to analyze, this encoding is used to decode.
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’.
Remove accents and perform other character normalization during the preprocessing step. ‘ascii’ is a fast method that only works on characters that have an direct ASCII mapping. ‘unicode’ is a slightly slower method that works on any characters. None (default) does nothing.
Both ‘ascii’ and ‘unicode’ use NFKD normalization from unicodedata.normalize.
Convert all characters to lowercase before tokenizing.
Override the preprocessing (string transformation) stage while preserving the tokenizing and n-grams generation steps. Only applies if analyzer is not callable.
Override the string tokenization step while preserving the preprocessing and n-grams generation steps. Only applies if analyzer == 'word'.
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.
If a callable is passed it is used to extract the sequence of features out of the raw, unprocessed input.
Changed in version 0.21.
Since v0.21, if input is filename or file, the data is first read from the file and then passed to the given callable analyzer.
If a string, it is passed to _check_stop_list and the appropriate stop list is returned. ‘english’ is currently the only supported string value. 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 None, no stop words will be used. max_df can be set to a value in the range [0.7, 1.0) to automatically detect and filter stop words based on intra corpus document frequency of terms.
Regular expression denoting what constitutes a “token”, only used if analyzer == 'word'. The default regexp selects 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.
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. Only applies if analyzer is not callable.
When building the vocabulary ignore terms that have a document frequency strictly higher than the given threshold (corpus-specific stop words). If float in range [0.0, 1.0], the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
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 in range of [0.0, 1.0], the parameter represents a proportion of documents, integer absolute counts. This parameter is ignored if vocabulary is not None.
If not None, build a vocabulary that only consider the top max_features ordered by term frequency across the corpus.
This parameter is ignored if vocabulary is not None.
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.
If True, all non-zero term counts are set to 1. This does not mean outputs will have only 0/1 values, only that the tf term in tf-idf is binary. (Set idf and normalization to False to get 0/1 outputs).
Type of the matrix returned by fit_transform() or transform().
Each output row will have unit norm, either: * ‘l2’: Sum of squares of vector elements is 1. The cosine similarity between two vectors is their dot product when l2 norm has been applied. * ‘l1’: Sum of absolute values of vector elements is 1. See preprocessing.normalize.
Enable inverse-document-frequency reweighting.
Smooth idf weights by adding one to document frequencies, as if an extra document was seen containing every term in the collection exactly once. Prevents zero divisions.
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
A mapping of terms to feature indices.
True if a fixed vocabulary of term to indices mapping is provided by the user
The inverse document frequency (IDF) vector; only defined if use_idf is True.
Terms that were ignored because they either:
This is only available if no vocabulary was given.
See also
Transforms text into a sparse matrix of n-gram counts.
Performs the TF-IDF transformation from a provided matrix of counts.
The stop_words_ attribute can get large and increase the model size when pickling. This attribute is provided only for introspection and can be safely removed using delattr or set to None before pickling.
build_analyzer()
Return a callable that handles preprocessing, tokenization and n-grams generation.
build_preprocessor()
Return a function to preprocess the text before tokenization.
build_tokenizer()
Return a function that splits a string into a sequence of tokens.
decode(doc)
Decode the input into a string of unicode symbols.
fit(raw_documents[, y])
Learn vocabulary and idf from training set.
fit_transform(raw_documents[, y])
Learn vocabulary and idf, return document-term matrix.
get_feature_names()
Array mapping from feature integer indices to feature name.
get_params([deep])
Get parameters for this estimator.
get_stop_words()
Build or fetch the effective stop words list.
inverse_transform(X)
Return terms per document with nonzero entries in X.
set_params(**params)
Set the parameters of this estimator.
transform(raw_documents)
Transform documents to document-term matrix.
Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.
Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.
Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.
Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.
Learn vocabulary and idf from training set.
An iterable which yields either str, unicode or file objects.
This parameter is not needed to compute tfidf.
Fitted vectorizer.
Learn vocabulary and idf, return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
This parameter is ignored.
Tf-idf-weighted document-term matrix.
Array mapping from feature integer indices to feature name.
A list of feature names.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Build or fetch the effective stop words list.
A list of stop words.
Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Transform documents to document-term matrix.
Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform).
An iterable which yields either str, unicode or file objects.
Tf-idf-weighted document-term matrix.
Biclustering documents with the Spectral Co-clustering algorithm
Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation
Column Transformer with Heterogeneous Data Sources
Clustering text documents using k-means
Classification of text documents using sparse features


Return a callable that handles preprocessing, tokenization and n-grams generation.
A function to handle preprocessing, tokenization and n-grams generation.


Return a function to preprocess the text before tokenization.
A function to preprocess the text before tokenization.


Return a function that splits a string into a sequence of tokens.
A function to split a string into a sequence of tokens.


Decode the input into a string of unicode symbols.
The decoding strategy depends on the vectorizer parameters.
The string to decode.
A string of unicode symbols.


Learn vocabulary and idf from training set.
An iterable which yields either str, unicode or file objects.
This parameter is not needed to compute tfidf.
Fitted vectorizer.


Learn vocabulary and idf, return document-term matrix.
This is equivalent to fit followed by transform, but more efficiently implemented.
An iterable which yields either str, unicode or file objects.
This parameter is ignored.
Tf-idf-weighted document-term matrix.


Array mapping from feature integer indices to feature name.
A list of feature names.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


Build or fetch the effective stop words list.
A list of stop words.


Return terms per document with nonzero entries in X.
Document-term matrix.
List of arrays of terms.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.


Transform documents to document-term matrix.
Uses the vocabulary and document frequencies (df) learned by fit (or fit_transform).
An iterable which yields either str, unicode or file objects.
Tf-idf-weighted document-term matrix.


Compute chi-squared stats between each non-negative feature and class.
This score can be used to select the n_features features with the highest values for the test chi-squared statistic from X, which must contain only non-negative features such as booleans or frequencies (e.g., term counts in document classification), relative to the classes.
Recall that the chi-square test measures dependence between stochastic variables, so using this function “weeds out” the features that are the most likely to be independent of class and therefore irrelevant for classification.
Read more in the User Guide.
Sample vectors.
Target vector (class labels).
chi2 statistics of each feature.
p-values of each feature.
See also
ANOVA F-value between label/feature for classification tasks.
F-value between label/feature for regression tasks.
Complexity of this algorithm is O(n_classes * n_features).


Compute the ANOVA F-value for the provided sample.
Read more in the User Guide.
The set of regressors that will be tested sequentially.
The data matrix.
The set of F values.
The set of p-values.
See also
Chi-squared stats of non-negative features for classification tasks.
F-value between label/feature for regression tasks.


Univariate linear regression tests.
Linear model for testing the individual effect of each of many regressors. This is a scoring function to be used in a feature selection procedure, not a free standing feature selection procedure.
This is done in 2 steps:
For more on usage see the User Guide.
The set of regressors that will be tested sequentially.
The data matrix
If true, X and y will be centered.
F values of features.
p-values of F-scores.
See also
Mutual information for a continuous target.
ANOVA F-value between label/feature for classification tasks.
Chi-squared stats of non-negative features for classification tasks.
Select features based on the k highest scores.
Select features based on a false positive rate test.
Select features based on an estimated false discovery rate.
Select features based on family-wise error rate.
Select features based on percentile of the highest scores.


Univariate feature selector with configurable strategy.
Read more in the User Guide.
Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues). For modes ‘percentile’ or ‘kbest’ it can return a single array scores.
Feature selection mode.
Parameter of the corresponding mode.
Scores of features.
p-values of feature scores, None if score_func returned scores only.
See also
ANOVA F-value between label/feature for classification tasks.
Mutual information for a discrete target.
Chi-squared stats of non-negative features for classification tasks.
F-value between label/feature for regression tasks.
Mutual information for a continuous target.
Select features based on percentile of the highest scores.
Select features based on the k highest scores.
Select features based on a false positive rate test.
Select features based on an estimated false discovery rate.
Select features based on family-wise error rate.
fit(X, y)
Run score function on (X, y) and get the appropriate features.
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
get_support([indices])
Get a mask, or integer index, of the features selected
inverse_transform(X)
Reverse the transformation operation
set_params(**params)
Set the parameters of this estimator.
transform(X)
Reduce X to the selected features.
Run score function on (X, y) and get the appropriate features.
The training input samples.
The target values (class labels in classification, real numbers in regression).
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Get a mask, or integer index, of the features selected
If True, the return value will be an array of integers, rather than a boolean mask.
An index that selects the retained features from a feature vector. If indices is False, this is a boolean array of shape [# input features], in which an element is True iff its corresponding feature is selected for retention. If indices is True, this is an integer array of shape [# output features] whose values are indices into the input feature vector.
Reverse the transformation operation
The input samples.
X with columns of zeros inserted where features would have been removed by transform.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Reduce X to the selected features.
The input samples.
The input samples with only the selected features.


Run score function on (X, y) and get the appropriate features.
The training input samples.
The target values (class labels in classification, real numbers in regression).


Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.


Get a mask, or integer index, of the features selected
If True, the return value will be an array of integers, rather than a boolean mask.
An index that selects the retained features from a feature vector. If indices is False, this is a boolean array of shape [# input features], in which an element is True iff its corresponding feature is selected for retention. If indices is True, this is an integer array of shape [# output features] whose values are indices into the input feature vector.


Reverse the transformation operation
The input samples.
X with columns of zeros inserted where features would have been removed by transform.


Reduce X to the selected features.
The input samples.
The input samples with only the selected features.


Estimate mutual information for a discrete target variable.
Mutual information (MI) [1] between two random variables is a non-negative value, which measures the dependency between the variables. It is equal to zero if and only if two random variables are independent, and higher values mean higher dependency.
The function relies on nonparametric methods based on entropy estimation from k-nearest neighbors distances as described in [2] and [3]. Both methods are based on the idea originally proposed in [4].
It can be used for univariate features selection, read more in the User Guide.
Feature matrix.
Target vector.
If bool, then determines whether to consider all features discrete or continuous. If array, then it should be either a boolean mask with shape (n_features,) or array with indices of discrete features. If ‘auto’, it is assigned to False for dense X and to True for sparse X.
Number of neighbors to use for MI estimation for continuous variables, see [2] and [3]. Higher values reduce variance of the estimation, but could introduce a bias.
Whether to make a copy of the given data. If set to False, the initial data will be overwritten.
Determines random number generation for adding small noise to continuous variables in order to remove repeated values. Pass an int for reproducible results across multiple function calls. See Glossary.
Estimated mutual information between each feature and the target.
Mutual Information on Wikipedia.
A. Kraskov, H. Stogbauer and P. Grassberger, “Estimating mutual information”. Phys. Rev. E 69, 2004.
B. C. Ross “Mutual Information between Discrete and Continuous Data Sets”. PLoS ONE 9(2), 2014.
L. F. Kozachenko, N. N. Leonenko, “Sample Estimate of the Entropy of a Random Vector:, Probl. Peredachi Inf., 23:2 (1987), 9-16


Estimate mutual information for a continuous target variable.
Mutual information (MI) [1] between two random variables is a non-negative value, which measures the dependency between the variables. It is equal to zero if and only if two random variables are independent, and higher values mean higher dependency.
The function relies on nonparametric methods based on entropy estimation from k-nearest neighbors distances as described in [2] and [3]. Both methods are based on the idea originally proposed in [4].
It can be used for univariate features selection, read more in the User Guide.
Feature matrix.
Target vector.
If bool, then determines whether to consider all features discrete or continuous. If array, then it should be either a boolean mask with shape (n_features,) or array with indices of discrete features. If ‘auto’, it is assigned to False for dense X and to True for sparse X.
Number of neighbors to use for MI estimation for continuous variables, see [2] and [3]. Higher values reduce variance of the estimation, but could introduce a bias.
Whether to make a copy of the given data. If set to False, the initial data will be overwritten.
Determines random number generation for adding small noise to continuous variables in order to remove repeated values. Pass an int for reproducible results across multiple function calls. See Glossary.
Estimated mutual information between each feature and the target.
Mutual Information on Wikipedia.
A. Kraskov, H. Stogbauer and P. Grassberger, “Estimating mutual information”. Phys. Rev. E 69, 2004.
B. C. Ross “Mutual Information between Discrete and Continuous Data Sets”. PLoS ONE 9(2), 2014.
L. F. Kozachenko, N. N. Leonenko, “Sample Estimate of the Entropy of a Random Vector”, Probl. Peredachi Inf., 23:2 (1987), 9-16


Feature ranking with recursive feature elimination.
Given an external estimator that assigns weights to features (e.g., the coefficients of a linear model), the goal of recursive feature elimination (RFE) is to select features by recursively considering smaller and smaller sets of features. First, the estimator is trained on the initial set of features and the importance of each feature is obtained either through any specific attribute or callable. Then, the least important features are pruned from current set of features. That procedure is recursively repeated on the pruned set until the desired number of features to select is eventually reached.
Read more in the User Guide.
A supervised learning estimator with a fit method that provides information about feature importance (e.g. coef_, feature_importances_).
The number of features to select. If None, half of the features are selected. If integer, the parameter is the absolute number of features to select. If float between 0 and 1, it is the fraction of features to select.
Changed in version 0.24: Added float values for fractions.
If greater than or equal to 1, then step corresponds to the (integer) number of features to remove at each iteration. If within (0.0, 1.0), then step corresponds to the percentage (rounded down) of features to remove at each iteration.
Controls verbosity of output.
If ‘auto’, uses the feature importance either through a coef_ or feature_importances_ attributes of estimator.
Also accepts a string that specifies an attribute name/path for extracting feature importance (implemented with attrgetter). For example, give regressor_.coef_ in case of TransformedTargetRegressor or named_steps.clf.feature_importances_ in case of class:~sklearn.pipeline.Pipeline with its last step named clf.
If callable, overrides the default feature importance getter. The callable is passed with the fitted estimator and it should return importance for each feature.
New in version 0.24.
The fitted estimator used to select features.
The number of selected features.
The feature ranking, such that ranking_[i] corresponds to the ranking position of the i-th feature. Selected (i.e., estimated best) features are assigned rank 1.
The mask of selected features.
See also
Recursive feature elimination with built-in cross-validated selection of the best number of features.
Feature selection based on thresholds of importance weights.
Sequential cross-validation based feature selection. Does not rely on importance weights.
Allows NaN/Inf in the input if the underlying estimator does as well.
Guyon, I., Weston, J., Barnhill, S., & Vapnik, V., “Gene selection for cancer classification using support vector machines”, Mach. Learn., 46(1-3), 389–422, 2002.
The following example shows how to retrieve the 5 most informative features in the Friedman #1 dataset.
decision_function(X)
Compute the decision function of X.
fit(X, y)
Fit the RFE model and then the underlying estimator on the selected
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
get_support([indices])
Get a mask, or integer index, of the features selected
inverse_transform(X)
Reverse the transformation operation
predict(X)
Reduce X to the selected features and then predict using the
predict_log_proba(X)
Predict class log-probabilities for X.
predict_proba(X)
Predict class probabilities for X.
score(X, y)
Reduce X to the selected features and then return the score of the
set_params(**params)
Set the parameters of this estimator.
transform(X)
Reduce X to the selected features.
Compute the decision function of X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The decision function of the input samples. The order of the classes corresponds to that in the attribute classes_. Regression and binary classification produce an array of shape [n_samples].
features.
The training input samples.
The target values.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Get a mask, or integer index, of the features selected
If True, the return value will be an array of integers, rather than a boolean mask.
An index that selects the retained features from a feature vector. If indices is False, this is a boolean array of shape [# input features], in which an element is True iff its corresponding feature is selected for retention. If indices is True, this is an integer array of shape [# output features] whose values are indices into the input feature vector.
Reverse the transformation operation
The input samples.
X with columns of zeros inserted where features would have been removed by transform.
underlying estimator.
The input samples.
The predicted target values.
Predict class log-probabilities for X.
The input samples.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Predict class probabilities for X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
underlying estimator.
The input samples.
The target values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Reduce X to the selected features.
The input samples.
The input samples with only the selected features.


Feature ranking with recursive feature elimination.
Given an external estimator that assigns weights to features (e.g., the coefficients of a linear model), the goal of recursive feature elimination (RFE) is to select features by recursively considering smaller and smaller sets of features. First, the estimator is trained on the initial set of features and the importance of each feature is obtained either through any specific attribute or callable. Then, the least important features are pruned from current set of features. That procedure is recursively repeated on the pruned set until the desired number of features to select is eventually reached.
Read more in the User Guide.
A supervised learning estimator with a fit method that provides information about feature importance (e.g. coef_, feature_importances_).
The number of features to select. If None, half of the features are selected. If integer, the parameter is the absolute number of features to select. If float between 0 and 1, it is the fraction of features to select.
Changed in version 0.24: Added float values for fractions.
If greater than or equal to 1, then step corresponds to the (integer) number of features to remove at each iteration. If within (0.0, 1.0), then step corresponds to the percentage (rounded down) of features to remove at each iteration.
Controls verbosity of output.
If ‘auto’, uses the feature importance either through a coef_ or feature_importances_ attributes of estimator.
Also accepts a string that specifies an attribute name/path for extracting feature importance (implemented with attrgetter). For example, give regressor_.coef_ in case of TransformedTargetRegressor or named_steps.clf.feature_importances_ in case of class:~sklearn.pipeline.Pipeline with its last step named clf.
If callable, overrides the default feature importance getter. The callable is passed with the fitted estimator and it should return importance for each feature.
New in version 0.24.
The fitted estimator used to select features.
The number of selected features.
The feature ranking, such that ranking_[i] corresponds to the ranking position of the i-th feature. Selected (i.e., estimated best) features are assigned rank 1.
The mask of selected features.
See also
Recursive feature elimination with built-in cross-validated selection of the best number of features.
Feature selection based on thresholds of importance weights.
Sequential cross-validation based feature selection. Does not rely on importance weights.
Allows NaN/Inf in the input if the underlying estimator does as well.
Guyon, I., Weston, J., Barnhill, S., & Vapnik, V., “Gene selection for cancer classification using support vector machines”, Mach. Learn., 46(1-3), 389–422, 2002.
The following example shows how to retrieve the 5 most informative features in the Friedman #1 dataset.
decision_function(X)
Compute the decision function of X.
fit(X, y)
Fit the RFE model and then the underlying estimator on the selected
fit_transform(X[, y])
Fit to data, then transform it.
get_params([deep])
Get parameters for this estimator.
get_support([indices])
Get a mask, or integer index, of the features selected
inverse_transform(X)
Reverse the transformation operation
predict(X)
Reduce X to the selected features and then predict using the
predict_log_proba(X)
Predict class log-probabilities for X.
predict_proba(X)
Predict class probabilities for X.
score(X, y)
Reduce X to the selected features and then return the score of the
set_params(**params)
Set the parameters of this estimator.
transform(X)
Reduce X to the selected features.
Compute the decision function of X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The decision function of the input samples. The order of the classes corresponds to that in the attribute classes_. Regression and binary classification produce an array of shape [n_samples].
features.
The training input samples.
The target values.
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
Input samples.
Target values (None for unsupervised transformations).
Additional fit parameters.
Transformed array.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Get a mask, or integer index, of the features selected
If True, the return value will be an array of integers, rather than a boolean mask.
An index that selects the retained features from a feature vector. If indices is False, this is a boolean array of shape [# input features], in which an element is True iff its corresponding feature is selected for retention. If indices is True, this is an integer array of shape [# output features] whose values are indices into the input feature vector.
Reverse the transformation operation
The input samples.
X with columns of zeros inserted where features would have been removed by transform.
underlying estimator.
The input samples.
The predicted target values.
Predict class log-probabilities for X.
The input samples.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
Predict class probabilities for X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.
underlying estimator.
The input samples.
The target values.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Reduce X to the selected features.
The input samples.
The input samples with only the selected features.
Recursive feature elimination


Compute the decision function of X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The decision function of the input samples. The order of the classes corresponds to that in the attribute classes_. Regression and binary classification produce an array of shape [n_samples].


features.
The training input samples.
The target values.


Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.


underlying estimator.
The input samples.
The predicted target values.


Predict class log-probabilities for X.
The input samples.
The class log-probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


Predict class probabilities for X.
The input samples. Internally, it will be converted to dtype=np.float32 and if a sparse matrix is provided to a sparse csr_matrix.
The class probabilities of the input samples. The order of the classes corresponds to that in the attribute classes_.


underlying estimator.
The input samples.
The target values.


Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
Estimator parameters.
Estimator instance.