In [1]:
# Standard Scaling (Z-score Normalization)
import pandas as pd
from sklearn.preprocessing import StandardScaler
df = pd.DataFrame({
''''A'''': [1, 2, 3, 4, 5],
''''B'''': [10, 20, 30, 40, 50]
})
scaler = StandardScaler()
scaled_data = scaler.fit_transform(df)
df_scaled = pd.DataFrame(scaled_data, columns=df.columns)
print("df")
print(df)
print("df_scaled")
print(df_scaled)
df A B 0 1 10 1 2 20 2 3 30 3 4 40 4 5 50 df_scaled A B 0 -1.414214 -1.414214 1 -0.707107 -0.707107 2 0.000000 0.000000 3 0.707107 0.707107 4 1.414214 1.414214
In [2]:
# Min-Max Scaling
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
df = pd.DataFrame({
''''A'''': [1, 2, 3, 4, 5],
''''B'''': [10, 20, 30, 40, 50]
})
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df)
df_scaled = pd.DataFrame(scaled_data, columns=df.columns)
print("df")
print(df)
print("df_scaled")
print(df_scaled)
df A B 0 1 10 1 2 20 2 3 30 3 4 40 4 5 50 df_scaled A B 0 0.00 0.00 1 0.25 0.25 2 0.50 0.50 3 0.75 0.75 4 1.00 1.00
In [3]:
# MaxAbs Scaling
import pandas as pd
from sklearn.preprocessing import MaxAbsScaler
df = pd.DataFrame({
''''A'''': [1, -2, 3, -4, 5],
''''B'''': [10, -20, 30, -40, 50]
})
scaler = MaxAbsScaler()
scaled_data = scaler.fit_transform(df)
df_scaled = pd.DataFrame(scaled_data, columns=df.columns)
print("df")
print(df)
print("df_scaled")
print(df_scaled)
df A B 0 1 10 1 -2 -20 2 3 30 3 -4 -40 4 5 50 df_scaled A B 0 0.2 0.2 1 -0.4 -0.4 2 0.6 0.6 3 -0.8 -0.8 4 1.0 1.0
In [4]:
# Robust Scaling
import pandas as pd
from sklearn.preprocessing import RobustScaler
df = pd.DataFrame({
''''A'''': [1, 2, 3, 4, 100],
''''B'''': [10, 20, 30, 40, 500]
})
scaler = RobustScaler()
scaled_data = scaler.fit_transform(df)
df_scaled = pd.DataFrame(scaled_data, columns=df.columns)
print("df")
print(df)
print("df_scaled")
print(df_scaled)
df A B 0 1 10 1 2 20 2 3 30 3 4 40 4 100 500 df_scaled A B 0 -1.0 -1.0 1 -0.5 -0.5 2 0.0 0.0 3 0.5 0.5 4 48.5 23.5