Numpy 随机二维数组 [英] Numpy Random 2D Array
本文介绍了Numpy 随机二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 numpy 中创建一个二维均匀随机数组,类似于:
I want to create a 2D uniformly random array in numpy which is something like:
A=[[a1,b1],
[a2,b2],
...,
[a99,b99]]
但我想要某个范围内的 A 列的值(比如 1-10 之间)和 B 在不同范围内的值(比如 11-20).
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).
如何在 Python 中获得它?
How would this be obtained in Python?
推荐答案
两种方式.
我们可以用正确分配的 low
和 high
值堆叠两个随机数组 -
We could stack two random arrays with properly assigned low
and high
values -
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]))
另一种方法是使用 [1,10]
间隔创建二维随机数组,然后为第二列添加 10
偏移量,从而得到 [11,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 -
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]))
这篇关于Numpy 随机二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文