12

I'm having an issue with modifying an array, by adding the percentage of each item compared to its row to a new matrix. This is the code providing error:

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 the error:

RuntimeWarning: invalid value encountered in long_scalars

I have tried rounding the denominator to no success.

bastelflp
  • 9,362
  • 7
  • 32
  • 67
twallien
  • 141
  • 1
  • 1
  • 4

2 Answers2

7

If it provides any help for other persons encountering this warning, it appeared in a similar context, where I was dividing a value with another, and the warning appeared in cases where I was dividing by 0. Adding a special-case with the value of the denominator, and avoiding a zero-division, made the warning disappear.

I don't know why that warning appeared instead of a ZeroDivisionError.

Gouvernathor
  • 92
  • 1
  • 12
3

It seems that you are dealing with big numbers, since it raised the error RuntimeWarning. To get rid of such errors, as a numpythonic way you can first calculate the sum of each row using the np.sum() function by specifying the proper axis then repeat and reshape the array in order to be able to divide with your array, them multiple with 100 and round the result:

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)

Demo :

>>> 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]])
lstodd
  • 168
  • 2
  • 9
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • @twallien You don't need to use loop, just change your array name with `a` – Mazdak Jan 10 '16 at 19:32
  • I tried with the replacement but due to the reshape it came out with a "ValueError: total size of new array must be unchanged". I tried replacing the values in reshape with np.shape(a) and the repeat argument with np.shape(a)[1] but still returned an error. – twallien Jan 10 '16 at 19:40
  • @twallien Is `a` your `let_mat` array? – Mazdak Jan 10 '16 at 20:38
  • yes, this is the code: `shape = np.shape(let_mat) let_prob = np.round((let_mat/np.repeat(let_mat.sum(axis=1),shape[1]).reshape(shape).astype(float))*100,2)` – twallien Jan 10 '16 at 20:46
  • @twallien I think you need to unpack your shape. Use `reshape(*shape)` – Mazdak Jan 10 '16 at 20:48
  • the shape is (27,27), and im still receiving a runtime error, due to "invalid value encountered in true_divide" – twallien Jan 10 '16 at 20:50
  • @twallien I just edited the comment, use unpacking operation. – Mazdak Jan 10 '16 at 20:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100315/discussion-between-twallien-and-kasramvd). – twallien Jan 10 '16 at 20:53