简体   繁体   English

以 numpy 数组作为对象的 Numpy 数组

[英]Numpy array with numpy arrays as objects

I'd like to create a numpy ndarray with entries of type ndarray itself.我想用 ndarray 本身类型的条目创建一个 numpy ndarray。 I was able to wrap ndarrays into another type to get it work but I want to do this without wrapping.我能够将 ndarrays 包装成另一种类型以使其工作,但我想在不包装的情况下执行此操作。 With wrapping a ndarray x into eg the dictionary {1:x} I can do将 ndarray x包装到例如字典{1:x}我可以做到

F = np.vectorize(lambda x: {1:np.repeat(x,3)})
F(np.arange(9).reshape(3,3))

and get (3,3) ndarray with entries {1:[0,0,0]} ... {1:[8,8,8]} (with ndarrays).并使用条目{1:[0,0,0]} ... {1:[8,8,8]} (使用 ndarrays)获取 (3,3) ndarray。 When change F to F = np.vectorize(lambda x: np.repeat(x,3)) numpy complains ValueError: setting an array element with a sequence .当将F更改为F = np.vectorize(lambda x: np.repeat(x,3)) numpy 会抱怨ValueError: setting an array element with a sequence I guess it detects that the entries as arrays themselves and doesn't threat them as objects anymore.我想它会将条目检测为数组本身,并且不再威胁它们作为对象。

How can I avoid this and do the same thing without wrapping the entries from ndarray into something different?我怎样才能避免这种情况并在不将 ndarray 中的条目包装成不同的东西的情况下做同样的事情?

Thanks a lot in advance for hints :)非常感谢您的提示:)

You can (ab-)use numpy.frompyfunc :你可以(ab-)使用numpy.frompyfunc

>>> F = np.arange(9).reshape(3, 3)
>>> np.frompyfunc(F.__getitem__, 1, 1)(range(3))
array([array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])], dtype=object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM