python - how to extract a zero-dimensional slice from a 1-dimensional array in numpy -
is there way piece zero-dimensional sub-array 1-dimensional array?
for example, if have n-dimensional ndarray
arr
, arr[0]
returns (n-1)-dimensional ndarray
.
however, if have 1-dimensional ndarray
x
, x[0]
doesn't homecoming 0-dimensional ndarray, rather numpy.int64
, (if x
contains int64
s).
minimal example:
def increment(zero_d_array): zero_d_array[...] = zero_d_array + 1 counter = numpy.array(0) # zero-dimensional array containing scalar 0 increment(counter) # success; counter 1 counters = numpy.zeros(3, dtype=int) # [0, 0, 0] increment(counter[1]) # fails; counter[1] numpy.int64, not 0-d array
i realize above work increment(counter[1:2])
, because increment()
happens work both 0-d , 1-d inputs. not functions flexible.
use ellipsis:
increment(counter[1, ...])
python numpy slice matrix-indexing
No comments:
Post a Comment