RuntimeWarning: invalid value found in long_scalars

I had a problem modifying the array by adding the percentage of each element compared to its row to the new matrix. This is an error indicating the code:

for j in range(1,27):
        for k in range(1,27):
                let_prob[j,k] = let_mat[j,k]*100/(let_mat[j].sum())

I get an error message:

RuntimeWarning: invalid value found in long_scalars

I tried to round the denominator without success.

+4
source share
2 answers

, , RuntimeWarning RuntimeWarning. , np.sum(), , , , 100 :

col, row = np.shape(let_mat) 
let_prob = np.round((let_mat/np.repeat(let_mat.sum(axis=1),row).reshape(col, row).astype(‌​float))*100,2)

:

>>> a = np.arange(20).reshape(4,5)
>>> 
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

>>> np.round((a/np.repeat(a.sum(axis=1),5).reshape(4,5).astype(float))*100,2)
array([[  0.  ,  10.  ,  20.  ,  30.  ,  40.  ],
       [ 14.29,  17.14,  20.  ,  22.86,  25.71],
       [ 16.67,  18.33,  20.  ,  21.67,  23.33],
       [ 17.65,  18.82,  20.  ,  21.18,  22.35]])
+1

.

, , - np.nan - .

0

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


All Articles