简体   繁体   中英

Numpy array with numpy arrays as objects

I'd like to create a numpy ndarray with entries of type ndarray itself. I was able to wrap ndarrays into another type to get it work but I want to do this without wrapping. With wrapping a ndarray x into eg the dictionary {1:x} I can do

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). When change F to F = np.vectorize(lambda x: np.repeat(x,3)) numpy complains 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?

Thanks a lot in advance for hints :)

You can (ab-)use 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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