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
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.
obj_array.size[0]is invalid ... you meanobj_array.shaperecarrayperhaps?