0

I've this numpy array:

array([ 0.49010508,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.09438115,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        ])

which is the first row of the 5D numpy array called allSimilarity. I have defined it with np.full() and the fill_value is -1. After computing, I would want to remove last unuseful -1 value. So, I calculate the size difference, but when I use np.delete() or np.resize() or allSimilarity[index1][index2][index3][index4] = allSimilarity[index1][index2][index3][index4][:diff].copy() (where diff is the size difference between old size and new size) I got this error:

ValueError: could not broadcast input array from shape (55) into shape (67)

Any advice?

Thanks in advance.

Federico Cuozzo
  • 363
  • 1
  • 7
  • 20
  • of course you can't remove it (and therefore resize it) if there are more dimensions in which rely data which would remain in the old shape. That's what the Error told you. Maybe give us more insights into the other data? – Nico Albers Jan 22 '18 at 10:21
  • You cannot resize a single row in a ND array, if that is what you are trying to do. Of course you can resize all rows - but all of them must have the same size. – MB-F Jan 22 '18 at 10:25
  • have a look on [the accepted answer here](https://stackoverflow.com/questions/43977463/valueerror-could-not-broadcast-input-array-from-shape-224-224-3-into-shape-2) – Nico Albers Jan 22 '18 at 10:25
  • So, the unique way to go ahead is to exclude the `-1` value when computing? Could I "mask" `-1` value before computing mean, or any other math operation? – Federico Cuozzo Jan 22 '18 at 10:34
  • Where does the `-1` come from? Sounds like NaN (`np.nan`) was maybe more fitting. – Alfe Jan 22 '18 at 10:37
  • Replaced `-1` values with `np.nan` values! `np.nan` values in math operations won't be including in computing? – Federico Cuozzo Jan 22 '18 at 10:51
  • Hi @FedericoCuozzo What is the size of your array? –  Jan 22 '18 at 10:58
  • Any number being operated with `nan` will result in `nan`. `nan+100 = nan`, etc. –  Jan 22 '18 at 11:09
  • If I try to call `mean()` method on 2d array I got `ValueError: cannot convert float NaN to integer` error. If I try to call `nanmean()` method, I got `AttributeError: 'numpy.ndarray' object has no attribute 'nanmean'` error. – Federico Cuozzo Jan 22 '18 at 17:52

1 Answers1

1

Hope this helps.

import numpy as np

j = np.array([ 0.49010508,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.09438115,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        ])

j = j[j!=-1]
print j

Result:

[ 0.49010508  0.          0.          0.          0.          0.          0.
  0.          0.09438115  0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.        ]
Rakesh
  • 81,458
  • 17
  • 76
  • 113