gpt4 book ai didi

python-3.x - 获取前 10 名和后 10 名特征

转载 作者:行者123 更新时间:2023-12-01 03:09:54 28 4
gpt4 key购买 nike

我是 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()

如您所见,我的图表中涉及很多功能......

另外,我想知道这个是否有简写,或者这是否已经是最短的代码行了..

Result of the code above

最佳答案

尝试这个:

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)

#add 2 rows in you code
n = 10 # this is number of features top
sorter = np.append(sorter[:n],sorter[-n:]) #this is fixed code

#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()

关于python-3.x - 获取前 10 名和后 10 名特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020910/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com