TfidfTransformer#
- class cuml.feature_extraction.text.TfidfTransformer(
- *,
- norm='l2',
- use_idf=True,
- smooth_idf=True,
- sublinear_tf=False,
- verbose=False,
- output_type=None,
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) whennorm='l2', “n” (none) whennorm=None.- Parameters:
- norm{‘l1’, ‘l2’, None}, default=’l2’
Norm used to normalize term vectors. None for no normalization.
- use_idfbool, default=True
Enable inverse-document-frequency reweighting.
- smooth_idfbool, default=True
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.
- sublinear_tfbool, default=False
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
- verboseint or boolean, default=False
Sets logging level. It must be one of
cuml.common.logger.level_*. See Verbosity Levels for more info.- output_type{None, ‘input’, ‘cupy’, ‘numpy’, ‘cudf’, ‘pandas’}, default=None
Return results and set estimator attributes to the indicated output type. If None, the output type set at the module level (
cuml.global_settings.output_type) will be used. See Output Data Type Configuration for more info.
- Attributes:
- idf_array of shape (n_features)
The inverse document frequency (IDF) vector; only defined if
use_idfis True.
Methods
fit(X[, y])Fit the transformer.
fit_transform(X[, y, copy])Fit the transformer, then transform X.
transform(X[, copy])Transform a count matrix to tf or tf-idf representation.
Examples
>>> from cuml.feature_extraction.text import TfidfTransformer >>> from cuml.feature_extraction.text import CountVectorizer >>> from sklearn.pipeline import Pipeline >>> corpus = ['this is the first document', ... 'this document is the second document', ... 'and this is the third one', ... 'is this the first document'] >>> pipe = Pipeline([('count', CountVectorizer()), ... ('tfid', TfidfTransformer())]) >>> X = pipe.fit_transform(corpus) >>> X.shape (4, 9)
- fit(X, y=None)[source]#
Fit the transformer.
- Parameters:
- Xsparse matrix of shape (n_samples, n_features)
A matrix of term/token counts.
- yNone
Ignored. Exists for API compatibility only.
- Returns:
- selfobject
The instance itself.
- fit_transform(X, y=None, copy=True)[source]#
Fit the transformer, then transform X.
- Parameters:
- Xsparse matrix of shape (n_samples, n_features)
A matrix of term/token counts.
- yNone
Ignored. Exists for API compatibility only.
- copybool, default=True
If
copy=False,thenfit_transformmay choose to mutateXin-place if that would be more efficient.
- Returns:
- Xsparse matrix of shape (n_samples, n_features)
Tf-idf weighted document-term matrix.
- get_feature_names_out(input_features=None)[source]#
Get output feature names for transformation.
- Parameters:
- input_featuresarray-like of str or None, default=None
Input features.
If
input_featuresisNone, thenfeature_names_in_is used as feature names in. Iffeature_names_in_is not defined, then the following input feature names are generated:["x0", "x1", ..., "x(n_features_in_ - 1)"].If
input_featuresis an array-like, theninput_featuresmust matchfeature_names_in_iffeature_names_in_is defined.
- Returns:
- feature_names_outndarray of str objects
Same as input features.
- get_params(deep=True)[source]#
Returns a dict of all params owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is there in this method, then it doesn’t have to override this method
- set_params(**params)[source]#
Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is, there in this method, then it doesn’t have to override this method
- transform(X, copy=True)[source]#
Transform a count matrix to tf or tf-idf representation.
- Parameters:
- Xsparse matrix of shape (n_samples, n_features)
A matrix of term/token counts.
- copybool, default=True
If
copy=False,thenfit_transformmay choose to mutateXin-place if that would be more efficient.
- Returns:
- Xsparse matrix of shape (n_samples, n_features)
Tf-idf weighted document-term matrix.