Skip to content Skip to sidebar Skip to footer

Divide Every Item In An Array Of Arrays In One Shot

I have an numpy aray of shape 3X4X4 as shown: [[[0 0 0 2] [0 0 0 0] [1 0 0 0] [0 0 0 0]] [[0 1 0 0] [0 0 0 0] [0 0 0 0] [0 1 1 0]] [[0 0 0 0] [0 1 1 0] [0 0 1 0

Solution 1:

You can simply do:

numpy.log(yourNumpyArray / 0.25)

And numpy will do the right thing (divide each element by 0.25)

Read more:


Solution 2:

I dont see what is wrong with this

>> import numpy as np
>> a = np.array([[[0, 0, 0, 2],[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ,[0, 1, 1, 0]] ,[[0, 0, 0, 0], [0, 1, 1, 0] ,[0, 0, 1, 0] ,[0, 0, 0, 0]]])
>> a
[[[0, 0, 0, 2], [0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]],
 [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 1, 0]],
 [[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]]]

>> np.log(a/0.25)

array([[[       -inf,        -inf,        -inf,  2.07944154],
        [       -inf,        -inf,        -inf,        -inf],
        [ 1.38629436,        -inf,        -inf,        -inf],
        [       -inf,        -inf,        -inf,        -inf]],

       [[       -inf,  1.38629436,        -inf,        -inf],
        [       -inf,        -inf,        -inf,        -inf],
        [       -inf,        -inf,        -inf,        -inf],
        [       -inf,  1.38629436,  1.38629436,        -inf]],

       [[       -inf,        -inf,        -inf,        -inf],
        [       -inf,  1.38629436,  1.38629436,        -inf],
        [       -inf,        -inf,  1.38629436,        -inf],
        [       -inf,        -inf,        -inf,        -inf]]])

Solution 3:

import numpy as np

arr = np.array([[[0, 0, 0, 2],[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0] ,[0, 1, 1, 0]] ,[[0, 0, 0, 0], [0, 1, 1, 0] ,[0, 0, 1, 0] ,[0, 0, 0, 0]]])
arr /= 0.25
arr = np.log(arr)

Post a Comment for "Divide Every Item In An Array Of Arrays In One Shot"