主要使用sklearn.feature_selection的SelectFromModel类。
案例摘自官网:https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.SelectFromModel.html#sklearn.feature_selection.SelectFromModel
from sklearn.feature_selection import SelectFromModelfrom sklearn.linear_model import LogisticRegressionX = [[ 0.87, -1.34, 0.31 ],[-2.79, -0.02, -0.85 ],[-1.34, -0.48, -2.55 ],[ 1.92, 1.48, 0.65 ]]y = [0, 1, 0, 1]selector = SelectFromModel(estimator=LogisticRegression()).fit(X, y)selector.estimator_.coef_>>> array([[-0.3252302 , 0.83462377, 0.49750423]])selector.threshold_>>> 0.55245...selector.get_support()>>> array([False, True, False])selector.transform(X)>>> array([[-1.34],[-0.02],[-0.48],[ 1.48]])
很简单地就能得到哪个特征是较为重要的特征。
主要的参数:
estimator:估计器需要有feature_importances_ or coef_属性
threshold:阈值
max_features:最大特征数
结合各种集成树模型是个很不错的选择。
