异常检测(二)——IsolationForest

1、简介

孤立森林(Isolation Forest)是另外一种高效的异常检测算法,它和随机森林类似,但每次选择划分属性和划分点(值)时都是随机的,而不是根据信息增益或者基尼指数来选择。在建树过程中,如果一些样本很快就到达了叶子节点(即叶子到根的距离d很短),那么就被认为很有可能是异常点。因为那些路径d比较短的样本,都是因为距离主要的样本点分布中心比较远的。也就是说,可以通过计算样本在所有树中的平均路径长度来寻找异常点。

sklearn提供了ensemble.IsolationForest模块可用于Isolation Forest算法。

2、主要参数和函数介绍

class  sklearn.ensemble. IsolationForest ( n_estimators=100 ,  max_samples=’auto’ ,  contamination=0.1 ,  max_features=1.0 ,  bootstrap=False ,  n_jobs=1 ,  random_state=None ,  verbose=0 )

参数:

n_estimators : int, optional (default=100)

      森林中树的颗数

max_samples : int or float, optional (default=”auto”)

对每棵树,样本个数或比例

contamination : float in (0., 0.5), optional (default=0.1)

       这是最关键的参数,用户设置样本中异常点的比例

max_features : int or float, optional (default=1.0)

        对每棵树,特征个数或比例

函数:

fit(X)

           Fit estimator.(无监督)

predict(X)

          返回值:+1 表示正常样本, -1表示异常样本。
decision_function(X)

          返回样本的异常评分。 值越小表示越有可能是异常样本。

3、IsolationForest实例

 
  1. #!/usr/bin/python

  2. # -*- coding:utf-8 -*-

  3.  
  4. import numpy as np

  5. import matplotlib.pyplot as plt

  6. from sklearn.ensemble import IsolationForest

  7. from scipy import stats

  8.  
  9. rng = np.random.RandomState(42)

  10.  
  11. # 构造训练样本

  12. n_samples = 200 #样本总数

  13. outliers_fraction = 0.25 #异常样本比例

  14. n_inliers = int((1. - outliers_fraction) * n_samples)

  15. n_outliers = int(outliers_fraction * n_samples)

  16.  
  17. X = 0.3 * rng.randn(n_inliers // 2, 2)

  18. X_train = np.r_[X + 2, X - 2] #正常样本

  19. X_train = np.r_[X_train, np.random.uniform(low=-6, high=6, size=(n_outliers, 2))] #正常样本加上异常样本

  20.  
  21. # fit the model

  22. clf = IsolationForest(max_samples=n_samples, random_state=rng, contamination=outliers_fraction)

  23. clf.fit(X_train)

  24. # y_pred_train = clf.predict(X_train)

  25. scores_pred = clf.decision_function(X_train)

  26. threshold = stats.scoreatpercentile(scores_pred, 100 * outliers_fraction) #根据训练样本中异常样本比例,得到阈值,用于绘图

  27.  
  28. # plot the line, the samples, and the nearest vectors to the plane

  29. xx, yy = np.meshgrid(np.linspace(-7, 7, 50), np.linspace(-7, 7, 50))

  30. Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])

  31. Z = Z.reshape(xx.shape)

  32.  
  33. plt.title("IsolationForest")

  34. # plt.contourf(xx, yy, Z, cmap=plt.cm.Blues_r)

  35. plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), threshold, 7), cmap=plt.cm.Blues_r) #绘制异常点区域,值从最小的到阈值的那部分

  36. a = plt.contour(xx, yy, Z, levels=[threshold], linewidths=2, colors='red') #绘制异常点区域和正常点区域的边界

  37. plt.contourf(xx, yy, Z, levels=[threshold, Z.max()], colors='palevioletred') #绘制正常点区域,值从阈值到最大的那部分

  38.  
  39. b = plt.scatter(X_train[:-n_outliers, 0], X_train[:-n_outliers, 1], c='white',

  40. s=20, edgecolor='k')

  41. c = plt.scatter(X_train[-n_outliers:, 0], X_train[-n_outliers:, 1], c='black',

  42. s=20, edgecolor='k')

  43. plt.axis('tight')

  44. plt.xlim((-7, 7))

  45. plt.ylim((-7, 7))

  46. plt.legend([a.collections[0], b, c],

  47. ['learned decision function', 'true inliers', 'true outliers'],

  48. loc="upper left")

  49. plt.show()

结果:

参考文献:

http://scikit-learn.org/stable/modules/outlier_detection.html#outlier-detection
http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.ensemble.IsolationForest

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值