np.array中的广播函数调用 [英] Broadcasting function calls in np.array
问题描述
我正在尝试创建一个充满对象的NumPy数组,我想知道是否有一种方法可以将每个对象广播到整个数组以做某事.
I am trying creating an NumPy array filled with an object, and I was wondering if there was a way I could broadcast to the entire array for each object to do something.
代码:
class player:
def __init__(self,num = 5):
self.num = num
def printnum():
print(self.num)
...
objs = np.array([player(5),player(6)],dtype=Object)
objs.printnum()
按原样返回错误.我尝试按照手册将dtype更改为:_object,但似乎没有任何作用.
As it stands this returns an error. I have tried changing the dtype to: _object as per the manual, but nothing seems to work.
推荐答案
numpy对象数组不继承该对象的方法. ndarray
方法通常作用于整个数组
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]: n
Out[127]: 4.0
In [128]: a
Out[128]: array([ 0., 1., 2., 3.])
In [129]: n + a
Out[129]: array([ 4., 5., 6., 7.])
如果要对数组中的所有元素基本调用 print
,则可以简单地重新定义 .__ repr __()
方法,该方法由 print调用
.我会警告您,您将通过覆盖该方法而丢失信息.
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]: class player:
.....: def __init__(self, num=5):
.....: self.num = num
.....: def __repr__(self):
.....: return str(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
[5 6]
尽管看起来像,但它与 np.array([5,6])
不同:
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'
在那里,您会看到覆盖 __ repr __
的缺点.
And there you can see the disadvantage of overriding __repr__
.
更简单的方法是使用当前的 printnum()
方法,但要在循环中调用它:
The simpler way to do this is to use your current printnum()
method, but call it in a loop:
In [164]: class player:
.....: def __init__(self, num=5):
.....: self.num = num
.....: def printnum(self):
.....: print(self.num)
.....:
In [165]: for p in objs:
.....: p.printnum()
.....:
5
6
或者,也许定义您的方法以返回一个字符串而不是打印一个字符串,然后进行列表理解:
Or, perhaps define your method to return a string rather than print one, then make a list comprehension:
In [169]: class player:
.....: def __init__(self, num=5):
.....: self.num = num
.....: def printnum(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']
这篇关于np.array中的广播函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!