Broadcasting Function Calls In Np.array
Solution 1:
A numpy array of objects does not inherit the methods of that object. ndarray
methods in general act on the entire array
This does not work for built-in types either, e.g.:
In [122]: import numpy as np
In [123]: n = 4.0
In [124]: a = np.arange(n)
In [125]: n.is_integer()
Out[125]: True
In [126]: a.is_integer()
---------------------------------------------------------------------------
AttributeError: 'numpy.ndarray' object has no attribute 'is_integer'
Numpy broadcasting is done with element-wise operators, for example addition:
In[127]: nOut[127]: 4.0In[128]: aOut[128]: array([ 0., 1., 2., 3.])
In[129]: n + aOut[129]: array([ 4., 5., 6., 7.])
If you want to basically call print
on all the elements in your array, you could simply redefine the .__repr__()
method which is called by print
. I would caution you that you will lose information by overriding the method.
In [148]: classplayer:
.....: def__init__(self, num=5):
.....: self.num = num
.....: def__repr__(self):
.....: returnstr(self.num)
.....:
In [149]: objs = np.array([player(5), player(6)])
In [150]: objs
Out[150]: array([5, 6], dtype=object)
In [151]: print objs
[56]
Even though it looks like it, this is not the same as np.array([5,6])
though:
In [152]: objs * 3
----------------------
TypeError: unsupported operand type(s) for *: 'instance' and 'int'
And there you can see the disadvantage of overriding __repr__
.
The simpler way to do this is to use your current printnum()
method, but call it in a loop:
In [164]: classplayer:
.....: def__init__(self, num=5):
.....: self.num = num
.....: defprintnum(self):
.....: print(self.num)
.....:
In [165]: for p inobjs:
.....: p.printnum()
.....:
56
Or, perhaps define your method to return a string rather than print one, then make a list comprehension:
In [169]: classplayer:
.....: def__init__(self, num=5):
.....: self.num = num
.....: defprintnum(self):
.....: return str(self.num)
.....:
In [170]: objs = np.array([player(5), player(6)])
In [171]: [p.printnum() for p in objs]
Out[171]: ['5', '6']
Solution 2:
Couple typos in your code: printnum()
needs self
arg, and Object
->object
classplayer:
def__init__(self, num=5):
self.num = num
defprintnum(self):
print(self.num)
objs = np.array([player(5),player(6)], dtype=object)
# It's not a "broadcast" (what you mean is map), but it has the same result# plus it's pythonic (explicit + readable)for o in objs:
o.printnum()
It looks like what you really want to do is create a generator object. Google python generator yield
and you'll get some examples like this
Post a Comment for "Broadcasting Function Calls In Np.array"