<jupyter_start><jupyter_text>One-hot encoding of top categories with pandas<jupyter_code># let's inspect the unique categories of A6

X_train["A6"].unique()
# let's find the top 5 most frequent categories in A6

X_train["A6"].value_counts().sort_values(ascending=False).head(5)
# let's make a list with the most frequent categories in A6

top_5 = [
    x for x in X_train["A6"].value_counts().sort_values(ascending=False).head(5).index
]

top_5
# Now let's create 5 binary variables to encode A6
# in train and test sets

for label in top_5:

    X_train[f"A6_{label}"] = np.where(X_train["A6"] == label, 1, 0)

    X_test[f"A6_{label}"] = np.where(X_test["A6"] == label, 1, 0)
# let's visualise the result

print(X_train[["A6"] + [f"A6_{label}" for label in top_5]].head(10))<jupyter_output>A6  A6_c  A6_q  A6_w  A6_i  A6_ff
596   c     1     0     0     0      0
303   q     0     1     0     0      0
204   w     0     0     1     0      0
351  ff     0     0     0     0      1
118   m     0     0     0     0      0
247   q     0     1     0     0      0
652   i     0     0     0     1      0
513   e     0     0     0     0      0
230  cc     0     0     0     0      0
250   e     0     0     0     0      0<jupyter_text>One-hot encoding of top categories with Feature-engine<jupyter_code># let's divide in train and test sets (again)

X_train, X_test, y_train, y_test = train_test_split(
    data.drop(labels=["target"], axis=1),  # predictors
    data["target"],  # target
    test_size=0.3,  # percentage of observations in test set
    random_state=0,  # seed to ensure reproducibility
)
ohe_enc = OneHotEncoder(
    top_categories=5,  # the number of popular categories
    variables=["A6", "A7"],  # the variables to encode
)

ohe_enc.fit(X_train)
# the encoder stores the variables it will encode

ohe_enc.variables_
# the encoder stores the most frequent labels per variable

ohe_enc.encoder_dict_
# let's transform train and test sets

X_train_enc = ohe_enc.transform(X_train)
X_test_enc = ohe_enc.transform(X_test)
# let's inspect the result

X_train_enc.head()
X_test_enc.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09402914903620123, 'fraction_numerical': 0.056417489421720736, 'mean_word_length': 2.643835616438356, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '517906', 'license': 'MIT', 'n_tokens_mistral': 819, 'n_tokens_neox': 773, 'n_words': 243, 'path': 'ch02-categorical-encoding/Recipe-03-One-hot-encoding-frequent-categories.ipynb', 'repo_name': 'PacktPublishing/Python-Feature-Engineering-Cookbook-Second-Edition', 'size': '2127'}