只需创建索引,它将分割原始特性和目标,并在每次需要引导数据集时应用这些索引。
import numpy as np
from sklearn.datasets import load_boston
def get_bootstrap_indices(dataset, datasets: int):
for _ in range(datasets):
yield np.random.choice(np.arange(len(dataset)), size=len(dataset), replace=True)
dataset = load_boston().data
print(f"Original dataset shape: {dataset.shape}")
for indices in get_bootstrap_indices(dataset, 10):
print(dataset[indices].shape)