在python中如何将机器学习模型置换重要性放入数组或字典中

在python中如何将机器学习模型置换重要性放入数组或字典中,python,permutation,random-forest,Python,Permutation,Random Forest,我有这个代码示例,当我在jupyternotebook中尝试时,它运行良好。它显示为一个带有两列的表(图像),如下代码所示: from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split import eli5 from eli5.sklearn import PermutationImportance X = inputsdf y = targ

我有这个代码示例,当我在
jupyternotebook
中尝试时,它运行良好。它显示为一个带有两列的表(图像),如下代码所示:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

import eli5
from eli5.sklearn import PermutationImportance

X = inputsdf
y = targetdf
X_traindf, X_testdf, y_traindf, y_testdf = train_test_split(inputsNew, target, random_state=0)

estimator = RandomForestClassifier(max_depth=2, random_state=0)
estimator.fit(X_traindf, y_traindf)


perm = PermutationImportance(estimator, random_state=1).fit(X_testdf, y_testdf)
eli5.show_weights(perm, feature_names = X_testdf.columns.tolist())

但我需要将这些值转换为数组、字典或任何可以分配给变量并重新使用的内容。因此,它将看起来像下面这样:

{
"PercentageSalaryHike": "0.0960 +- 0.0222",
.
.
.
}
有人能帮帮我吗?还是有更好的方法来确定每列的排列重要性?

variable = np.array(eli5.show_weights(perm, feature_names = X_testdf.columns.tolist()))

我希望这能奏效。

你可以使用list bro。你能举个例子说明如何做到这一点吗@Ghost5454不,它不输出。它给出了以下警告/错误:
/usr/local/lib/python3.6/dist-packages/eli5/formatters/html.py:235:RuntimeWarning:在双标量rel\u-weight=(abs(weight)/weight\u-range)**0.7