Binarize data (set feature values to 0 or 1) according to a threshold.
Values greater than the threshold map to 1, while values less than
or equal to the threshold map to 0. With the default threshold of 0,
only positive values map to 1.
Binarization is a common operation on text count data where the
analyst can decide to only consider the presence or absence of a
feature rather than a quantified number of occurrences for instance.
It can also be used as a pre-processing step for estimators that
consider boolean random variables (e.g. modelled using the Bernoulli
distribution in a Bayesian setting).
Read more in the .
User Guide
Parameters:
threshold : float, default=0.0
Feature values below or equal to this are replaced by 0, above it by 1.
Threshold may not be less than 0 for operations on sparse matrices.
copy : bool, default=True
Set to False to perform inplace binarization and avoid a copy (if
the input is already a numpy array or a scipy.sparse CSR matrix).
feature_names_in_ : ndarray of shape (n_features_in_,)
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Notes
If the input is a sparse matrix, only the non-zero values are subject
to update by the Binarizer class.
This estimator is stateless and does not need to be fitted.
However, we recommend to call fit_transform instead of
transform, as parameter validation is only performed in
fit.
Examples
>>> fromsklearn.preprocessingimportBinarizer>>> X=[[1.,-1.,2.],... [2.,0.,0.],... [0.,1.,-1.]]>>> transformer=Binarizer().fit(X)# fit does nothing.>>> transformerBinarizer()>>> transformer.transform(X)array([[1., 0., 1.], [1., 0., 0.], [0., 1., 0.]])
fit(X, y=None)
Only validates estimator’s parameters.
This method allows to: (i) validate the estimator’s parameters and
(ii) be consistent with the scikit-learn transformer API.
Parameters:
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The data.
y : None
Ignored.
Returns:
self : object
Fitted transformer.
fit_transform(X, y=None, **fit_params)
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params
and returns a transformed version of X.
Parameters:
X : array-like of shape (n_samples, n_features)
Input samples.
y : array-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
**fit_params : dict
Additional fit parameters.
Pass only if the estimator accepts additional params in its fit method.
Returns:
X_new : ndarray array of shape (n_samples, n_features_new)
Transformed array.
get_feature_names_out(input_features=None)
Get output feature names for transformation.
Parameters:
input_features : array-like of str or None, default=None
Input features.
If input_features is None, then feature_names_in_ is
used as feature names in. If feature_names_in_ is not defined,
then the following input feature names are generated:
["x0","x1",...,"x(n_features_in_-1)"].
If input_features is an array-like, then input_features must
match feature_names_in_ if feature_names_in_ is defined.
Returns:
feature_names_out : ndarray of str objects
Same as input features.
get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing
mechanism works.
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.
Configure whether metadata should be requested to be passed to the transform method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to transform.
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.
Parameters:
copy : str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for copy parameter in transform.
Returns:
self : object
The updated object.
transform(X, copy=None)
Binarize each element of X.
Parameters:
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The data to binarize, element by element.
scipy.sparse matrices should be in CSR format to avoid an
un-necessary copy.
copy : bool
Copy the input X or not.
Returns:
X_tr : {ndarray, sparse matrix} of shape (n_samples, n_features)