asreview.models.feature_extractors.Tfidf#
- class asreview.models.feature_extractors.Tfidf(columns=['title', 'abstract'], sep=' ', lowercase=True, stop_words=None, token_pattern='(?u)\\b\\w\\w+\\b', ngram_range=(1, 1), max_df=1.0, min_df=1, max_features=None, vocabulary=None, binary=False, norm='l2', use_idf=True, smooth_idf=True, sublinear_tf=False, **kwargs)[source]#
Bases:
Pipeline
TF-IDF feature extraction.
Based on the sklearn implementation of the TF-IDF feature extraction sklearn.feature_extraction.text.TfidfVectorizer.
- Parameters:
columns (list, default=["title", "abstract"]) – See TextMerger
sep (str, default=" ") – See TextMerger
lowercase (bool, default=True) – See ScikitLearn CountVectorizer
stop_words ({'english'} or list or None, default=None) – See ScikitLearn CountVectorizer
token_pattern (str or None, default=r"(?u)bww+b") – See ScikitLearn CountVectorizer
ngram_range (tuple (min_n, max_n), default=(1,1)) – See ScikitLearn CountVectorizer
max_df (float in range [0.0, 1.0] or int, default=1.0) – See ScikitLearn CountVectorizer
min_df (float in range [0.0, 1.0] or int, default=1) – See ScikitLearn CountVectorizer
max_features (int, default=None) – See ScikitLearn CountVectorizer
vocabulary (Mapping or iterable, default=None) – See ScikitLearn CountVectorizer
binary (bool, default=False) – See ScikitLearn CountVectorizer
norm ({"l1", "l2"} or None, default="l2") – See ScikitLearn CountVectorizer
use_idf (bool, default=True) – See ScikitLearn CountVectorizer
smooth_idf (bool, default=True) – See ScikitLearn CountVectorizer
sublinear_tf (bool, default=False) – See ScikitLearn CountVectorizer
**kwargs (dict) – See ScikitLearn CountVectorizer for additional parameters
Methods
__init__
([columns, sep, lowercase, ...])decision_function
(X, **params)Transform the data, and apply decision_function with the final estimator.
fit
(X[, y])Fit the model.
fit_predict
(X[, y])Transform the data, and apply fit_predict with the final estimator.
fit_transform
(X[, y])Fit the model and transform with the final estimator.
get_feature_names_out
([input_features])Get output feature names for transformation.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
inverse_transform
([X, Xt])Apply inverse_transform for each step in a reverse order.
predict
(X, **params)Transform the data, and apply predict with the final estimator.
predict_log_proba
(X, **params)Transform the data, and apply predict_log_proba with the final estimator.
predict_proba
(X, **params)Transform the data, and apply predict_proba with the final estimator.
score
(X[, y, sample_weight])Transform the data, and apply score with the final estimator.
Transform the data, and apply score_samples with the final estimator.
set_output
(*[, transform])Set the output container when "transform" and "fit_transform" are called.
set_params
(**kwargs)Set the parameters of this estimator.
set_score_request
(*[, sample_weight])Request metadata passed to the
score
method.transform
(X, **params)Transform the data, and apply transform with the final estimator.
Attributes
The classes labels.
Names of features seen during first step fit method.
Number of features seen during first step fit method.
Access the steps by name.
- property classes_#
The classes labels. Only exist if the last step is a classifier.
- decision_function(X, **params)#
Transform the data, and apply decision_function with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls decision_function method. Only valid if the final estimator implements decision_function.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
**params (dict of string -> object) –
Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 1.4: Only available if enable_metadata_routing=True. See Metadata Routing User Guide for more details.
- Returns:
y_score (ndarray of shape (n_samples, n_classes)) – Result of calling decision_function on the final estimator.
- property feature_names_in_#
Names of features seen during first step fit method.
- fit(X, y=None, **params)#
Fit the model.
Fit all the transformers one after the other and sequentially transform the data. Finally, fit the transformed data using the final estimator.
- Parameters:
X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.
y (iterable, default=None) – Training targets. Must fulfill label requirements for all steps of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True is set viaset_config()
.See Metadata Routing User Guide for more details.
- Returns:
self (object) – Pipeline with fitted steps.
- fit_predict(X, y=None, **params)#
Transform the data, and apply fit_predict with the final estimator.
Call fit_transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls fit_predict method. Only valid if the final estimator implements fit_predict.
- Parameters:
X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.
y (iterable, default=None) – Training targets. Must fulfill label requirements for all steps of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters to the
predict
called at the end of all transformations in the pipeline.If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 0.20.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True.See Metadata Routing User Guide for more details.
Note that while this may be used to return uncertainties from some models with
return_std
orreturn_cov
, uncertainties that are generated by the transformations in the pipeline are not propagated to the final estimator.
- Returns:
y_pred (ndarray) – Result of calling fit_predict on the final estimator.
- fit_transform(X, y=None, **params)#
Fit the model and transform with the final estimator.
Fit all the transformers one after the other and sequentially transform the data. Only valid if the final estimator either implements fit_transform or fit and transform.
- Parameters:
X (iterable) – Training data. Must fulfill input requirements of first step of the pipeline.
y (iterable, default=None) – Training targets. Must fulfill label requirements for all steps of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters passed to the
fit
method of each step, where each parameter name is prefixed such that parameterp
for steps
has keys__p
.If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True.See Metadata Routing User Guide for more details.
- Returns:
Xt (ndarray of shape (n_samples, n_transformed_features)) – Transformed samples.
- get_feature_names_out(input_features=None)#
Get output feature names for transformation.
Transform input features using the pipeline.
- Parameters:
input_features (array-like of str or None, default=None) – Input features.
- Returns:
feature_names_out (ndarray of str objects) – Transformed feature names.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing (MetadataRouter) – A
MetadataRouter
encapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
Returns the parameters given in the constructor as well as the estimators contained within the steps of the Pipeline.
- Parameters:
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
params (mapping of string to any) – Parameter names mapped to their values.
- inverse_transform(X=None, *, Xt=None, **params)#
Apply inverse_transform for each step in a reverse order.
All estimators in the pipeline must support inverse_transform.
- Parameters:
X (array-like of shape (n_samples, n_transformed_features)) – Data samples, where
n_samples
is the number of samples andn_features
is the number of features. Must fulfill input requirements of last step of pipeline’sinverse_transform
method.Xt (array-like of shape (n_samples, n_transformed_features)) –
Data samples, where
n_samples
is the number of samples andn_features
is the number of features. Must fulfill input requirements of last step of pipeline’sinverse_transform
method.Deprecated since version 1.5: Xt was deprecated in 1.5 and will be removed in 1.7. Use X instead.
**params (dict of str -> object) –
Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 1.4: Only available if enable_metadata_routing=True. See Metadata Routing User Guide for more details.
- Returns:
Xt (ndarray of shape (n_samples, n_features)) – Inverse transformed data, that is, data in the original feature space.
- label = 'TF-IDF'#
- property n_features_in_#
Number of features seen during first step fit method.
- name = 'tfidf'#
- property named_steps#
Access the steps by name.
Read-only attribute to access any step by given name. Keys are steps names and values are the steps objects.
- predict(X, **params)#
Transform the data, and apply predict with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict method. Only valid if the final estimator implements predict.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters to the
predict
called at the end of all transformations in the pipeline.If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 0.20.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True is set viaset_config()
.See Metadata Routing User Guide for more details.
Note that while this may be used to return uncertainties from some models with
return_std
orreturn_cov
, uncertainties that are generated by the transformations in the pipeline are not propagated to the final estimator.
- Returns:
y_pred (ndarray) – Result of calling predict on the final estimator.
- predict_log_proba(X, **params)#
Transform the data, and apply predict_log_proba with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict_log_proba method. Only valid if the final estimator implements predict_log_proba.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters to the predict_log_proba called at the end of all transformations in the pipeline.
If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 0.20.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True.See Metadata Routing User Guide for more details.
- Returns:
y_log_proba (ndarray of shape (n_samples, n_classes)) – Result of calling predict_log_proba on the final estimator.
- predict_proba(X, **params)#
Transform the data, and apply predict_proba with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls predict_proba method. Only valid if the final estimator implements predict_proba.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
**params (dict of str -> object) –
If enable_metadata_routing=False (default): Parameters to the predict_proba called at the end of all transformations in the pipeline.
If enable_metadata_routing=True: Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 0.20.
Changed in version 1.4: Parameters are now passed to the
transform
method of the intermediate steps as well, if requested, and if enable_metadata_routing=True.See Metadata Routing User Guide for more details.
- Returns:
y_proba (ndarray of shape (n_samples, n_classes)) – Result of calling predict_proba on the final estimator.
- score(X, y=None, sample_weight=None, **params)#
Transform the data, and apply score with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls score method. Only valid if the final estimator implements score.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
y (iterable, default=None) – Targets used for scoring. Must fulfill label requirements for all steps of the pipeline.
sample_weight (array-like, default=None) – If not None, this argument is passed as
sample_weight
keyword argument to thescore
method of the final estimator.**params (dict of str -> object) –
Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 1.4: Only available if enable_metadata_routing=True. See Metadata Routing User Guide for more details.
- Returns:
score (float) – Result of calling score on the final estimator.
- score_samples(X)#
Transform the data, and apply score_samples with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls score_samples method. Only valid if the final estimator implements score_samples.
- Parameters:
X (iterable) – Data to predict on. Must fulfill input requirements of first step of the pipeline.
- Returns:
y_score (ndarray of shape (n_samples,)) – Result of calling score_samples on the final estimator.
- set_output(*, transform=None)#
Set the output container when “transform” and “fit_transform” are called.
Calling set_output will set the output of all estimators in steps.
- Parameters:
transform ({"default", "pandas", "polars"}, default=None) –
Configure output of transform and fit_transform.
”default”: Default output format of a transformer
”pandas”: DataFrame output
”polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
self (estimator instance) – Estimator instance.
- set_params(**kwargs)#
Set the parameters of this estimator.
Valid parameter keys can be listed with
get_params()
. Note that you can directly set the parameters of the estimators contained in steps.- Parameters:
**kwargs (dict) – Parameters of this estimator or parameters of estimators contained in steps. Parameters of the steps may be set using its name and the parameter name separated by a ‘__’.
- Returns:
self (object) – Pipeline class instance.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') Tfidf #
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weight
parameter inscore
.- Returns:
self (object) – The updated object.
- steps: List[Any]#
- transform(X, **params)#
Transform the data, and apply transform with the final estimator.
Call transform of each transformer in the pipeline. The transformed data are finally passed to the final estimator that calls transform method. Only valid if the final estimator implements transform.
This also works where final estimator is None in which case all prior transformations are applied.
- Parameters:
X (iterable) – Data to transform. Must fulfill input requirements of first step of the pipeline.
**params (dict of str -> object) –
Parameters requested and accepted by steps. Each step must have requested certain metadata for these parameters to be forwarded to them.
Added in version 1.4: Only available if enable_metadata_routing=True. See Metadata Routing User Guide for more details.
- Returns:
Xt (ndarray of shape (n_samples, n_transformed_features)) – Transformed data.