来自另一个numpy数组中对象属性的新numpy数组 [英] New numpy array from attribute of objects in another numpy array

查看:80
本文介绍了来自另一个numpy数组中对象属性的新numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从numpy数组中提取按元素的属性?例如,说我有:

Is there a way to do an element-wise attribute extraction from a numpy array? For example, say I have:

import numpy as np

class foo():
    def __init__(self, value):
        self.bar = value

obj_array = np.empty((2, 2), dtype='object')
for i in range(obj_array.shape[0]):
    for j in range(obj_array.shape[1]):
        obj_array[i, j] = foo(i+j)

bar_array_hard_way = np.empty_like(obj_array)
for i in range(obj_array.shape[0]):
    for j in range(obj_array.shape[1]):
        bar_array_hard_way[i, j] = obj_array[i, j].bar

在这里,我有一个对象数组.每个对象都有一些属性.我希望有一种精巧的内置方法可以将这些属性提取为新的numpy数组.显然,这是一个非常简单的示例,但是较大的数组按元素进行复制非常烦人.

Here, I have an array of objects. Each object has some attribute. I'm hoping there is a slick built-in way of extracting those attributes as a new numpy array. Obviously this is a pretty trivial example, but larger arrays the element-wise copying is pretty annoying.

推荐答案

我认为最快的方法是将Python的operator.attrgetter与numpy的np.frompyfunction结合在一起-第一种方法提供了一种内联的快速本机代码,用于检索对象的属性.第二,将普通的Python函数转换为Numpy的广播函数,该函数可以在一个调用中处理整个数组-

I think the fastest way will be to combine Python's operator.attrgetter with numpy's np.frompyfunction - the first gives a fast, native code inlined, way to retrieve an object's attribute. The second, transforms an ordinary Python function into a Numpy's broadcast function, which can process an entire array in a single call -

所以,您的电话是:

from operator import attrgetter
import numpy as np
# code to build obj_array
...
bar_array_easy_way = np.frompyfunc(attrgetter("bar"), 1, 1)(obj_array)

与使用fromtiterator进行快速比较,用一半的时间就从我的对象中构建了一个100万个int数组-此外,fromiterator无法使用dtype=object来构建数组-只是固定大小的元素.

Quickly comparing it against using fromtiterator built a 1 million int array from my objects in half the time - besides, fromiterator can't build arrays with dtype=object - just fixed size elements.

请注意,attrgetter本身是一个函数工厂",它具有一个属性名称,并返回一个将包含任何对象并返回该属性的函数.我们依次将返回的函数传递给frompyfunc,该函数接受其他2个参数以使numpy发挥其广播魔力:输入参数的数量和函数返回结果的数量.

Note that attrgetter itself is rather a "function factory" - it takes an attribute name, and returns a function that will take any object and return that attribute. That returned function we pass in turn, to frompyfunc - which takes other 2 arguments in order to allow numpy to make its broadcasting magic: the number of input arguments and the number of return results for our function.

这篇关于来自另一个numpy数组中对象属性的新numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