繁体   English   中英

从 ndarray 生成引导样本

[英]Generate bootstrap sample from ndarray

有没有办法在 N 维数组上生成引导样本? 我仅限于使用 numpy==1.19.4

我已经尝试在其他维度上使用 for 循环无济于事,但以下适用于一维 arrays。

 import numpy as np # Set random state and number of resamples random.seed(random_state) n_resamples = 9999 # Generate data data_1d = np.arange(2, 3, 0.1) data_nd = np.random.default_rng(42).random((2,3,2)) data = data_1d.copy() # Resample the data with replacement, computing the test statistic for each set of resamples bs_samples = [np.std(np.random.choice(data, size=len(data))) for _ in range(n_resamples)]

如果我遇到您的问题,我会使用此方法:

假设你有这个多维数组:

 data_nd = np.random.rand(100, 3, 2) data_nd.shape #(100, 3, 2)

您可以通过这种方式使用引导程序对元素进行采样:

 n_resamples = 99 data_nd[np.random.randint(len(data_nd), size=len(data_nd)*n_resamples)].reshape(n_resamples, *data_nd.shape).shape

我正在做的是随机提取带有替换的索引(randint),最后重塑采样以获得与原始数据集相同尺寸的 99 个自举数据集。

请注意,通过此过程,您将 arrays 视为沿第一个轴的“元素”,因此您采样的每个元素都具有形状 (3,2)。

我希望这很清楚,但如果您有任何疑问,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM