Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将eli5.show_权重转换为数组/列表_Python_Numpy_Scikit Learn_Permutation_Logistic Regression - Fatal编程技术网

如何在python中将eli5.show_权重转换为数组/列表

如何在python中将eli5.show_权重转换为数组/列表,python,numpy,scikit-learn,permutation,logistic-regression,Python,Numpy,Scikit Learn,Permutation,Logistic Regression,我是训练有素的模特,一切都很好。我正在计算每个特征权重的排列重要性。我使用的是eli5.show_weights(),但它将输出显示为html,但我需要将其转换为某种列表或数组,以便访问值 这是我的代码示例,不包括估计器的代码部分,以便简化: import eli5 from eli5.sklearn import PermutationImportance perm = PermutationImportance(estimator, random_state=1).fit(X_testdf

我是训练有素的模特,一切都很好。我正在计算每个特征权重的排列重要性。我使用的是
eli5.show_weights()
,但它将输出显示为
html
,但我需要将其转换为某种列表或数组,以便访问值

这是我的代码示例,不包括
估计器的
代码部分,以便简化:

import eli5
from eli5.sklearn import PermutationImportance

perm = PermutationImportance(estimator, random_state=1).fit(X_testdf, y_testdf)
eli5.show_weights(perm, feature_names = X_testdf.columns.tolist())
这很好,但是我需要获得eli5输出的值。向数组或任何变量显示权重(…),以便访问这些值

我也试过了,但是运气不好

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

有人能帮我吗?

权重或特征重要性存储在排列对象中,您可以直接提取它们:

perm.feature_importances_ 
例如,这可能返回一个数组,如

array([0.   , 0.008, 0.584, 0.172]) # Only the mean, not the Std.Dev.
如果需要将它们与功能名称一起使用,可以压缩名称和功能重要性:

 list(zip(X_test.columns.tolist(), perm.feature_importances_))
或者,您也可以将HTML显示转换回原始HTML,然后使用Pandas读取。例如:

w = eli5.show_weights(perm, feature_names=feature_names)
result = pd.read_html(w.data)[0]
result
(使用
iris
数据集的输出示例)


尽管请注意这里的
Weight
列是一个字符串,但是您必须进行进一步的操作。

我使用以下代码参考了tania的post and get list

result = [(x[0], x[1], x[2]) for x in zip(perm_import.feature_importances_, perm_import.feature_importances_std_, X_train.columns)]
result.sort(key=lambda x: x[0], reverse=True)

print(result)
result = [(x[0], x[1], x[2]) for x in zip(perm_import.feature_importances_, perm_import.feature_importances_std_, X_train.columns)]
result.sort(key=lambda x: x[0], reverse=True)

print(result)