OrdinalEncoder#

class cuml.preprocessing.OrdinalEncoder(
*,
categories='auto',
dtype=<class 'numpy.float64'>,
handle_unknown='error',
verbose=False,
output_type=None,
)[source]#

Encode categorical features as an integer array.

The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are converted to ordinal integers. This results in a single column of integers (0 to n_categories - 1) per feature.

Parameters:
categories‘auto’ or a list of array-like, default=’auto’

Categories (unique values) per feature:

  • ‘auto’ : Determine categories automatically from the training data.

  • list : categories[i] holds the categories expected in the ith column.

The used categories can be found in the categories_ attribute.

dtypenumber type, default=np.float64

Desired dtype of output.

handle_unknown{‘error’, ‘ignore’}, default=’error’

When set to ‘error’ an error will be raised in case an unknown categorical feature is present during transform. When set to ‘ignore’, the encoded value of unknown categories will be set to NaN. In inverse_transform(), an unknown category will be denoted as None.

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:
categories_list of arrays

The categories of each feature determined during fit (in order of the features in X and corresponding with the output of transform). This does not include categories that weren’t seen during fit.

n_features_in_int

Number of features seen during fit.

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.

Methods

fit(X[, y])

Fit OrdinalEncoder to X.

fit_transform(X[, y])

Fit OrdinalEncoder to X, then transform X.

inverse_transform(X)

Convert the data back to the original representation.

transform(X)

Transform X using ordinal encoding.

Examples

>>> import cudf
>>> from cuml.preprocessing import OrdinalEncoder
>>> X = cudf.DataFrame({"fruit": ["apple", "banana", "apple"], "group": [1, 3, 2]})
>>> enc = OrdinalEncoder(output_type="numpy").fit(X)
>>> enc.categories_
[array(['apple', 'banana'], dtype=object), array([1, 2, 3])]
>>> enc.transform(X)
array([[0., 0.],
       [1., 2.],
       [0., 1.]])
>>> enc.inverse_transform([[1, 0], [0, 1]])
array([['banana', 1],
       ['apple', 2]], dtype=object)
fit(
X,
y=None,
) OrdinalEncoder[source]#

Fit OrdinalEncoder to X.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yNone

Ignored. This parameter exists for compatibility only.

fit_transform(X, y=None)[source]#

Fit OrdinalEncoder to X, then transform X.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

yNone

Ignored. This parameter exists for compatibility only.

Returns:
X_outcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, n_features)

Transformed input.

For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.

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_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

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_names method and does not need anything other than what is there in this method, then it doesn’t have to override this method

inverse_transform(X)[source]#

Convert the data back to the original representation.

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

The transformed data.

Returns:
X_originalndarray of shape (n_samples, n_features)

Inverse transformed array.

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_names method and does not need anything other than what is, there in this method, then it doesn’t have to override this method

transform(X)[source]#

Transform X using ordinal encoding.

Parameters:
Xarray-like (device or host) shape = (n_samples, n_features)

Dense matrix with dtype float32 or float64. Acceptable formats: CUDA array interface compliant objects like CuPy, cuDF DataFrame/Series, NumPy ndarray and Pandas DataFrame/Series.

Returns:
X_outcuDF, CuPy or NumPy object depending on cuML’s output type configuration, shape = (n_samples, n_features)

Transformed input.

For more information on how to configure cuML’s output type, refer to: Output Data Type Configuration.