我试图绘制随机森林模型的特征重要性,并将每个特征重要性映射回原始系数.我设法创建了一个显示重要性的图,并使用原始变量名作为标签,但现在它按照它们在数据集中的顺序排序变量名(而不是按重要性顺序排序).如何按功能重要性排序?谢谢!
我的代码是:
importances = brf.feature_importances_
std = np.std([tree.feature_importances_ for tree in brf.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(x_dummies.shape[1]):
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
# Plot the feature importances of the forest
plt.figure(figsize=(8,8))
plt.title("Feature importances")
plt.bar(range(x_train.shape[1]), importances[indices],
color="r", yerr=std[indices], align="center")
feature_names = x_dummies.columns
plt.xticks(range(x_dummies.shape[1]), feature_names)
plt.xticks(rotation=90)
plt.xlim([-1, x_dummies.shape[1]])
plt.show()
Run Code Online (Sandbox Code Playgroud) 我需要从循环中创建一个字典,该循环遍历2列数字的数组.下面是数组的一小部分:
array([[ 0, 1],
[ 1, 0],
[ 1, 2],
[ 2, 3],
[ 2, 1]])
Run Code Online (Sandbox Code Playgroud)
我想创建一个字典,它将第一列的唯一编号作为键(例如本例中为{0,1,2}),并将第二列中的相应数字作为值.
对于此示例,字典将如下所示:
dict = {0:[1], 1:[0,2], 2:[3,1]}
Run Code Online (Sandbox Code Playgroud)
我的数组很长(370,000 x 2)所以我想通过一个有效的循环来做到这一点.任何建议将不胜感激!
我正在尝试创建一个函数来计算多维numpy数组中元素之间的晶格距离(水平和垂直步长的数量).为此,我需要在迭代数组时从每个元素的索引中检索实际数字.我想将这些值存储为可以通过距离公式运行的数字.
对于示例阵列A.
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
Run Code Online (Sandbox Code Playgroud)
我想创建一个迭代遍历每个元素的循环,对于第一个元素1,它将检索a = 0,b = 0,因为1是A [0,0],然后a = 0,b = 1表示元素2因为它位于A [0,1],依此类推......
我设想的输出是数组中每个元素的两个数字(对应于该元素的两个索引值).所以在上面的例子中,我将分配的两个值是a和b.我只需要在循环中检索这两个数字(而不是单独保存为另一个数据对象).
任何关于如何做到这一点的想法将不胜感激!
我有一个列表数组,我需要将其转换为字典,其中每个列表中的第一个元素是一个键,其余元素是与该键对应的值.
例如,数组:
a=[[[1, 2, 4] [2, 1, 3, 5] [3, 2, 6]]
[[4, 1, 5, 7] [5, 2, 4, 6, 8] [6, 3, 5, 9]]]
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
dict = {1:[2,4], 2:[1,3,5], 3:[2,6], 4:[1,5,7], 5:[2,4,6,8], 6:[3,5,9]}
Run Code Online (Sandbox Code Playgroud)
虽然该对象看起来像列表列表,但它实际上是由此过程创建的数组:
a = [[i] for i in range(1, 10)]
swap = a[0]
a[0] = None
b = np.array(a)
b[0] = swap
b.shape = 3, 3
Run Code Online (Sandbox Code Playgroud)
然后我循环遍历数组并将数字附加到不同的列表元素,这就是列表扩展的原因.如果不清楚,请告诉我!
有没有一种简单的方法来循环数组并创建它?谢谢!