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.

9
  • 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? Commented Jan 22, 2018 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. Commented Jan 22, 2018 at 10:25
  • have a look on the accepted answer here Commented Jan 22, 2018 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? Commented Jan 22, 2018 at 10:34
  • Where does the -1 come from? Sounds like NaN (np.nan) was maybe more fitting. Commented Jan 22, 2018 at 10:37

1 Answer 1

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.        ]
Sign up to request clarification or add additional context in comments.

1 Comment

ValueError: could not broadcast input array from shape (55) into shape (69) because I've a 2D array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.