4

I want to declare an array of object and later to include arrays in it. I can do it this way:

import numpy as np    
v = np.empty([2,2], dtype=object)
for i in range(len(v.flat)):
  v.flat[i] = np.ones([3])

But since Numpy has iterators, I wanted to use them:

v = np.empty([2,2], dtype=object)
for i in np.nditer(v, flags=['refs_ok'],op_flags=['readwrite']):
  i[...] = np.ones([3])

and the message is:

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

Can someone explain my how to do it correctly?

TIA

4
  • 3
    It seems that numpy detects an iterable in the assignment and tries to broadcast it. I am not sure myself how to do it properly, but i.fill(numpy.ones([3])) seems to work. Commented Jan 19, 2015 at 10:42
  • 1
    Thanks for this solution. Now if someone can explain me why i[...] = is wrong or if I should make a "bug" report. Commented Jan 19, 2015 at 11:11
  • @Olric It's only 'wrong' in the sense that numpy will try to broadcast when the argument on the RHS of the assignment is iterable. This type of behaviour is usually desirable (e.g. v[0, :2] = 1, 2), so I wouldn't regard it as a bug. Commented Jan 19, 2015 at 13:02
  • 1
    Creation of object arrays has been under recent discussion by the numpy dev team. If you have a github account, reporting this as an issue here will be very welcome. Commented Jan 19, 2015 at 16:15

2 Answers 2

4

And here is a solution I like:

I am honestly not sure if this makes more sense or not (I would say it probably makes sense). But you can use i[()] = ... since you want to do item assignment not view based/sliced assignment anyway.

Oh, and be careful with nditer and objects I forgot what the traps were, but I am pretty sure there are traps with the buffer and reference counts.

seberg from github

Sign up to request clarification or add additional context in comments.

1 Comment

I ran some quick time tests using your range and nditer cases (and larger arrays). The nditer approach was a bit faster, but probably not enough to justify the uncertainties.
0

I don't know if it is exactly what you are looking, but to have the same result using numpy iterator, the following code maybe could be your answer.

v = np.empty([2,2], dtype=object)

for idRow,idCol in np.ndindex(np.shape(v)):
    v[idRow,idCol] = np.ones(3)
    print(idRow, idCol)

If it's not what you are looking, be more specfici on your request

3 Comments

This solution is the same than my first example. It works but I want to use Numpy's iterators (I expect them to be faster). I also want to understand why it does not work :-)
I though that you want the same result as your first example. I used the numpy iterator, so I really don't know what you want. And the second doesn't work because you cannot assign to "i" the value.
I wouldn't called that an iterator even if ndindex documentation says you are right. For me an iterator is an object which is the cell of the array and not its index. nditer is what I call an iterator, the regular for i in list is an iterator but not for i in range(len(list)). Now I may be wrong.

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.