1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| ''' 基于惩罚项的特征选择法: 使用带惩罚项的基模型,也就是l1l2逻辑回归,除了筛选出特征外,同时也进行了降维,筛掉了不需要的维度。 ''' from sklearn.feature_selection import SelectFromModel from sklearn.linear_model import LogisticRegression
xs = SelectFromModel(LogisticRegression(penalty = 'l2',C = 0.1)).fit_transform(x,y) xs
clf = LogisticRegression(penalty = 'l2',C = 0.1) clf.fit(x,y)
importances = clf.coef_ abs(importances[0])
''' 基于树模型的特征选择法: ''' from sklearn.feature_selection import SelectFromModel from sklearn.ensemble import RandomForestClassifier
xs = SelectFromModel(RandomForestClassifier()).fit_transform(x,y) xs
rfr = RandomForestClassifier() rfr.fit(x,y)
importances = rfr.feature_importances_ importances
|