TargetEncoder
Target Encoder for regression and classification targets.
Each category is encoded based on a shrunk estimate of the average target values for observations belonging to the category. The encoding scheme mixes the global target mean with the target mean conditioned on the value of the category. [MIC]
TargetEncoder
considers missing values, such as np.nan
or undefined
, as another category and encodes them like any other category. Categories that are not seen during fit
are encoded with the target mean, i.e. target\_mean\_
.
For a demo on the importance of the TargetEncoder
internal cross-fitting, see ref:sphx\_glr\_auto\_examples\_preprocessing\_plot\_target\_encoder\_cross\_val.py
. For a comparison of different encoders, refer to Comparing Target Encoder with Other Encoders. Read more in the User Guide.
Python Reference (opens in a new tab)
Constructors
constructor()
Signature
new TargetEncoder(opts?: object): TargetEncoder;
Parameters
Name | Type | Description |
---|---|---|
opts? | object | - |
opts.categories? | "auto" | Categories (unique values) per feature: Default Value 'auto' |
opts.cv? | number | Determines the number of folds in the cross fitting strategy used in fit\_transform . For classification targets, StratifiedKFold is used and for continuous targets, KFold is used. Default Value 5 |
opts.random_state? | number | When shuffle is true , random\_state affects the ordering of the indices, which controls the randomness of each fold. Otherwise, this parameter has no effect. Pass an int for reproducible output across multiple function calls. See Glossary. |
opts.shuffle? | boolean | Whether to shuffle the data in fit\_transform before splitting into folds. Note that the samples within each split will not be shuffled. Default Value true |
opts.smooth? | number | "auto" | The amount of mixing of the target mean conditioned on the value of the category with the global target mean. A larger smooth value will put more weight on the global target mean. If "auto" , then smooth is set to an empirical Bayes estimate. Default Value 'auto' |
opts.target_type? | "auto" | "binary" | "continuous" | Type of target. Default Value 'auto' |
Returns
Defined in: generated/preprocessing/TargetEncoder.ts:27 (opens in a new tab)
Methods
dispose()
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Signature
dispose(): Promise<void>;
Returns
Promise
<void
>
Defined in: generated/preprocessing/TargetEncoder.ts:131 (opens in a new tab)
fit()
Fit the TargetEncoder
to X and y.
Signature
fit(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | The data to determine the categories of each feature. |
opts.y? | ArrayLike | The target data used to encode the categories. |
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:148 (opens in a new tab)
fit_transform()
Fit TargetEncoder
and transform X with the target encoding.
Signature
fit_transform(opts: object): Promise<ArrayLike[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | The data to determine the categories of each feature. |
opts.y? | ArrayLike | The target data used to encode the categories. |
Returns
Promise
<ArrayLike
[]>
Defined in: generated/preprocessing/TargetEncoder.ts:188 (opens in a new tab)
get_feature_names_out()
Get output feature names for transformation.
Signature
get_feature_names_out(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.input_features? | any | Input features. |
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:228 (opens in a new tab)
get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Signature
get_metadata_routing(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.routing? | any | A MetadataRequest encapsulating routing information. |
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:266 (opens in a new tab)
init()
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Signature
init(py: PythonBridge): Promise<void>;
Parameters
Name | Type |
---|---|
py | PythonBridge |
Returns
Promise
<void
>
Defined in: generated/preprocessing/TargetEncoder.ts:85 (opens in a new tab)
set_output()
Set output container.
See Introducing the set_output API for an example on how to use the API.
Signature
set_output(opts: object): Promise<any>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.transform? | "default" | "pandas" | Configure output of transform and fit\_transform . |
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:303 (opens in a new tab)
transform()
Transform X with the target encoding.
Signature
transform(opts: object): Promise<ArrayLike[]>;
Parameters
Name | Type | Description |
---|---|---|
opts | object | - |
opts.X? | ArrayLike [] | The data to determine the categories of each feature. |
Returns
Promise
<ArrayLike
[]>
Defined in: generated/preprocessing/TargetEncoder.ts:336 (opens in a new tab)
Properties
_isDisposed
boolean
=false
Defined in: generated/preprocessing/TargetEncoder.ts:25 (opens in a new tab)
_isInitialized
boolean
=false
Defined in: generated/preprocessing/TargetEncoder.ts:24 (opens in a new tab)
_py
PythonBridge
Defined in: generated/preprocessing/TargetEncoder.ts:23 (opens in a new tab)
id
string
Defined in: generated/preprocessing/TargetEncoder.ts:20 (opens in a new tab)
opts
any
Defined in: generated/preprocessing/TargetEncoder.ts:21 (opens in a new tab)
Accessors
categories_
The categories of each feature determined during fitting or specified in categories
(in order of the features in X
and corresponding with the output of transform
).
Signature
categories_(): Promise<any>;
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:394 (opens in a new tab)
encodings_
Encodings learnt on all of X
. For feature i
, encodings\_\[i\]
are the encodings matching the categories listed in categories\_\[i\]
.
Signature
encodings_(): Promise<any>;
Returns
Promise
<any
>
Defined in: generated/preprocessing/TargetEncoder.ts:369 (opens in a new tab)
feature_names_in_
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Signature
feature_names_in_(): Promise<ArrayLike>;
Returns
Promise
<ArrayLike
>
Defined in: generated/preprocessing/TargetEncoder.ts:494 (opens in a new tab)
n_features_in_
Number of features seen during fit.
Signature
n_features_in_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/preprocessing/TargetEncoder.ts:469 (opens in a new tab)
py
Signature
py(): PythonBridge;
Returns
PythonBridge
Defined in: generated/preprocessing/TargetEncoder.ts:72 (opens in a new tab)
Signature
py(pythonBridge: PythonBridge): void;
Parameters
Name | Type |
---|---|
pythonBridge | PythonBridge |
Returns
void
Defined in: generated/preprocessing/TargetEncoder.ts:76 (opens in a new tab)
target_mean_
The overall mean of the target. This value is only used in transform
to encode categories.
Signature
target_mean_(): Promise<number>;
Returns
Promise
<number
>
Defined in: generated/preprocessing/TargetEncoder.ts:444 (opens in a new tab)
target_type_
Type of target.
Signature
target_type_(): Promise<string>;
Returns
Promise
<string
>
Defined in: generated/preprocessing/TargetEncoder.ts:419 (opens in a new tab)