-1

I am working with a massive NDArray and am trying to recurse over each individual integer within this nested array. The NDArray, called box_ids, looks like this:

[[[-1.]
  [-1.]
  [-1.]
  ...
  [-1.]
  [-1.]
  [-1.]]]

and when I write

for ids in box_ids[0]:
    print(ids)

I am returned:

<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[0.]
<NDArray 1 @cpu(0)>, 
[1.]
<NDArray 1 @cpu(0)>, 
[1.]

...over and over again.

I have tried box_ids.flatten() and this has yielded the same result. So, I went on to try .tolist() and .ravel(), but then I got the Attribute Error saying Numpy does not have these objects.

Basically, I want a list of all the individual integers in the array so that I can recurse over them. Ideally, a final list might look like [0, 0, 0, 1, 1, ...]. The fact that each element when I print(ids) has the newline and , is really confusing and I can't figure out how to get rid of this. When I print(ids), I simply want something that would look like: 0 0 1 1...

I hope this makes sense. Thanks so much for the help if you can!!

hpaulj
  • 221,503
  • 14
  • 230
  • 353
cbnnbc
  • 35
  • 3

1 Answers1

3

You can use mxnet.ndarray.squeeze to remove unused nested dimensions, then convert to numpy array with mxnet.ndarray.asnumpy to extract the list of values, like this:

box_ids.squeeze().asnumpy().tolist()

As mentioned in the comment above, please note that even though MXNet NDArrays have by design an API extremely similar to Numpy arrays, they are actually totally different libraries with different internals. See in this post more details about MXNet NDArray specificities.

NDArray really shines at doing things in a vectorized fashion over CPU or GPU, so before implementing a manual for loop over an NDArray consider if you can implement things in native mxnet NDArray only. This will likely be much faster since mxnet commands are ran asynchronously in C++

Olivier Cruchant
  • 3,747
  • 15
  • 18