参考网址:https://blog.csdn.net/wang1127248268/article/details/53505078
from sklearn.cross_validation import cross_val_score, ShuffleSplit
from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
#Load boston housing dataset as an example
boston = load_boston()
X = boston["data"]
Y = boston["target"]
names = boston["feature_names"]
rf = RandomForestRegressor(n_estimators=20, max_depth=4)
scores = []
for i in range(X.shape[1]):
# 取第i个特征和输出特征Y比较,使用随机森林rf模型和r2评估工具
# ShuffleSplit类用于将样本集合随机“打散”后划分为训练集、测试集
# 元素总数为len(X),重新洗牌和分裂迭代次数为3,test集所占比例为0.3
score = cross_val_score(rf, X[:, i:i+1], Y, scoring="r2",cv=ShuffleSplit(len(X), 3, .3))
# round表示浮点数的四舍五入,小数点后保留三位有效数字
scores.append((round(np.mean(score), 3), names[i]))
print(sorted(scores, reverse=True))