【发布时间】:2022-01-20 14:46:36
【问题描述】:
我正在尝试在 scikit-learn 中使用 IsolationForest 算法,并且我对计算出的分数感兴趣。但是当调用score_samples() 时,我没有得到我期望的分数。
下面是调用score_samples() 时 IsolationForest 算法的相应分数图:
如您所见,这两个系列对于右侧最后 100 个值中的几乎每个值都有相同的分数。为什么?我希望它们是不同的。
此外,还有几个分数低于最后 100 分,这表明它们更有可能是异常情况。但在系列图中,它们更接近拟合数据。这是为什么呢?
最后 100 分的两个分数系列终于有了区别。就好像有一个他们不能超过的最低分值(尽管之前的一些分数做到了?)
我查看了 Scikit-Learn 文档中引用的 scores formula 和 paper,但这并没有让我更接近答案。
分数出现这种行为的原因是什么?是否有任何变通方法来获得更“合理”的分数指标?理想情况下,我希望得分在 (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