简体   繁体   English

从 ndarray 生成引导样本

[英]Generate bootstrap sample from ndarray

Is there a way to generate a bootstrap sample on an N-dimensional array?有没有办法在 N 维数组上生成引导样本? I am limited to using numpy==1.19.4我仅限于使用 numpy==1.19.4

I have already tried using a for loop on the other dimensions to no avail, but the following works for 1-dimensional arrays.我已经尝试在其他维度上使用 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)]

If I get your problem, I use to apply this method:如果我遇到您的问题,我会使用此方法:

suppose you have this multi-dimensionale array:假设你有这个多维数组:

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

you can sample elements with bootstrap in this way:您可以通过这种方式使用引导程序对元素进行采样:

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

what I'm doing is to randomly extract indices (randint) with replacement and finally reshape the sampling to obtain 99 bootstrapped dataset with the same dimensions of the original one.我正在做的是随机提取带有替换的索引(randint),最后重塑采样以获得与原始数据集相同尺寸的 99 个自举数据集。

Note that by this procedure you are considering as "elements" the arrays along the first ax and so each element that you are sampling have shape (3,2).请注意,通过此过程,您将 arrays 视为沿第一个轴的“元素”,因此您采样的每个元素都具有形状 (3,2)。

I hope that is clear, but if you have any doubt please let me know.我希望这很清楚,但如果您有任何疑问,请告诉我。

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

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