【发布时间】:2023-03-29 15:38:01
【问题描述】:
我是 python 新手,正在尝试学习如何从我使用此代码创建的列表中提取前 10 名和后 10 名:
clftest = logres1.fit(X_train,y_train)
#getting the feature's coefficient
feature_importance = clftest.coef_[0]
#creating an array to identify the highest and lowest value
sorter = np.argsort(feature_importance)
#using the shape of sorter, arrange it from lowest to highest
position = np.arange(sorter.shape[0])
featfig = plt.figure(figsize=(100,100))
featax = featfig.add_subplot(1, 1, 1)
featax.barh(position, feature_importance[sorter], align="center")
featax.set_yticks(position)
featax.set_yticklabels(np.array(X.columns)[sorter], fontsize=8)
plt.show()
如您所见,我的图表中涉及很多功能...
另外,我想知道这个是否有简写,或者这是否已经是最短的代码行了..
【问题讨论】:
-
您到底需要什么?
-
获取图表中的前 10 列和后 10 列。我所能做的就是从最高到最低显示它们并呈现所有列。遗憾的是,由于列数过多,无法阅读
-
@Marcus 你不能只分割
position和feature_importance[sorter]吗?像这样position_top = position[:10]
标签: python-3.x numpy matplotlib scikit-learn jupyter-notebook