mojolearn.KBinsDiscretizer#
- class mojolearn.KBinsDiscretizer(n_bins=5, *, encode='onehot', strategy='quantile', quantile_method='averaged_inverted_cdf', dtype=None, subsample=200000, random_state=None)[source]#
Bases:
_PrepBaseBin continuous features into intervals.
Follows scikit-learn’s
KBinsDiscretizer. Each column is split inton_binsintervals and each value is replaced by its bin index or a one-hot block. One-hot output is always a dense float32 array. A constant column gets a single bin with edges(-inf, inf).- Parameters:
n_bins (int or array-like of int, default 5) – Number of bins, one value for all columns or one per column; each must be
>= 2.encode ({‘onehot’, ‘onehot-dense’, ‘ordinal’}, default ‘onehot’) – Output encoding.
'onehot'returns a dense array, the same as'onehot-dense'.strategy ({‘uniform’, ‘quantile’, ‘kmeans’}, default ‘quantile’) –
'uniform': equal-width bins.'quantile': edges at the quantiles given byquantile_method.'kmeans': 1-D k-means started from the uniform bin centers.quantile_method (str, default ‘averaged_inverted_cdf’) – numpy quantile method for
strategy='quantile':'averaged_inverted_cdf','inverted_cdf','closest_observation','interpolated_inverted_cdf','hazen','weibull','linear','median_unbiased'or'normal_unbiased'.dtype (default None) – Accepted for scikit-learn compatibility; the output is always float32.
subsample (int or None, default 200000) – Above this many rows, fit uses a resample of
subsamplerows drawn with replacement. None uses every row.random_state (int or None, default None) – Seed for the resample. None uses seed 0. The draw uses mojolearn’s own generator, so the rows differ from scikit-learn’s for the same seed.
- Variables:
bin_edges_ (list of arrays) – Edges of each column’s bins.
n_bins_ (array of int64) – Number of bins of each column (fewer than requested for a constant column).
n_features_in_ (int) – Number of columns seen in fit.
numeric_mode_ (str) – Numeric mode the estimator was fitted in; transform runs in the same mode.
Notes
fitacceptssample_weight(nonnegative, not all zero). It weights the quantile edges (only'averaged_inverted_cdf'and'inverted_cdf'are allowed with weights, as in scikit-learn; other methods raiseValueError), the uniform range (minimum and maximum over rows of nonzero weight) and the k-means fit. Abovesubsamplerows the weights instead drive a weighted resample.inverse_transformreturns each bin’s center. An all-zero one-hot block or a code that names no bin raisesValueError.Examples
>>> import numpy as np, mojolearn >>> X = np.random.default_rng(0).random((1000, 4), dtype=np.float32) >>> Xt = mojolearn.KBinsDiscretizer(n_bins=4, encode="ordinal").fit_transform(X)
- __init__(n_bins=5, *, encode='onehot', strategy='quantile', quantile_method='averaged_inverted_cdf', dtype=None, subsample=200000, random_state=None)[source]#
Methods
fit(X[, y, sample_weight])fit_transform(X[, y])Fit to
X, then transformX.get_params([deep])Return the constructor parameters as a dict.
inverse_transform(X)set_params(**params)Set constructor parameters.
transform(X)