简体   繁体   English

使用来自不同范围的随机数生成 numpy 数组的每一列

[英]Generate each column of the numpy array with random number from different range

How to generate a numpy array such that each column of the array comes from a uniform distribution within different ranges efficiently?如何生成一个 numpy 数组,使得数组的每一列都有效地来自不同范围内的均匀分布? The following code uses two for loop which is slow, is there any matrix-style way to generate such array faster?下面的代码使用了两个很慢的 for 循环,有没有矩阵式的方法可以更快地生成这样的数组? Thanks.谢谢。

import numpy as np
num = 5
ranges = [[0,1],[4,5]]
a = np.zeros((num, len(ranges)))
for i in range(num):
    for j in range(len(ranges)):
        a[i, j] = np.random.uniform(ranges[j][0], ranges[j][1])

What you can do is produce all random numbers in the interval [0, 1) first and then scale and shift them accordingly:您可以做的是首先生成区间 [0, 1) 中的所有随机数,然后相应地缩放和移动它们:

import numpy as np
num = 5
ranges = np.asarray([[0,1],[4,5]])
starts = ranges[:, 0]
widths = ranges[:, 1]-ranges[:, 0]
a = starts + widths*np.random.random(size=(num, widths.shape[0]))

So basically, you create an array of the right size via np.random.random(size=(num, widths.shape[0])) with random number between 0 and 1. Then you scale each value by a factor corresponding to the width of the interval that you actually want to sample.所以基本上,你通过np.random.random(size=(num, widths.shape[0]))创建一个大小合适的数组,随机数在 0 到 1 之间。然后你将每个值按对应于您实际想要采样的间隔的宽度。 Finally, you shift them by starts to account for the different starting values of the intervals.最后,您将它们按starts以考虑间隔的不同起始值。

numpy.random.uniform will broadcast its arguments, it can generate the desired samples by passing the following arguments: numpy.random.uniform将广播它的参数,它可以通过传递以下参数来生成所需的样本:

  • low : the sequence of low values. lowlow的序列。
  • high : the sequence of high values. high :高值的序列。
  • size : a tuple like (num, m) , where m is the number of ranges and num the number of groups of m samples to generate. size :像(num, m)这样的元组,其中m是范围的数量, num是要生成的m样本组的数量。

For example:例如:

In [23]: num = 5

In [24]: ranges = np.array([[0, 1], [4, 5], [10, 15]])

In [25]: np.random.uniform(low=ranges[:, 0], high=ranges[:, 1], size=(num, ranges.shape[0]))
Out[25]: 
array([[  0.98752526,   4.70946614,  10.35525699],
       [  0.86137374,   4.22046152,  12.28458447],
       [  0.92446543,   4.52859103,  11.30326391],
       [  0.0535877 ,   4.8597036 ,  14.50266784],
       [  0.55854656,   4.86820001,  14.84934564]])

暂无
暂无

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

相关问题 生成一个新的2阶numpy数组,该数组用一定范围内的随机数填充每个元素 - Generate a new numpy array of order 2 filling in each element with a random number in a certain range 使用 Numpy 生成范围内的随机数 - Generate Random Number within range using Numpy 从 numpy 数组的每一列中选择随机元素 - selecting random elements from each column of numpy array 从数组的每一列中减去一个不同的数字 - Subtract a different number from each column in an array 从给定的元素列表生成随机的numpy数组,每个元素至少重复一次 - Generate random numpy array from a given list of elements with at least one repetition of each element 用另一个范围的每行随机整数创建numpy数组 - Create numpy array with random integers each row with another range 如何生成一个新数组,其中每个元素都是 numpy 一维数组的局部范围内的最大数 - How do I generate a new array that each element is the the maximum number within a local range of a numpy 1d array 对每列具有不同阈值的 numpy 数组进行阈值处理 - Thresholding a numpy array with different threshold on each column 为数组中的每个槽分配不同的随机数 - Assigning different random number to each slot in an array 使用 numpy 操作从每行填充 numpy 数组(不包括填充)和未填充值的数量中获取 Select 的最快方法 - Fastest way to Select a random number from each row padded numpy array (excluding the pad) and number of non padded values, using numpy operations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM