Python 3.x 使用python并行化numpy数组中的属性分配对象
我正在寻找一种使用python将属性分配并行化到numpy数组中的对象的方法。在下面的示例中,我更新对象中每个ExampleObject的属性(attribute),给定一个大小相同的numpy数组,其中包含每个对象的新属性值Python 3.x 使用python并行化numpy数组中的属性分配对象,python-3.x,numpy,parallel-processing,variable-assignment,numpy-ndarray,Python 3.x,Numpy,Parallel Processing,Variable Assignment,Numpy Ndarray,我正在寻找一种使用python将属性分配并行化到numpy数组中的对象的方法。在下面的示例中,我更新对象中每个ExampleObject的属性(attribute),给定一个大小相同的numpy数组,其中包含每个对象的新属性值 import numpy as np import math import random class ExampleObject(): def __init__(self): self.attribute = random.random()
import numpy as np
import math
import random
class ExampleObject():
def __init__(self):
self.attribute = random.random()
rows, columns = (5, 5)
objects = np.empty((rows, columns), dtype=object)
objects.flat = [ExampleObject() for _ in objects.flat]
attributes = np.zeros((rows, columns))
attributes = np.vectorize(lambda x: x.attribute)(objects)
new_attributes = np.random.rand(rows, columns)
for i in range(rows):
for j in range(columns):
objects[i][j].attribute = new_attributes[i][j]
我知道这可以通过使用两个for循环来实现,我已经在上面包含了实现这一点的代码,但是,我找不到一种方法来并行化这段代码。我曾尝试使用多处理,但它似乎无法对我的代码进行pickle处理。
np.frompyfunc
似乎是操作自定义类对象数组最方便、最快的工具。它可以是列表理解速度的两倍(增益没有那么大),并且可以处理广播
您的课程有一个小的增强:
In [418]: class Foo():
...: def __init__(self, val):
...: self.attribute = val
...: def __repr__(self):
...: return f'<Foo> {self.attribute}'
...:
In [419]: Foo(1)
Out[419]: <Foo> 1
获取值:
In [426]: np.frompyfunc(lambda f: f.attribute,1,1)(arr)
Out[426]: array([0, 1, 2, 3, 4], dtype=object)
修改值(不能使用lambda执行此操作):
[427]中的:def setfoo(foo,val):
…:foo.attribute=val
...:
In[428]:np.frompyfunc(setfoo,2,0)
输出[428]:
在[429]中:(arr[10,np.one((3,2)),np.arange(3),None,'foobar')
Out[429]:()
In[430]:arr
出[430]:
数组([10,[[1.1.]
[1. 1.]
[1. 1.]], [0 1 2],
无,foobar],数据类型=对象)
二维阵列创建:
In [432]: M = np.frompyfunc(lambda i,j: Foo((i,j)), 2,1)(*np.ix_([1,2,3],[10,20]))
In [433]: M
Out[433]:
array([[<Foo> (1, 10), <Foo> (1, 20)],
[<Foo> (2, 10), <Foo> (2, 20)],
[<Foo> (3, 10), <Foo> (3, 20)]], dtype=object)
[432]中的:M=np.frompyfunc(lambda i,j:Foo((i,j)),2,1)(*np.ix_u40;[1,2,3],[10,20]))
In[433]:M
出[433]:
数组([(1,10)、(1,20)],
[ (2, 10), (2, 20)],
[(3,10)、(3,20)],数据类型=对象)
在for循环中使用对象进行操作有多重要?不幸的是,每个对象都需要有一个自包含的属性。理想情况下,对象[:][:]可以工作。属性=新属性[:][:]可以工作。fastnunpy
代码只适用于数字类型。除了重塑和切片之类的事情外,处理对象列表要比处理对象数组快np.frompyfunc
似乎是在数组中创建和操作对象的最佳工具。
In [426]: np.frompyfunc(lambda f: f.attribute,1,1)(arr)
Out[426]: array([0, 1, 2, 3, 4], dtype=object)
In [427]: def setfoo(foo, val):
...: foo.attribute = val
...:
In [428]: np.frompyfunc(setfoo,2,0)
Out[428]: <ufunc '? (vectorized)'>
In [429]: _(arr, [10,np.ones((3,2)),np.arange(3),None, 'foobar'])
Out[429]: ()
In [430]: arr
Out[430]:
array([<Foo> 10, <Foo> [[1. 1.]
[1. 1.]
[1. 1.]], <Foo> [0 1 2],
<Foo> None, <Foo> foobar], dtype=object)
In [432]: M = np.frompyfunc(lambda i,j: Foo((i,j)), 2,1)(*np.ix_([1,2,3],[10,20]))
In [433]: M
Out[433]:
array([[<Foo> (1, 10), <Foo> (1, 20)],
[<Foo> (2, 10), <Foo> (2, 20)],
[<Foo> (3, 10), <Foo> (3, 20)]], dtype=object)