【问题标题】:Why is the score_samples-function of the IsolationForest algorithm in Scikit-Learn giving identical scores?为什么 Scikit-Learn 中 IsolationForest 算法的 score_samples-function 给出相同的分数?
【发布时间】:2022-01-20 14:46:36
【问题描述】:

我正在尝试在 scikit-learn 中使用 IsolationForest 算法,并且我对计算出的分数感兴趣。但是当调用score_samples() 时,我没有得到我期望的分数。

这是我的数据图:

下面是调用score_samples() 时 IsolationForest 算法的相应分数图:

如您所见,这两个系列对于右侧最后 100 个值中的几乎每个值都有相同的分数。为什么?我希望它们是不同的。

此外,还有几个分数低于最后 100 分,这表明它们更有可能是异常情况。但在系列图中,它们更接近拟合数据。这是为什么呢?

最后 100 分的两个分数系列终于有了区别。就好像有一个他们不能超过的最低分值(尽管之前的一些分数做到了?)

我查看了 Scikit-Learn 文档中引用的 scores formulapaper,但这并没有让我更接近答案。

分数出现这种行为的原因是什么?是否有任何变通方法来获得更“合理”的分数指标?理想情况下,我希望得分在 (0, 1) 范围内。

这是用于生成两个数据系列的代码:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = [16, 6]

### simulating data
np.random.seed(0)
X1 = np.concatenate((np.random.normal(loc=2.75, scale=0.1, size=335),
                     np.random.normal(loc=3.2, scale=0.1, size=100)))
X1_train = X1[:200]

np.random.seed(0)
X2 = np.concatenate((np.random.normal(loc=2.75, scale=0.1, size=335),
                     np.random.normal(loc=3.0, scale=0.1, size=100)))
X2_train = X2[:200]

### plotting simulated data
plt.plot(X1, 'x', label='values of series 1')
plt.plot(X2, '.', markersize=3, label='values of series 2')
plt.axvline(200, c='k', linestyle=(0, (5, 10)), linewidth=0.5) ### visualizing the end of the training data.
plt.legend(loc='upper left')

这是用于生成 IsolationForest 算法分数的代码:

from sklearn.ensemble import IsolationForest    

### fitting isolation forests and computing scores
iso1 = IsolationForest(random_state=0).fit(X1_train.reshape(-1, 1))
score1 = iso1.score_samples(X1.reshape(-1, 1))

iso2 = IsolationForest(random_state=0).fit(X2_train.reshape(-1, 1))
score2 = iso2.score_samples(X2.reshape(-1, 1))

### plotting scores
plt.plot(score1, 'x', label='IForest score of series 1')
plt.plot(score2, '.', markersize=3, label='IForest score of series 2')
plt.axvline(200, c='k', linestyle=(0, (5, 10)), linewidth=0.5)
plt.legend(loc='lower left')

【问题讨论】:

  • 这似乎更适合 datascience.stackexchange.com 或 stats.stackexchange.com
  • 我现在给出了答案。如果您在另一个 SE 上发帖,请在此处链接帖子?例如,在帖子顶部,或者至少在评论中

标签: python scikit-learn scoring anomaly-detection isolation-forest


【解决方案1】:

我相信这个问题是由于您的异常样本超出了训练数据的数据分布造成的。在这个区域中,树木不会分裂,所以你只会得到“最大异常”。

一般来说,这种数据(连续正态单变量正态分布时间序列)不适合 IsolationForest。它更擅长于多变量、稀疏数据、混合分类数据、非典型分布。

在这种情况下,其他模型(如 z 分数或中值绝对偏差变换)会更加连续,随着数据点的距离越远,分数也会越高。他们的得分可以解释为概率。

【讨论】:

  • 我相信你是对的。谢谢!我会研究其他一些模型。
猜你喜欢
  • 2021-02-15
  • 2017-04-25
  • 1970-01-01
  • 2023-03-29
  • 2021-05-06
  • 1970-01-01
  • 2023-04-05
  • 2021-07-15
  • 2016-08-17
相关资源
最近更新 更多