# OrdinalEncoder

### *class* cuml.preprocessing.OrdinalEncoder(\*, categories='auto', dtype=<class 'numpy.float64'>, handle_unknown='error', verbose=False, output_type=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/preprocessing/encoders.py#L706)

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**
  : Categories (unique values) per feature:
    - ‘auto’ : Determine categories automatically from the training data.
    - list : `categories[i]` holds the categories expected in the ith
      column.
    <br/>
    The used categories can be found in the `categories_` attribute.

  **dtype**
  : Desired dtype of output.

  **handle_unknown**
  : 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()`](#cuml.preprocessing.OrdinalEncoder.inverse_transform), an unknown category will be denoted as None.

  **verbose**
  : Sets logging level. It must be one of `cuml.common.logger.level_*`.
    See [Verbosity Levels](../../cuml/index.html.md#verbosity-levels) for more info.

  **output_type**
  : 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](../../cuml/index.html.md#output-data-type-configuration) for more info.
* **Attributes:**
  **categories_**
  : 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_**
  : Number of features seen during `fit`.

  **feature_names_in_**
  : Names of features seen during `fit`. Defined only when `X`
    has feature names that are all strings.

### Methods

| [`fit`](#cuml.preprocessing.OrdinalEncoder.fit)(X[, y])           | Fit OrdinalEncoder to X.                              |
|------------------------------------------------------------------------|-------------------------------------------------------|
| [`fit_transform`](#cuml.preprocessing.OrdinalEncoder.fit_transform)(X[, y]) | Fit OrdinalEncoder to X, then transform X.            |
| [`inverse_transform`](#cuml.preprocessing.OrdinalEncoder.inverse_transform)(X)  | Convert the data back to the original representation. |
| [`transform`](#cuml.preprocessing.OrdinalEncoder.transform)(X)          | Transform X using ordinal encoding.                   |

### Examples

```pycon
>>> 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)
```

<!-- !! processed by numpydoc !! -->

#### as_sklearn()[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L206)

Convert this estimator into an equivalent scikit-learn (or scikit-learn
extension) estimator.

* **Returns:**
  sklearn.base.BaseEstimator
  : A scikit-learn compatible estimator instance that mirrors the trained
    state of the current estimator.

<!-- !! processed by numpydoc !! -->

#### fit(X, y=None) → [OrdinalEncoder](#cuml.preprocessing.OrdinalEncoder)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/preprocessing/encoders.py#L867)

Fit OrdinalEncoder to X.

* **Parameters:**
  **X**
  : 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.

  **y**
  : Ignored. This parameter exists for compatibility only.

<!-- !! processed by numpydoc !! -->

#### fit_transform(X, y=None)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/preprocessing/encoders.py#L875)

Fit OrdinalEncoder to X, then transform X.

* **Parameters:**
  **X**
  : 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.

  **y**
  : Ignored. This parameter exists for compatibility only.
* **Returns:**
  **X_out**
  : Transformed input.
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->

#### *classmethod* from_sklearn(model)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/interop.py#L222)

Create a cuml estimator from a scikit-learn estimator.

* **Parameters:**
  **model**
  : A compatible scikit-learn (or scikit-learn extension) estimator.
* **Returns:**
  cls
  : A new instance of this cuml estimator class that mirrors the
    state of the input estimator.

### Notes

`output_type` of the estimator is set to “numpy” by default, as these
cannot be inferred from training arguments. If something different is
required, then please use cuml’s output_type configuration utilities.

<!-- !! processed by numpydoc !! -->

#### get_feature_names_out(input_features=None)

Get output feature names for transformation.

* **Parameters:**
  **input_features**
  : Input features.
    - If `input_features` is [`None`](https://docs.python.org/3/builtins/constants.html#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**
  : Same as input features.

<!-- !! processed by numpydoc !! -->

#### get_params(deep=True)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L191)

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

<!-- !! processed by numpydoc !! -->

#### inverse_transform(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/preprocessing/encoders.py#L972)

Convert the data back to the original representation.

* **Parameters:**
  **X**
  : The transformed data.
* **Returns:**
  **X_original**
  : Inverse transformed array.

<!-- !! processed by numpydoc !! -->

#### set_params(\*\*params)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/internals/base.py#L200)

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

<!-- !! processed by numpydoc !! -->

#### transform(X)[[source]](https://github.com/NVIDIA/cuml/blob/93418baf5/python/cuml/cuml/preprocessing/encoders.py#L925)

Transform X using ordinal encoding.

* **Parameters:**
  **X**
  : 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_out**
  : Transformed input.
    <br/>
    For more information on how to configure cuML’s output type,
    refer to: [Output Data Type Configuration](../../cuml/index.html.md#output-data-type-configuration).

<!-- !! processed by numpydoc !! -->
