Skip to content

Instantly share code, notes, and snippets.

@smly
Created August 6, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smly/52e634e98a4cd5bca07cf72d979984c5 to your computer and use it in GitHub Desktop.
Save smly/52e634e98a4cd5bca07cf72d979984c5 to your computer and use it in GitHub Desktop.
(xfeat:PR#3) Test code and the result
col target
0 Cat 1
1 Dog 1
2 Dog 0
3 Dog 0
4 Fox 0
<class 'cudf.core.dataframe.DataFrame'>
col target col_le
0 Cat 1 0
1 Dog 1 1
2 Dog 0 1
3 Dog 0 1
4 Fox 0 2
<class 'cudf.core.dataframe.DataFrame'>
col target
0 Cat 1
1 Dog 1
2 Dog 0
3 Dog 0
4 Fox 0
col target col_te
0 Cat 1 1.0
1 Dog 1 0.0
2 Dog 0 0.5
3 Dog 0 0.5
4 Fox 0 0.0
<class 'cudf.core.dataframe.DataFrame'>
import pandas as pd
import cudf
from xfeat.cat_encoder import TargetEncoder, LabelEncoder
df_train = cudf.from_pandas(pd.DataFrame({
"col": ["Cat", "Dog", "Dog", "Dog", "Fox", "Cat", "Rabbit"],
"target": [1, 1, 0, 0, 0, 1, 1],
}))
print(df_train.head())
print(type(df_train))
encoder = LabelEncoder(input_cols=["col"])
df_train_le = encoder.fit_transform(df_train)
print(df_train_le.head())
print(type(df_train_le))
df_train = cudf.from_pandas(pd.DataFrame({
"col": ["Cat", "Dog", "Dog", "Dog", "Fox", "Cat", "Rabbit"],
"target": [1, 1, 0, 0, 0, 1, 1],
}))
print(df_train.head())
target_encoder = TargetEncoder(input_cols=["col"], target_col="target")
df_train_te = target_encoder.fit_transform(df_train)
print(df_train_te.head())
print(type(df_train_te))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment