本文介绍的 Isolation Forest 算法原理请参看我的博客:Isolation Forest异常检测算法原理详解,本文中我们只介绍详细的代码实现过程。

1、ITree的设计与实现

首先,我们参看原论文中的ITree的构造伪代码:

这里写图片描述

1.1 设计ITree类的数据结构

由原论文[1,2]以及上述伪代码可知,ITree是一个二叉树,并且构建ITree的算法采用的是递归构建。同时构造的结束条件是:

当前节点的高度超过了算法设置的阈值 l ;
当前子树只包含一个叶节点;
当前子树的所有节点值的所有属性完全一致。
并且在递归的时候,我们需要随机的选择属性集 Q 中的一个属性Qi以及该属性在给出的输入数据上对应的最大值和最小值之间的一个值 q ,来将当前节点包含的样本分为左右子树。因此我们为了后续算法设计的方便,需要记录被选中的属性Qi的索引值 attrIndex,以及算出来的q值attrValue,因为后面算法需要根据该节点为根节点的子树包含的叶节点总数估计该节点总高度,因此我们还需要定义一个变量leafNodes记录树的叶子节点总数,同时我们还需要一个成员变量curHeight来记录该节点的实际高度。当然,二叉树少不了定义左右子树指针 lTree 和 rTree。

因此,我设计了如下的数据结构ITree:

public class ITree {

// 被选中的属性索引
public int attrIndex;

// 被选中的属性的一个具体的值
public double attrValue;

// 树的总叶子节点数
public int leafNodes;

// 该节点在树种的高度
public int curHeight;

// 左右孩子书
public ITree lTree, rTree;

// 构造函数,初始化ITree中的值
public ITree(int attrIndex, double attrValue) {
// 默认高度,树的高度从0开始计算
this.curHeight = 0;

this.lTree = null;
this.rTree = null;
this.leafNodes = 1;
this.attrIndex = attrIndex;
this.attrValue = attrValue;

1.2 递归地构造二叉树ITree

根据原论文中的算法2的伪代码,我们知道递归地构造二叉树ITree分为两个部分:

第一,首先判断是否满足1.1节列出的三个递归结束条件;

第二,随机的选取属性集中的一个属性以及该属性集下的一个具体的值,然后根据该属性以及生成的属性值将父节点中包含的样本数据划分到左右子树,并递归地创建左右子树。

同时记录每个节点包含的叶子节点数和当前节点在整个树中的实际高度。

参看如下的详细代码实现:

/**
* 根据samples样本数据递归的创建 ITree 树
*/
public static ITree createITree(http://www.wmyl15.com/ double[][] samples, int curHeight,
int limitHeight)
{

ITree iTree = null;

/*************** 第一步:判断递归是否满足结束条件 **************/
if (samples.length == 0) {
return iTree;
} else if (curHeight >= limitHeight || samples.length == 1) {
iTree = new ITree(0, samples[0][0]);
iTree.leafNodes = 1;
iTree.curHeight = curHeight;
return iTree;
}

int rows = samples.length;
int cols = samples[0].length;

// 判断是否所有样本都一样,如果都一样构建也终止
boolean isAllSame = true;
break_label:
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < cols; http://www.wmyl11.com/ j++) {
if (samples[i][j] != samples[i + 1][j]) {
isAllSame = false;
break break_label;
}
}
}

// 所有的样本都一样,构建终止,返回的是叶节点
if (isAllSame == true) {
iTree = new ITree(0, samples[0][0]);
iTree.leafNodes = samples.length;
iTree.curHeight = curHeight;
return iTree;
}

/*********** 第二步:不满足递归结束条件,继续递归产生子树 *********/
Random random = new Random(System.currentTimeMillis());
int attrIndex = random.nextInt(cols);

// 找这个被选维度的最大值和最小值
double min, max;
min = samples[0][attrIndex];
max = min;
for (int i = 1; i < rows; i++) {
if (samples[i][attrIndex] < min) {
min = samples[www.zhouyajinguawang.cn i][attrIndex];
}
if (samples[i][attrIndex] > max) {
max = samples[i][attrIndex];
}
}

// 计算划分属性值
double attrValue = random.nextDouble() * (max - min) + min;

// 将所有的样本的attrIndex对应的属性与
// attrValue 进行比较以选出左右子树对应的样本
int lnodes = 0, rnodes = 0;
double curValue;
for (int i = 0; i < rows; i++) {
curValue = samples[i www.wmylcs.com ][attrIndex];
if (curValue < attrValue) {
lnodes++;
} else {
rnodes++;
}
}

double[][] lSamples = new double[lnodes][cols];
double[][] rSamples = new double[rnodes][cols];

lnodes = 0;
rnodes = 0;
for (int i = 0; i < rows; i++) {
curValue = samples[i][attrIndex];
if (curValue < attrValue) {
lSamples[lnodes++] = samples[i];
} else {
rSamples[rnodes++] = samples[i];
}
}

// 创建父节点
ITree parent = new ITree( www.feifanshifan8.cn attrIndex, attrValue);
parent.leafNodes = rows;
parent.curHeight = curHeight;
parent.lTree = createITree(lSamples, curHeight + 1, limitHeight);
parent.rTree = createITree(rSamples, curHeight + 1, limitHeight);

return parent;
今天已晚,我要下班,未完待续……
这里写图片描述

这里写图片描述

参考文献:

http://www.yongshiyule178.com /zhouzh.files/publication/icdm08b.pdf
http://cs.nju.edu.cn/zhouzh/zhouzh.files/publication/tkdd11.pdf

Isolation Forest算法实现详解的更多相关文章

