การรวม feature selector

การลดมิติข้อมูลใน Python

Jeroen Boeye

Head of Machine Learning, Faktion

Lasso regressor

from sklearn.linear_model import Lasso

la = Lasso(alpha=0.05)
la.fit(X_train, y_train)

# Actual coefficients = [5 2 0]
print(la.coef_)
[ 4.91  1.76 0. ]
print(la.score(X_test, y_test))
0.974
การลดมิติข้อมูลใน Python

LassoCV regressor

from sklearn.linear_model import LassoCV

lcv = LassoCV()

lcv.fit(X_train, y_train)

print(lcv.alpha_)
0.09
การลดมิติข้อมูลใน Python

LassoCV regressor

mask = lcv.coef_ != 0

print(mask)
[ True True False ]
reduced_X = X.loc[:, mask]
การลดมิติข้อมูลใน Python

ทบทวนแนวคิด

  • Random forest คือการรวมกันของ decision tree

  • เราสามารถใช้การรวมกันของโมเดลสำหรับการเลือก feature ได้เช่นกัน

การลดมิติข้อมูลใน Python

การเลือก feature ด้วย LassoCV

from sklearn.linear_model import LassoCV

lcv = LassoCV()
lcv.fit(X_train, y_train)

lcv.score(X_test, y_test)
0.99
lcv_mask = lcv.coef_ != 0
sum(lcv_mask)
66
การลดมิติข้อมูลใน Python

การเลือก feature ด้วย random forest

from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestRegressor

rfe_rf = RFE(estimator=RandomForestRegressor(), 
             n_features_to_select=66, step=5, verbose=1)

rfe_rf.fit(X_train, y_train)

rf_mask = rfe_rf.support_
การลดมิติข้อมูลใน Python

การเลือก feature ด้วย gradient boosting

from sklearn.feature_selection import RFE
from sklearn.ensemble import GradientBoostingRegressor

rfe_gb = RFE(estimator=GradientBoostingRegressor(), 
             n_features_to_select=66, step=5, verbose=1)

rfe_gb.fit(X_train, y_train)

gb_mask = rfe_gb.support_
การลดมิติข้อมูลใน Python

การรวม feature selector

import numpy as np

votes = np.sum([lcv_mask, rf_mask, gb_mask], axis=0)

print(votes)
array([3, 2, 2, ..., 3, 0, 1])
mask = votes >= 2

reduced_X = X.loc[:, mask]
การลดมิติข้อมูลใน Python

มาฝึกกันเถอะ!

การลดมิติข้อมูลใน Python

Preparing Video For Download...