首页 > 解决方案 > 在隔离林中应该根据什么标准选择引导参数?

问题描述

如您所知,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,1,size = (size,1)), 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, max_samples= 4, max_features = 1.0, bootstrap = False, random_state= 0)
iso.fit(train_x)
predicted_y = iso.predict(test_x)
print(accuracy(test_y, predicted_y)) # 1.0

我的问题是,

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

请让我知道何时选择 True 以及何时选择 False。

标签: pythonmachine-learningscikit-learnisolation-forest

解决方案


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

随机森林风格模型的整个前提是从数据集中为每棵树获取一个引导样本(即有替换),这使得模型比决策树能够更好地泛化。

长话短说,如果您希望 Forest 成为适当的 Forest,则 bootstrap 应始终设置为True.


推荐阅读