from sklearn.preprocessing import StandardScaler# create and fit scalerscaler =StandardScaler()scaler.fit(X)# scale data setXt = scaler.transform(X)# create data frame with resultsstats = np.vstack((X.mean(axis=0), X.var(axis=0), Xt.mean(axis=0), Xt.var(axis=0))).Tfeature_names = data['feature_names']columns = ['Dönüştürülmemiş Ortalama','Dönüştürülmemiş Varyans','Dönüştürülmüş Ortalama','Dönüştürülmüş Varyans']df = pd.DataFrame(stats, index=feature_names, columns=columns)df
🧱 Transformers Metotları
📊 Column Transformers
Sadece belirlenen sütunlara dönüştürme işlemi uygulamak için tercih edilir
from sklearn.compose import ColumnTransformercol_transformer =ColumnTransformer( remainder='passthrough', transformers=[ ('scaler', StandardScaler(), slice(0,6)) # first 6 columns ])col_transformer.fit(X)Xt = col_transformer.transform(X)print('MedInc mean before transformation?', X.mean(axis=0)[0])# 3.8706710029069766print('MedInc mean after transformation?', Xt.mean(axis=0)[0], '\n')# 6.609699867535816e-17print('Longitude mean before transformation?', X.mean(axis=0)[-1])# -119.56970445736432print('Longitude mean after transformation?', Xt.mean(axis=0)[-1])# -119.56970445736432col_transformer =ColumnTransformer( remainder='passthrough', transformers=[ ('remove', 'drop', 0), ('scaler', StandardScaler(), slice(1,6)) ])Xt = col_transformer.fit_transform(X)print('Number of features in X:', X.shape[1])# 8print('Number of features Xt:', Xt.shape[1])# 7
🍢 Pipeline
Birden fazla işlemleri seri olarak yapmayı sağlayan yöntemdir