我试图为DecisionTreeRegressor
绘制特征重要性,并将每个特征重要性映射回列名。任何人都可以建议我怎么做?先谢谢你。
这是我正在使用的代码:
df1=df[['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO','LSTAT' ]]
x=df1[['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO']]
y=df1['LSTAT']
from sklearn.linear_model import LinearRegression
lm_model=LinearRegression()
lm_model.fit (x,y)
#y_pred=lm_model.predict(y)
# fit the model
model = RandomForestRegressor(random_state=1)
model.fit(x_train, y_train)
yhat = model.predict(x_test)
# evaluate predictions
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_test, yhat)
print('MAE: %.3f' % mae)
from sklearn.tree import DecisionTreeRegressor
model = DecisionTreeRegressor()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
您可以使用以下代码段创建包含功能及其重要性的数据框架
相关问题 更多 >
编程相关推荐