python - Error when indexing with 2 dimensions in NumPy -
why work:
>>> (tf[:,[91,1063]])[[0,3,4],:] array([[ 0.04480133, 0.01079433], [ 0.11145042, 0. ], [ 0.01177578, 0.01418614]])
but not:
>>> tf[[0,3,4],[91,1063]] indexerror: shape mismatch: indexing arrays not broadcast shapes (3,) (2,)
what doing wrong?
tf[:,[91,1063]])[[0,3,4],:]
operates in 2 steps, first selecting 2 columns, , 3 rows result
tf[[0,3,4],[91,1063]]
tries select tf[0,91]
, tf[3,1063]
, ft[4, oops]
.
tf[[[0],[3],[4]], [91,1063]]
should work, giving same result first expression. think of 1st list being column, selecting rows.
tf[np.array([0,3,4])[:,newaxis], [91,1063]]
is way of generating column index array
tf[np.ix_([0,3,4],[91,1063])]
np.ix_
can generate these index arrays.
in [140]: np.ix_([0,3,4],[91,1063]) out[140]: (array([[0], [3], [4]]), array([[ 91, 1063]]))
these column , row arrays broadcast produce 2d array of coordinates
[[(0,91), (0,1063)] [(3,91), ... ] .... ]]
this relevant part of docs: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing
i'm repeating answer composite index updates numpy matrices
Comments
Post a Comment