sklearn.preprocessing.KBinsDiscretizer

您所在的位置:网站首页 binsarray sklearn.preprocessing.KBinsDiscretizer

sklearn.preprocessing.KBinsDiscretizer

2023-09-26 08:44| 来源: 网络整理| 查看: 265

Bin continuous data into intervals.

Read more in the User Guide.

New in version 0.20.

Parameters: n_binsint or array-like of shape (n_features,), default=5

The number of bins to produce. Raises ValueError if n_bins > from sklearn.preprocessing import KBinsDiscretizer >>> X = [[-2, 1, -4, -1], ... [-1, 2, -3, -0.5], ... [ 0, 3, -2, 0.5], ... [ 1, 4, -1, 2]] >>> est = KBinsDiscretizer( ... n_bins=3, encode='ordinal', strategy='uniform', subsample=None ... ) >>> est.fit(X) KBinsDiscretizer(...) >>> Xt = est.transform(X) >>> Xt array([[ 0., 0., 0., 0.], [ 1., 1., 1., 0.], [ 2., 2., 2., 1.], [ 2., 2., 2., 2.]])

Sometimes it may be useful to convert the data back into the original feature space. The inverse_transform function converts the binned data into the original feature space. Each value will be equal to the mean of the two bin edges.

>>> est.bin_edges_[0] array([-2., -1., 0., 1.]) >>> est.inverse_transform(Xt) array([[-1.5, 1.5, -3.5, -0.5], [-0.5, 2.5, -2.5, -0.5], [ 0.5, 3.5, -1.5, 0.5], [ 0.5, 3.5, -1.5, 1.5]])

Methods

fit(X[, y, sample_weight])

Fit the estimator.

fit_transform(X[, y])

Fit to data, then transform it.

get_feature_names_out([input_features])

Get output feature names.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

inverse_transform(Xt)

Transform discretized data back to original feature space.

set_fit_request(*[, sample_weight])

Request metadata passed to the fit method.

set_output(*[, transform])

Set output container.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Discretize the data.

fit(X, y=None, sample_weight=None)[source]¶

Fit the estimator.

Parameters: Xarray-like of shape (n_samples, n_features)

Data to be discretized.

yNone

Ignored. This parameter exists only for compatibility with Pipeline.

sample_weightndarray of shape (n_samples,)

Contains weight values to be associated with each sample. Only possible when strategy is set to "quantile".

New in version 1.3.

Returns: selfobject

Returns the instance itself.

fit_transform(X, y=None, **fit_params)[source]¶

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: Xarray-like of shape (n_samples, n_features)

Input samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters.

Returns: X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names_out(input_features=None)[source]¶

Get output feature names.

Parameters: input_featuresarray-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_outndarray of str objects

Transformed feature names.

get_metadata_routing()[source]¶

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns: routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]¶

Get parameters for this estimator.

Parameters: deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns: paramsdict

Parameter names mapped to their values.

inverse_transform(Xt)[source]¶

Transform discretized data back to original feature space.

Note that this function does not regenerate the original data due to discretization rounding.

Parameters: Xtarray-like of shape (n_samples, n_features)

Transformed data in the binned space.

Returns: Xinvndarray, dtype={np.float32, np.float64}

Data in the original feature space.

set_fit_request(*, sample_weight: Union[bool, None, str] = '$UNCHANGED$') → KBinsDiscretizer[source]¶

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

True: metadata is requested, and passed to fit 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 fit.

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.

New 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_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns: selfobject

The updated object.

set_output(*, transform=None)[source]¶

Set output container.

See Introducing the set_output API for an example on how to use the API.

Parameters: transform{“default”, “pandas”}, default=None

Configure output of transform and fit_transform.

"default": Default output format of a transformer

"pandas": DataFrame output

None: Transform configuration is unchanged

Returns: selfestimator instance

Estimator instance.

set_params(**params)[source]¶

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 __ so that it’s possible to update each component of a nested object.

Parameters: **paramsdict

Estimator parameters.

Returns: selfestimator instance

Estimator instance.

transform(X)[source]¶

Discretize the data.

Parameters: Xarray-like of shape (n_samples, n_features)

Data to be discretized.

Returns: Xt{ndarray, sparse matrix}, dtype={np.float32, np.float64}

Data in the binned space. Will be a sparse matrix if self.encode='onehot' and ndarray otherwise.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3