Using NumPy Array as Indices for the 2nd Dimension of Another Array
To extract specific elements from a 2D array based on indices provided by a second array, you can leverage NumPy's integer array indexing.
Consider this示例:
,[2,3],[4,5]]) b = np.Array([[1],[0],[1],[1],dtype ='int')A = np.array([[0,1], [2,3], [4,5]]) B = np.array([[1], [0], [1]], dtype='int'),[2],[2],[2],[2],[5],[5],[5]])方法:
),b.ravel()]
扁平b,删除任何其他维度。C = np.array([[1], [2], [5]])
A[np.arange(A.shape[0]),B]
A[np.arange(A.shape[0]),B.ravel()]
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3