numpy array slicing unxpected results

后端 未结 3 1736
不知归路
不知归路 2021-01-23 07:17

I don\'t understand the behavior below. numpy arrays can generally be accessed through indexing, so [:,1] should be equivalent to [:][1], or so I thought. Could someone explain

3条回答
  •  日久生厌
    2021-01-23 08:03

    >>> a = np.array([[1, 2, 3], [4, 5, 6]])
    >>> a[:,1]
    array([2, 5])
    

    you select the second dimension (column of your matrix) and take the element 1 in this dimension. the same way, a[:,0] selects the first column, here array([1,4]), a[:,2] the third column.

    As was previously said, a[:] copies your list (be a numpy array or a list).

提交回复
热议问题