<jupyter_start><jupyter_text>Count encoding with pandas<jupyter_code># let's obtain the counts for each category in the variable A7

counts = X_train["A7"].value_counts().to_dict()

counts
# replace the categories with the counts

X_train["A7"] = X_train["A7"].map(counts)
X_test["A7"] = X_test["A7"].map(counts)
# let's explore the result

X_train["A7"].head(10)
# if instead of the count we would like the frequency:

frequencies = X_train["A6"].value_counts(normalize=True).to_dict()

frequencies
# replace the categories with the frequency

X_train["A6"] = X_train["A6"].map(frequencies)
X_test["A6"] = X_test["A6"].map(frequencies)<jupyter_output><empty_output><jupyter_text>Count encoding with Feature-engine<jupyter_code>from feature_engine.encoding import CountFrequencyEncoder
# let's divide into 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
)
# let's set up the encoder

count_enc = CountFrequencyEncoder(
    encoding_method="count",  # to do frequency ==> encoding_method='frequency'
    variables=None,  # to select all categorical variables
)
# fit the encoder to the train set

count_enc.fit(X_train)
# the encoder finds categorical variables automatically

count_enc.variables_
# the encoder_dict_ attribute contains the counts
# per category per variable.

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

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

X_train_enc.head()
X_test_enc.head()<jupyter_output><empty_output><jupyter_text>Count encoding with Category Encoders<jupyter_code>from category_encoders.count import CountEncoder
# let's set up the encoder to encode all
# categorical variables with counts

count_enc = CountEncoder(cols=None)

# for frequency encoding ==> set parameter normalize=True
# fit the encoder to the train set

count_enc.fit(X_train)
# the encoder finds categorical variables automatically

count_enc.cols
# the enoding parameters are stored in the
# attribute mappings:

count_enc.mapping
# let's transform train and test sets

X_train_enc = count_enc.transform(X_train)
X_test_enc = count_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.09587378640776699, 'fraction_numerical': 0.007281553398058253, 'mean_word_length': 6.188953488372093, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '652149', 'license': 'MIT', 'n_tokens_mistral': 822, 'n_tokens_neox': 792, 'n_words': 220, 'path': 'ch02-categorical-encoding/Recipe-04-Replacing-categories-by-counts-frequency.ipynb', 'repo_name': 'PacktPublishing/Python-Feature-Engineering-Cookbook-Second-Edition', 'size': '2472'}