Isolation Forest孤立森林(二)之sklearn实现,源码分析

孤立森林算法sklearn实现,源码分析

算法一: 首先初始化一些参数

class sklearn.ensemble.IsolationForest(n_estimators=100max_samples=’auto’contamination=’legacy’max_features=1.0bootstrap=Falsen_jobs=Nonebehaviour=’old’random_state=Noneverbose=0)

参数解释:n_estimators = 构建多少个itree

              max_samples=采样数,自动是256

             contamination=c(n)默认为0.1 

            max_features=最大特征数 默认为1

           bootstrap=构建Tree时,下次是否替换采样,为True为替换,为False为不替换

           n_jobs=fit和perdict执行时的并行数

第二步进行fit训练:def fit(self, X, y=None, sample_weight=None):

其中max_samples:

if self.max_samples == 'auto':

max_samples = min(256, n_samples) #如果输入的采样数大于256,就选择256,小于256选择输入的max_samples
itree的最大路径为:max_depth = int(np.ceil(np.log2(max(max_samples, 2))))

然后将max_samples,max_depth,X,sample_weight  传入

super(IsolationForest, self)._fit(X, y, max_samples,
                                  max_depth=max_depth,
                                  sample_weight=sample_weight)

进行统计:使用Scipy我们很方便的得到数据所在区域中某一百分比处的数值

self.threshold_ = -sp.stats.scoreatpercentile(
    -self.decision_function(X), 100. * (1. - self.contamination))   
 return self

fit方法就此结束了,下面是创建树也是进行分类的过程。

def decision_function(self, X):

 

 

 

未完待续

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值