<jupyter_start><jupyter_text>OneHotEncodingTransformerThis notebook shows the functionality in the `OneHotEncodingTransformer` class. This transformer implements one hot encoding. It uses the scikit-learn `OneHotEncoder` and implements nice renaming of output one hot columns.<jupyter_code>import pandas as pd
import numpy as np
from pprint import pprint
from sklearn.datasets import load_diabetes
import tubular
from tubular.nominal import OneHotEncodingTransformer
tubular.__version__<jupyter_output><empty_output><jupyter_text>Load diabetes dataset from sklearnWe also create a categorical column from `bmi` and treat it as unordered for demonstration purposes in this notebook.<jupyter_code>diabetes = load_diabetes(return_X_y=False, as_frame=True)['data']
diabetes['bmi_cut'] = pd.cut(diabetes['bmi'], bins = 4)
diabetes.head()
diabetes['bmi_cut'].value_counts(dropna=False) / diabetes.shape[0]<jupyter_output><empty_output><jupyter_text>Simple usage  Initialising OneHotEncodingTransformer<jupyter_code>ohe_1 = OneHotEncodingTransformer(columns = 'bmi_cut', copy = True, verbose = True)<jupyter_output>BaseTransformer.__init__() called<jupyter_text>OneHotEncodingTransformer fitThe `fit` identifies the levels that will be used to create the dummy variables in `transform`, it method must be run before the `transform` method.<jupyter_code>ohe_1.fit(diabetes)<jupyter_output>BaseTransformer.fit() called<jupyter_text>OneHotEncodingTransformer transformBy default the `OneHotEncodingTransformer` adds the dummy columns onto the original data. The default naming of dummy columns is; `[categorical feature][separator][category level]`. The separator is set in the `separator` argument when initialising the `OneHotEncodingTransformer` object.<jupyter_code>diabetes_2 = ohe_1.transform(diabetes)
diabetes_2.head()<jupyter_output><empty_output><jupyter_text>Dropping the original one hot encoded columnsThe columns specified to be one hot encoded can be dropped by using the `drop_original` argument when initialising the `OneHotEncodingTransformer` object.<jupyter_code>ohe_2 = OneHotEncodingTransformer(
    columns = 'bmi_cut', 
    drop_original = True,
    copy = True, 
    verbose = True
)
ohe_2.fit(diabetes)
diabetes_3 = ohe_2.transform(diabetes)
diabetes_3.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0772234273318872, 'fraction_numerical': 0.005206073752711497, 'mean_word_length': 8.04313725490196, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '183552', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 715, 'n_tokens_neox': 679, 'n_words': 194, 'path': 'examples/nominal/OneHotEncodingTransformer.ipynb', 'repo_name': 'binti59/LV', 'size': '2305'}