I would like to create an array which is filled with identical tuples initially, specifically with tuples of NaN
. Eg,
array([[(nan, nan), (nan, nan)],
[(nan, nan), (nan, nan)],
[(nan, nan), (nan, nan)]], dtype=object)
However, when using the array initialisations listed eg here with an iterable value as value for filling in array, python
apparently tries to reshape that iterable into the new array rather than fill it with it:
np.full([3,2],(np.nan,np.nan,np.nan),dtype=tuple)
#ValueError: could not broadcast input array from shape (3) into shape (3,2)
np.fill
does not work either, it requires a scalar.
Is it only possible to fill the array item by item?
You can, with the correct dtype
. With 'f,f'
you can initialise the array with tuples of floats
; see Data type objects (dtype) for more.
np.full((3,2), np.nan, dtype='f,f')
array([[(nan, nan), (nan, nan)],
[(nan, nan), (nan, nan)],
[(nan, nan), (nan, nan)]], dtype=[('f0', '<f4'), ('f1', '<f4')])
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.