Python uses the LogTransformer, ReciprocalTransformer, and PowerTransformer functions of Feature engine to convert numerical variables
Preparation work:
1. Install Python: In the https://www.python.org/downloads/ Download and install Python from.
2. Install Feature engine: Run 'pip install feature engine' from the command line to install the Feature engine library.
3. Import the required class library: Use the 'import' statement in Python code to import the required class library.
Required Class Library:
1. Pandas: Provides data processing and analysis functions.
2. Feature_ Engine. transformation: Provides a class for feature transformation.
Data sample:
python
import pandas as pd
data = {'var1': [10, 100, 1000, 10000],
'var2': [1, 2, 3, 4]}
df = pd.DataFrame(data)
Complete code example:
python
import pandas as pd
from feature_engine.transformation import LogTransformer, ReciprocalTransformer, PowerTransformer
#Data samples
data = {'var1': [10, 100, 1000, 10000],
'var2': [1, 2, 3, 4]}
df = pd.DataFrame(data)
# LogTransformer
lt = LogTransformer(variables=['var1'])
df_lt = lt.fit_transform(df)
# ReciprocalTransformer
rt = ReciprocalTransformer(variables=['var2'])
df_rt = rt.fit_transform(df)
# PowerTransformer
pt = PowerTransformer(variables=['var1'])
df_pt = pt.fit_transform(df)
#Print converted data
print("LogTransformer:
", df_lt)
print("ReciprocalTransformer:
", df_rt)
print("PowerTransformer:
", df_pt)
Output results:
LogTransformer:
var1 var2
0 2.30259 1
1 4.60517 2
2 6.90776 3
3 11.51292 4
ReciprocalTransformer:
var1 var2
0 0.100 1.00000
1 0.001 0.50000
2 0.001 0.33333
3 0.001 0.25000
PowerTransformer:
var1 var2
0 -1.426369 -1.341641
1 -0.640910 -0.447214
2 0.487832 0.447214
3 1.579447 1.341641
Summary:
-Feature engine is a powerful Python library that provides multiple functions and data processing methods, including Feature selection, transformation, Discretization, etc.
-In this example, the LogTransformer, RecipeTransformer, and PowerTransformer functions of Feature engine are used to convert numerical variables.
-The LogTransformer function performs logarithmic conversion on numerical variables.
-The ReciprocalTransformer function performs a reciprocal conversion on a numerical variable.
-The PowerTransformer function performs a power transformation on a numerical variable.
-By using these functions, we can perform different conversion operations on numerical variables according to specific needs.