【问题标题】:Isolation forest - understanding the plot隔离森林 - 了解剧情
【发布时间】:2018-11-09 08:40:41
【问题描述】:

我是隔离林的初学者,我从这个链接中的教程开始:

https://www.kaggle.com/rgaddati/unsupervised-fraud-detection-isolation-forest

我不明白路径长度的第一个图。棕色是什么意思?

感谢您的帮助

##All General Import Statements
import pandas as pd
import numpy as np
import math
import random
%matplotlib inline
import random
from matplotlib import pyplot
import os
print(os.listdir("../input"))
class ExNode:
    def __init__(self,size):
        self.size=size

class InNode:
    def __init__(self,left,right,splitAtt,splitVal):
        self.left=left
        self.right=right
        self.splitAtt=splitAtt
        self.splitVal=splitVal

def iForest(X,noOfTrees,sampleSize):
    forest=[]
    hlim=math.ceil(math.log(sampleSize,2))
    for i in range(noOfTrees):
        X_train=df_data.sample(sampleSize)
        forest.append(iTree(X_train,0,hlim))
    return forest

def pathLength(x,Tree,currHeight):
    if isinstance(Tree,ExNode):
        return currHeight
    a=Tree.splitAtt
    if x[a]<Tree.splitVal:
        return pathLength(x,Tree.left,currHeight+1)
    else:
        return pathLength(x,Tree.right,currHeight+1)

df=pd.read_csv("../input/creditcard.csv")
y_true=df['Class']
df_data=df.drop('Class',1)

sampleSize=10000
ifor=iForest(df_data.sample(100000),10,sampleSize) ##Forest of 10 trees

posLenLst=[]
negLenLst=[]

for sim in range(1000):
    ind=random.choice(df_data[y_true==1].index)
    for tree in ifor:
        posLenLst.append(pathLength(df_data.iloc[ind],tree,0))

    ind=random.choice(df_data[y_true==0].index)
    for tree in ifor:
        negLenLst.append(pathLength(df_data.iloc[ind],tree,0))

bins = np.linspace(0,math.ceil(math.log(sampleSize,2)), math.ceil(math.log(sampleSize,2)))

pyplot.figure(figsize=(12,8))
pyplot.hist(posLenLst, bins, alpha=0.5, label='Anomaly')
pyplot.hist(negLenLst, bins, alpha=0.5, label='Normal')
pyplot.xlabel('Path Length')
pyplot.ylabel('Frequency')
pyplot.legend(loc='upper left')

【问题讨论】:

  • 请包含相关代码,因为链接可能会过时。
  • 您是否尝试联系教程作者?
  • 我添加了代码,老实说,我并没有尝试联系教程作者..
  • 我无法联系到教程作者,页面无法正常工作..

标签: machine-learning unsupervised-learning


【解决方案1】:

棕色看起来像是棕褐色和蓝色的混合。所以我认为这就是它们重叠的地方。这很明显,因为每个的 alpha 都是 0.5

进一步解释:颜色在它们重叠的地方混合。它绘制蓝色然后如果棕褐色也在那里它绘制它在顶部。由于颜色的 alpha 为 0.5,其中 alpha 是透明度,因此颜色正在混合。导致它们重叠的地方变成棕色。尝试分别绘制每一个,你会发现它们会重叠。

【讨论】:

  • 您能解释一下吗?我还是不明白
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2019-07-11
  • 2019-06-29
  • 2020-11-16
  • 2019-08-02
  • 2019-07-20
  • 2017-08-21
相关资源
最近更新 更多