Arrays of objects

I recently ran into problems when creating arrays of Numpy objects, like

a = np.array([c], dtype=np.object) 

where c is an instance of some complex class, and in some cases, Numpy tries to access some methods of this class. However, by doing:

 a = np.empty((1,), dtype=np.object) a[0] = c 

solves the problem. I am curious what the difference is between the two internally. Why in the first case, will Numpy try to access some attributes or methods of c ?

EDIT: For the record, here is an example code that demonstrates the problem:

 import numpy as np class Thing(object): def __getitem__(self, item): print "in getitem" def __len__(self): return 1 a = np.array([Thing()], dtype='object') 

This prints getitem twice. Basically, if __len__ present in a class, then this happens when you may encounter unexpected behavior.

+7
source share
1 answer

In the first case, a = np.array([c], dtype=np.object) numpy knows nothing about the shape of the proposed array.

For example, when you define

 d = range(10) a = np.array([d]) 

Then you expect numpy to determine the shape based on length d .

Similarly, in your case, numpy will try to determine if len(c) exists, and if so, access the elements of c through c[i] .

You can see the effect by defining a class, for example

 class X(object): def __len__(self): return 10 def __getitem__(self, i): return "x" * i 

Then

 print numpy.array([X()], dtype=object) 

produces

 [[ x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx]] 

On the contrary, in the second case

 a = np.empty((1,), dtype=np.object) a[0] = c 

Then the form a already defined. That way numpy can just assign an object directly.

However, to some extent this is true only when a is a vector. If it were defined with a different form, then access to the methods would still occur. The following, for example, will call ___getitem__ in the class

 a = numpy.empty((1, 10), dtype=object) a[0] = X() print a 

returns

 [[ x xx xxx xxxx xxxxx xxxxxx xxxxxxx xxxxxxxx xxxxxxxxx]] 
+9
source

All Articles