Python 如何使用矢量化为numpy数组中的对象赋值

Python 如何使用矢量化为numpy数组中的对象赋值,python,numpy,Python,Numpy,我是python的初学者,我编写这段代码是为了创建一个二维numpy对象数组来模拟物理晶格 import numpy as np class Site: def __init__(self, label, status): self.l = label self.s = status vSite = np.vectorize(Site(0,2), otypes=[object]) init_arra

我是python的初学者,我编写这段代码是为了创建一个二维numpy对象数组来模拟物理晶格

    import numpy as np  

    class Site:
       def __init__(self, label, status):
          self.l = label
          self.s = status

     vSite = np.vectorize(Site(0,2), otypes=[object])
     init_array = np.arange(25).reshape((5,5))
     lattice = np.empty((5,5), dtype=object)
     lattice[:,:] = vSite(init_array)
但是我的输出有错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-0c0dfed8eab8> in <module>()
      9 init_array = np.arange(25).reshape((5,5))
     10 lattice = np.empty((5,5), dtype=object)
---> 11 lattice[:,:] = vSite(init_array)

~/.local/lib/python3.5/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
   2753             vargs.extend([kwargs[_n] for _n in names])
   2754 
-> 2755         return self._vectorize_call(func=func, args=vargs)
   2756 
   2757     def _get_ufunc_and_otypes(self, func, args):

~/.local/lib/python3.5/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
   2823             res = func()
   2824         else:
-> 2825             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2826 
   2827             # Convert args to object arrays first

~/.local/lib/python3.5/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
   2770                 ufunc = self._ufunc
   2771             else:
-> 2772                 ufunc = self._ufunc = frompyfunc(func, len(args), nout)
   2773         else:
   2774             # Get number of outputs and output types by calling the function on

TypeError: function must be callable
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
9初始数组=np.arange(25).重塑((5,5))
10晶格=np.空((5,5),数据类型=对象)
--->11晶格[:,:]=vSite(初始数组)
~/.local/lib/python3.5/site-packages/numpy/lib/function\u base.py in\uu\u调用(self,*args,**kwargs)
2753变量扩展([kwargs[\n]表示名称中的变量])
2754
->2755返回自向量化调用(func=func,args=vargs)
2756
2757定义获取和输出类型(self、func、args):
调用中的~/.local/lib/python3.5/site-packages/numpy/lib/function\u base.py(self、func、args)
2823 res=func()
2824其他:
->2825 ufunc,otypes=self.\u获取ufunc和otypes(func=func,args=args)
2826
2827#首先将参数转换为对象数组
~/.local/lib/python3.5/site-packages/numpy/lib/function\u base.py in\u get\u ufunc\u和\u otypes(self、func、args)
2770 ufunc=自身
2771其他:
->2772 ufunc=self.\u ufunc=frompyfunc(func,len(args),nout)
2773其他:
2774#通过调用
TypeError:函数必须是可调用的
有人能帮我吗?

函数必须以函数作为第一个参数,而不是公共变量。然后,调用它的函数将能够在numpy数组上调用,以将其应用于数组的每个元素

如果要初始化3D numpy数组,应按如下所示使用
np.empty(dim)
函数:

a=np.empty((n,m,l), dtype=object)
此数组将有n*m*l个值。
然后,您可以使用循环遍历矩阵以填充它:

for i in np.ndindex(a.shape):
    a[i] = Site(1,1)

我发现
np.frompyfunc
是创建自定义类数组的最佳工具。使用
np.vectorize
也可以,因为您指定了
otypes
,但是
frompyfunc
已经返回了对象,而且更直接、更快

In [667]: class Site: 
     ...:        def __init__(self, label, status): 
     ...:           self.l = label 
     ...:           self.s = status 
     ...:        def __repr__(self):   # to improve display
     ...:            return f'Site({self.l},{self.s})' 
     ...:                                                                            
In [668]: f = np.frompyfunc(Site, 2,1)                                               
In [669]: f(np.zeros((2,3),int), np.ones((2,3),int)*2)                               
Out[669]: 
array([[Site(0,2), Site(0,2), Site(0,2)],
       [Site(0,2), Site(0,2), Site(0,2)]], dtype=object)
In [670]: f(np.arange(3),np.array(['a','b','c']))                                    
Out[670]: array([Site(0,a), Site(1,b), Site(2,c)], dtype=object)

不过,我应该提醒您,访问这些
站点
对象还需要使用
frompyfunc
。对象数组没有充分利用
numpy
计算速度
numpy
在处理数字而不是对象时,计算速度最快。

但是已经使用np.empty进行了初始化。你能用一个可运行的例子来回答吗?你可能应该只使用
list
对象,而不是
numpy