python - 在 Python 中实现引导程序的内存有效方式?
问题描述
我有一个数据集加载到内存中,并想从中引导。目前我使用 sklearnresample
默认实现。我使用生成器从驻留在内存中的数据集生成引导批处理。
然而,这非常耗费内存,因为每次引导迭代都会创建一个新数组。有没有更有效的方法来从存储在磁盘或内存中的数据实现引导程序?
解决方案
只需创建索引,这些索引将分割您的原始特征和目标,并在您每次需要引导数据集时应用它们。
这是使用 sklearn 的波士顿数据集的示例实现:
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)
推荐阅读
- php - 为 Symfony 翻译组件自动添加翻译标签到 Twig
- javascript - 具有文本 mime 类型并包含_任何_用户输入的网页的安全性如何
- html - 仅从自动格式化中禁用一个电话号码?
- excel - Excel VBA - 用另一个工作簿中的动态范围填充 ActiveX 组合框
- r - R中多个事件的百分比变化
- python - 如何告诉根进程使用 anaconda python 安装而不是 /usr/bin/python?
- python - 您可以按字母顺序将字典键排序到列表变量中并使用特定输出进行打印吗?
- python - 使用 CouchDB-Python 批量取消删除 CouchDB 文档
- python - 使用 conda 在 Visual Studio python 项目中安装 PyTorch
- python - 当有 None 时打印一些东西