<jupyter_start><jupyter_text>Exponential transformation with NumPy<jupyter_code># make a copy of the dataframe where we will store the modified
# variables

data_tf = data.copy()
# apply a 0.3 exponent power transformation to a set of variables

data_tf[['LSTAT', 'NOX', 'DIS', 'RM']] = np.power(data[['LSTAT', 'NOX', 'DIS', 'RM']], 0.3)
# visuallize the transformation (not included in recipe)

diagnostic_plots(data_tf, 'LSTAT')<jupyter_output><empty_output><jupyter_text>Exponential transformation with Scikit-learn<jupyter_code># initialize the transformer with a user defined function, in this
# case the 0.3 power transformation

transformer = FunctionTransformer(lambda x: np.power(x, 0.3), validate=True)
# capture variables to transform in a list
cols = ['LSTAT', 'NOX', 'DIS', 'RM']

# transform slice of dataframe with indicated variables
data_tf = transformer.transform(data[cols])
# capture returned NumPy array into a dataframe

data_tf = pd.DataFrame(data_tf, columns=cols)
# visualize the transformation (not in book)
diagnostic_plots(data_tf, 'LSTAT')<jupyter_output><empty_output><jupyter_text>Exponential transformation with Feature-engine<jupyter_code># initialize the transformer with the variables to transform and 
# the desired exponent

et = PowerTransformer(variables = ['LSTAT', 'NOX', 'DIS', 'RM'], exp=0.3)

# fit transformer to data
et.fit(data)
# transform indicated variables in the dataset
data_tf = et.transform(data)
# plot transform variable
diagnostic_plots(data_tf, 'LSTAT')<jupyter_output><empty_output><jupyter_text>There is more Not included in the bookApply different power transformations to different set of features using Feature-engine<jupyter_code>from sklearn.pipeline import Pipeline
pipe = Pipeline([
    ('power1', PowerTransformer(variables = ['LSTAT', 'NOX'], exp=0.3)),    
    ('power2', PowerTransformer(variables = ['DIS'], exp=0.4)),
    ('power3', PowerTransformer(variables = ['RM'], exp=0.5)),
     ])
pipe.fit(data)
data_tf = pipe.transform(data)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11874386653581943, 'fraction_numerical': 0.009322865554465163, 'mean_word_length': 6.6367041198501875, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '559339', 'license': 'MIT', 'n_tokens_mistral': 644, 'n_tokens_neox': 630, 'n_words': 189, 'path': 'Chapter04/Recipe-4-power-transformation.ipynb', 'repo_name': 'paulorobertolds/Python-Feature-Engineering-Cookbook', 'size': '2038'}