在隔离森林中应该根据什么标准选择引导参数?

问题描述

如您所知,scikit-learn 中的隔离森林模型有一个参数,bootstrap。 描述如下。

如果为 True,则单个树适合于替换采样的训练数据的随机子集。如果为False,则进行无替换抽样。

我做了一个简单的数据并训练了一个隔离森林模型。但无论bootstrap = True还是False,评估结果都大不相同。请参考以下代码

import numpy as np
from sklearn.ensemble import IsolationForest

np.random.seed(0)

# making train and test data
size = 10
train_x = np.concatenate( (np.random.uniform(0,1,size=(size,1)),np.array([[100]]) ),axis=0,)
train_y = [1]*size + [-1]
test_x = np.concatenate((np.random.uniform(0,size = (size,np.array([[102]])),axis=0)
test_y = train_y.copy()

# defining accuracy
def accuracy(y_true,y_pred):
    return sum(1 for i in range(len(y_true)) if y_true[i] == y_pred[i] ) / len(y_true)

# when bootstrap = True
iso = IsolationForest(n_estimators = 100,max_samples= 4,max_features = 1.0,bootstrap = True,random_state= 0)
iso.fit(train_x)
predicted_y = iso.predict(test_x)
print(accuracy(test_y,predicted_y)) # 0.8182

# when bootstrap = False
iso = IsolationForest(n_estimators = 100,bootstrap = False,predicted_y)) # 1.0

我的问题是,

  1. bootstrap 参数在隔离林中的作用是什么?
  2. 应该根据什么标准在隔离林中选择引导程序参数?

请告诉我什么时候选择 True,什么时候选择 False。

解决方法

如果 bootstrap 设置为 False,那么您实际上创建了许多包含整个训练数据集的相同决策树。

随机森林样式模型的整个前提是从每棵树的数据集中提取引导样本(即带有替换),这使得模型的泛化能力比决策树要好得多。

长话短说,如果您希望森林成为合适的森林,则引导程序应始终设置为 True