在数据帧中,如何根据特征的重要性映射特征?

2024-09-27 02:16:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为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()

Tags: fromimportmodel特征sklearn重要性featurefit

热门问题