IsolationForest 的 Predict_proba

PV8*_*PV8 5 python scikit-learn

我尝试使用隔离森林进行异常值检测(欺诈检测)。如果我运行下面的代码(使用训练集和测试集):

from sklearn.ensemble import IsolationForest
iso = IsolationForest(random_state=0).fit(X_train)
isopred = iso.predict(X_test)
Run Code Online (Sandbox Code Playgroud)

我得到一个数组:array([1, 1, -1, ..., 1, 1, 1]) 其中包含 1 或 -1。我如何使用可用于DecisionTreespredict_proba的内容。文档中是否有一个可用于 IsolationTree 的函数没有提到?

当我运行:iso.predict_proba(X_test)我收到此错误:

AttributeError:“IsolationForest”对象没有属性“predict_proba”

我正在搜索一个数组,它给出了预测属于哪个类(异常值或非异常值)的概率。

我的X_test样子:

A  B  C
11 1  0
11 3  0
11 0  1
Run Code Online (Sandbox Code Playgroud)

y_test.values.ravel()array([0,0,1])

Már*_*lho 7

该模块中没有Predict_proba,因为它不使用概率将每个样本评估为异常值,而是使用分数。

查看您提供的文档,每个样本都使用具有以下公式的决策函数进行分类:

decision_function = score_samples - offset_. offset_
Run Code Online (Sandbox Code Playgroud)

所以你可能想要的是Score_samples。使用样本数据:

X = [[-1.1], [0.3], [0.5], [100]]
iso = IsolationForest(random_state=0).fit(X)
iso_pred = iso.predict([[0.1], [0], [90]])
iso_scores = abs(iso.score_samples([[0.1], [0], [90]]))
Run Code Online (Sandbox Code Playgroud)

结果是:

[ 1  1 -1] 
[0.33644293 0.35190077 0.62865009]
Run Code Online (Sandbox Code Playgroud)

每个样本减去偏移量(默认=-0.5)后,如果返回结果为正,则为 inlier,否则为 outlier。

希望有帮助。