Answers for "how to divide array in three equal arrays in numpy"

0

numpy array divide each row by its sum

>>> e
array([[ 0.,  1.],
       [ 2.,  4.],
       [ 1.,  5.]])
# Method #1
>>> e/e.sum(axis=1)[:,None]
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

# Method #2
>>> (e.T/e.sum(axis=1)).T
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

# Method #3:
>>> e/e.sum(axis=1, keepdims=True)
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])
Posted by: Guest on October-20-2021
0

divide every element in numpy array

>>> x = np.arange(5)
>>> np.true_divide(x, 4)
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])
Posted by: Guest on June-26-2020

Code answers related to "how to divide array in three equal arrays in numpy"

Python Answers by Framework

Browse Popular Code Answers by Language