简体   繁体   English

Numpy随机矩阵数组

[英]Numpy array of random matrices

I'm new to python/numpy and I need to create an array containing matrices of random numbers. 我是python / numpy的新手,我需要创建一个包含随机数矩阵的数组。

What I've got so far is this: 到目前为止我得到的是:

for i in xrange(samples):
    SPN[] = np.random.random((6,5)) * np.random.randint(0,100)

Which make sense for me as PHP developer but is not working for python. 这对我作为PHP开发人员有意义,但不适用于python。 So how do I create a 3 dimensional array to contain this matrices/arrays? 那么如何创建一个包含这个矩阵/数组的三维数组呢?

Both np.random.randint and np.random.uniform , like most of the np.random functions, accept a size parameter, so in numpy we'd do it in one step: 像大多数np.random函数一样, np.random.randintnp.random.uniform都接受一个size参数,所以在numpy我们只需要一步完成:

>>> SPN = np.random.randint(0, 100, (3, 6, 5))
>>> SPN
array([[[45, 95, 56, 78, 90],
        [87, 68, 24, 62, 12],
        [11, 26, 75, 57, 12],
        [95, 87, 47, 69, 90],
        [58, 24, 49, 62, 85],
        [38,  5, 57, 63, 16]],

       [[61, 67, 73, 23, 34],
        [41,  3, 69, 79, 48],
        [22, 40, 22, 18, 41],
        [86, 23, 58, 38, 69],
        [98, 60, 70, 71,  3],
        [44,  8, 33, 86, 66]],

       [[62, 45, 56, 80, 22],
        [27, 95, 55, 87, 22],
        [42, 17, 48, 96, 65],
        [36, 64,  1, 85, 31],
        [10, 13, 15,  7, 92],
        [27, 74, 31, 91, 60]]])
>>> SPN.shape
(3, 6, 5)
>>> SPN[0].shape
(6, 5)

.. actually, it looks like you may want np.random.uniform(0, 100, (samples, 6, 5)) , because you want the elements to be floating point, not integers. ..实际上,看起来你可能想要np.random.uniform(0, 100, (samples, 6, 5)) ,因为你希望元素是浮点数,而不是整数。 Well, it works the same way. 嗯,它的工作方式相同。 :^) :^)


Note that what you did isn't equivalent to np.random.uniform , because you're choosing an array of values between 0 and 1 and then multiplying all of them by a fixed integer. 请注意,您所做的并不等同于np.random.uniform ,因为您选择的值介于0和1之间,然后将所有值乘以固定的整数。 I'm assuming that wasn't actually what you were trying to do, because it's a little unusual; 我假设那不是你想要做的事情,因为它有点不寻常; please comment if that is what you actually wanted. 请评论,如果这你真正想要的。

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

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