具有单变量 IsolationForest 的 Shap TreeExplainer 的 IndexError

问题描述

帮助!尝试解释 IsolationForest 时出现索引错误

我使用 Scikit-learnIsolationForest 进行异常检测。通常,我使用的数据集有多个变量——但有时它们只有一个。这适用于拟合和预测模型。但是,为了使用 shapTreeExplainer 解释模型的输出,我得到一个 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 的异常分数。我打算使用它作为一种解决方法,但肯定有一种更优雅的方式不需要这样做?

感谢并致以最良好的祝愿,

亚历山大

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)