树模型天然会对特征进行重要性排序,以分裂数据集,构建分支;
1. 使用 Random Forest
from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
boston_data = load_boston()
X = boston_data['data']
y = boston_data['target']
# dir(boston_data) ⇒ 查看其支持的属性为 ['DESCR', 'data', 'feature_names', 'target']
rf = RandomForestRegressor()
rf.fit(X, y)
print(sorted(zip(boston_data['feature_names'], map(lambda x: round(x, 4),
rf.feature_importances_)),
key=operator.itemgetter(1), reverse=True))