Pas encore de compte ?
La mise à l'échelle des fonctionnalités est utilisée pour normaliser les fonctionnalités des données afin que celle-ci soient ramenées à une échelle commune.
Il s'agit d'une étape de prétraitement des données très importante avant de créer un modèle d'apprentissage automatique, sinon le modèle résultant produira des résultats décevants.
Standardization
La standardisation est une méthode utile pour mettre à l'échelle les variables indépendantes afin qu'elles aient une distribution avec une valeur moyenne de 0 et une variance égale à 1.
Cependant, Standard Scaler n'est pas une bonne option si nos points de données ne sont pas normalement distribués, c'est-à-dire qu'ils ne suivent pas la distribution gaussienne.
Min-Max normalization
Dans la normalisation Min-Max, pour une caractéristique donnée, la valeur minimale de cette caractéristique est transformée en 0 tandis que la valeur maximale se transforme en 1 et toutes les autres valeurs sont normalisées entre 0 et 1.
Cette méthode présente cependant un inconvénient car elle est sensible aux valeurs aberrantes.
MaxAbs Scaler
Dans MaxAbs-Scaler, chaque fonctionnalité est mise à l'échelle en utilisant sa valeur maximale.
Au début, la valeur maximale absolue de la caractéristique est trouvée, puis les valeurs de la caractéristique sont divisées avec elle.
Tout comme MinMaxScaler, MaxAbs Scaler est également sensible aux valeurs aberrantes.
Robust scaler
Robust-Scaler est calculé en utilisant la plage interquartile (IQR), ici, IQR est la plage entre le 1er quartile (25e quantile) et le 3e quartile (75e quantile).
Il peut également gérer des points de données aberrants.
Comprendre les différentes techniques de mise à l'échelle des fonctionnalités avec le code Python
Testé sous Anaconda et Python 3.7
import pandas as pd import numpy as np import matplotlib.pyplot as plt df = pd.DataFrame({'WEIGHT': [15, 18, 12,10], 'PRICE': [1,3,2,5]}, index = ['Orange','Apple','Banana','Grape']) print(df) #Min-Max scaler from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() df1 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df1.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax); plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Standard Scaler from sklearn.preprocessing import StandardScaler scaler = StandardScaler() df2 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df2.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Max Abs Scaler from sklearn.preprocessing import MaxAbsScaler scaler = MaxAbsScaler() df4 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df4.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Robust Scaler from sklearn.preprocessing import RobustScaler scaler = RobustScaler() df3 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df3.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); dfr = pd.DataFrame({'WEIGHT': [15, 18, 12,10,50], 'PRICE': [1,3,2,5,20]}, index = ['Orange','Apple','Banana','Grape','Jackfruit']) print(dfr) from sklearn.preprocessing import StandardScaler scaler = StandardScaler() df21 = pd.DataFrame(scaler.fit_transform(dfr), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape','Jackfruit']) ax = dfr.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow','black'], marker = '*',s=80, label='BEFORE SCALING'); df21.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow','black'], marker = 'o',s=60,label='STANDARD', ax = ax,figsize=(12,6)) from sklearn.preprocessing import RobustScaler scaler = RobustScaler() df31 = pd.DataFrame(scaler.fit_transform(dfr), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape','Jackfruit']) df31.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow','black'], marker = 'v',s=60,label='ROBUST', ax = ax,figsize=(12,6)) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Quantile Transformer Scaler from sklearn.preprocessing import QuantileTransformer scaler = QuantileTransformer() df6 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df6.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax,figsize=(6,4)) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Power Transformer Scaler from sklearn.preprocessing import PowerTransformer scaler = PowerTransformer(method='yeo-johnson') df5 = pd.DataFrame(scaler.fit_transform(df), columns=['WEIGHT','PRICE'], index = ['Orange','Apple','Banana','Grape']) ax = df.plot.scatter(x='WEIGHT', y='PRICE',color=['red','green','blue','yellow'], marker = '*',s=80, label='BEFORE SCALING'); df5.plot.scatter(x='WEIGHT', y='PRICE', color=['red','green','blue','yellow'], marker = 'o',s=60,label='AFTER SCALING', ax = ax) plt.axhline(0, color='red',alpha=0.2) plt.axvline(0, color='red',alpha=0.2); #Unit Vector Scaler df8=df.apply(lambda x : x/np.linalg.norm(x,1)) print(df8) df9=df.apply(lambda x : x/np.linalg.norm(x,2)) print(df9)
Source : http://sefidian.com/2022/05/04/understand-different-feature-scaling-techniques-with-python-code/
Original data
WEIGHT PRICE
Orange 15 1
Apple 18 3
Banana 12 2
Grape 10 5
Robust Scaler
WEIGHT PRICE
Orange 15 1
Apple 18 3
Banana 12 2
Grape 10 5
Jackfruit 50 20
Unit Vector Scaler
WEIGHT PRICE
Orange 0.272727 0.090909
Apple 0.327273 0.272727
Banana 0.218182 0.181818
Grape 0.181818 0.454545
WEIGHT PRICE
Orange 0.532666 0.160128
Apple 0.639199 0.480384
Banana 0.426132 0.320256
Grape 0.355110 0.800641
Min-Max scaler
Standard Scaler
Max Abs Scaler
Robust Scaler
Robust Scaler
Power Transformer Scaler
Power Transformer Scaler
Bienvenu, je m’appelle Eric Soupet et je suis l'administrateur du site elodees.com. elodees.com est un état de l'art de l'Intelligence Artificielle et se veut collaboratif, vous pouvez dès à présent proposer du contenu tels que des articles, des événements, des tutoriels, ... alors n'hésitez pas !
Crédit des images de la plate-forme : Pixabay - Pixabay License | Pexels - Pexels License