[英]Numpy Random 2D Array
I want to create a 2D uniformly random array in numpy which is something like:我想在 numpy 中创建一个二维均匀随机数组,类似于:
A=[[a1,b1],
[a2,b2],
...,
[a99,b99]]
But I want the values of the A column between a certain range (say between 1-10) and values of B within a different range (say 11-20).但我想要某个范围内的 A 列的值(比如 1-10 之间)和不同范围内的 B 的值(比如 11-20)。
How would this be obtained in Python?这将如何在 Python 中获得?
Two ways.两种方式。
We could stack two random arrays with properly assigned low
and high
values -我们可以用正确分配的
low
和high
值堆叠两个随机数组 -
In [39]: n = 10000 # no. of rows
In [40]: np.c_[np.random.randint(1,11,(n)), np.random.randint(11,21,(n))]
Out[40]:
array([[ 6, 19],
[ 8, 18],
[ 6, 11],
...,
[ 5, 12],
[10, 16],
[ 7, 17]])
In [41]: _.min(0), _.max(0) # verify
Out[41]: (array([ 1, 11]), array([10, 20]))
Another would be to create 2D random array with [1,10]
interval and then add 10
offset for the second column, thus getting us [11,20]
interval for it -另一种方法是创建具有
[1,10]
间隔的二维随机数组,然后为第二列添加10
偏移量,从而为它获得[11,20]
间隔 -
In [42]: np.random.randint(1,11,(n,2)) + [0,10]
Out[42]:
array([[10, 16],
[ 9, 12],
[ 4, 17],
...,
[ 7, 15],
[ 5, 11],
[ 4, 14]])
In [43]: _.min(0), _.max(0) # verify
Out[43]: (array([ 1, 11]), array([10, 20]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.