scikit-learn - 单变量 IsolationForest 的 Shap TreeExplainer 的 IndexError
问题描述
帮助!尝试解释 IsolationForest 时出现 IndexError。
我正在使用Scikit-learn
'sIsolationForest
进行异常检测。通常,我使用的数据集有多个变量——但有时它们只有一个。这适用于拟合和预测模型。但是,为了使用shap
's解释模型的输出TreeExplainer
,我得到一个IndexError
.
请参阅下面的最小可重现示例:
import pandas as pd
import numpy as np
from sklearn.ensemble import IsolationForest
from shap import TreeExplainer
df = pd.DataFrame()
df['Column1'] = np.random.randint(0, 100, 100)
model = IsolationForest()
model.fit(X=df)
explainer = TreeExplainer(model)
问题的根本原因似乎如下(见下面的代码):每个IsolationForest
都有多个隔离树。在 中TreeExplainer
,多个IsoTree
对象被初始化。在初始化期间,该行崩溃,因为self.features
,一个列表,包含 -2,这是超出范围的,因为tree_features
它只是一个数组 ( [0]
)。所以也许问题是在拟合 时IsolationForest
,给出了错误的值self.features
。
# re-number the features if each tree gets a different set of features
self.features = np.where(self.features >= 0, tree_features[self.features], self.features)
知道如何解决这个问题吗?
当然,对于单变量模型,使用 Shapley 值是没有意义的,因为您可以只使用score_samples
. 我打算将其用作解决方法,但肯定有一种更优雅的方式不需要这样做?
谢谢和最良好的祝愿,
亚历山大
解决方案
推荐阅读
- android - DatabaseReference 'setValue' Android Studio 显示错误
- android - 收到错误 android.util.SuperNotCalledException: Activity {} 没有调用 super.onCreate()
- asp.net - 文本未在材料 UI 核心中选择下拉列表的中心垂直对齐
- python - 如何调整我的神经网络以避免过度拟合 mnist 数据集?
- r - 如何分别标记所有组记录?
- python - Pygame 错误,音乐无法正常播放,因为它只是循环开始部分
- ruby-on-rails - 在 Rails 配置中获取 Rack/Puma 选项
- javascript - 在 laravel 中使用 ajax 上传文件
- azure - 使用 Function App 的 MSI 连接到主题
- javascript - 如何解决这个 Javascript/CSS 问题?