Many random matrices

I am new to python / numpy and I need to create an array containing matrices of random numbers.

What I have so far:

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

Which makes sense to me as a PHP developer, but doesn't work on python. So, how do I create a three-dimensional array containing these matrices / arrays?

+6
source share
1 answer

Both np.random.randint and np.random.uniform , like most np.random functions, accept the size parameter, so in numpy we will do it in one step:

 >>> 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 might want np.random.uniform(0, 100, (samples, 6, 5)) because you want the elements to be a floating point, not integers. Well, this works the same way.: ^)


Note that what you did is not equivalent to np.random.uniform , because you select an array of values ​​from 0 to 1, and then multiply them by a fixed integer. I assume that this is actually not what you tried to do, because it is a little unusual; comment if this is what you really wanted.

+16
source

Source: https://habr.com/ru/post/958094/


All Articles