  1. [置顶] Isolation Forest算法原理详解

    本文只介绍原论文中的 Isolation Forest 孤立点检测算法的原理,实际的代码实现详解请参照我的另一篇博客:Isolation Forest算法实现详解. 或者读者可以到我的GitHub上去 ...

  2. [置顶] Isolation Forest算法实现详解

    本文算法完整实现源码已开源至本人的GitHub(如果对你有帮助,请给一个 star ),参看其中的 iforest 包下的 IForest 和 ITree 两个类: https://github.co ...

  3. 一致性算法RAFT详解

    原帖地址:http://www.solinx.co/archives/415?utm_source=tuicool&utm_medium=referral一致性算法Raft详解背景 熟悉或了解 ...

  4. 各大公司广泛使用的在线学习算法FTRL详解

    各大公司广泛使用的在线学习算法FTRL详解 现在做在线学习和CTR常常会用到逻辑回归( Logistic Regression),而传统的批量(batch)算法无法有效地处理超大规模的数据集和在线数据 ...

  5. 转】Mahout推荐算法API详解

    原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...

  6. MD5算法步骤详解

    转自MD5算法步骤详解 之前要写一个MD5程序,但是从网络上看到的资料基本上一样,只是讲了一个大概.经过我自己的实践,我决定写一个心得,给需要实现MD5,但又不要求很高深的编程知识的童鞋参考.不多说了 ...

  7. [转]Mahout推荐算法API详解

    Mahout推荐算法API详解 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeepe ...

  8. 2. EM算法-原理详解

    1. EM算法-数学基础 2. EM算法-原理详解 3. EM算法-高斯混合模型GMM 4. EM算法-高斯混合模型GMM详细代码实现 5. EM算法-高斯混合模型GMM+Lasso 1. 前言 概率 ...

  9. javascript常用经典算法实例详解

    javascript常用经典算法实例详解 这篇文章主要介绍了javascript常用算法,结合实例形式较为详细的分析总结了JavaScript中常见的各种排序算法以及堆.栈.链表等数据结构的相关实现与 ...

随机推荐

  1. cssParser

    //cssParser.h #include<iostream> using namespace std;struct MyAttribute{ MyAttribute*  next; s ...

  2. Linux下system函数

    http://www.jb51.net/article/40517.htm   浅析如何在c语言中调用Linux脚本 http://blog.csdn.net/koches/article/detai ...

  3. struts2--标签取值

    OGNL的概念: OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调 ...

  4. codeforces 650 C. Watchmen(数学公式)

    C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...

  5. 【论文笔记】基于图机构的推荐系统:Billion-scale Commodity Embedding for E-commerce Recommendation in Alibaba

    论文:https://arxiv.org/abs/1803.02349    题外话: 阿里和香港理工联合发布的这篇文章,整体来说,还挺有意思的. 刚开始随便翻翻看看结构图的时候,会觉得:这也能发文章 ...

  6. stl_heap.h

    stl_heap.h // Filename: stl_heap.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http://b ...

  7. 如何用Mendeley引用目标期刊要求的参考文献格式

    如果我们要向目标的杂志期刊投稿,则需要采用该期刊的参考文献格式.我用的mendeley管理文献,不收费且使用方便.那么,我们如何用mendeley引用目标期刊的参考文献呢?以Applied energ ...

  8. Arc077_E Guruguru

    传送门 题目大意 有$m$个点编号从小到大按照顺时针编成了一个环,有一枚棋子,每次移动可以选择顺时针移动到下一个或者直接移动到编号为$x$的点,现在有$n-1$次数操作,第$i$次要把棋子从第$A_i ...

  9. 【LeetCode】024. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  10. chrome中的content script脚本文件

    打开chrome的devtools工具,sources下有一个Content script: 1 chrome插件开发过程中难免会遇到使用content script来操作页面的dom,在chrome ...