numpy array slicing unxpected results

后端 未结 3 1707
不知归路
不知归路 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:04

    [:] creates a copy of your list ...

    so that is essentially the same as

    array[1] == array[:][1]
    

    which correctly returns in this case [4,5,6]

    while array[:,1] says return the first column which is indeed [2,5]

    eg

    a = [
          [1,2,3],
          [4,5,6]
        ]
    

    so as you can see column 0 (a[:,0] )would be [1,4] and column 2(a[:,2]) would be [3,6]

    meanwhilea[1] refers to the row 1 (or [4,5,6]) and a[0] would be the 0 row (or [1,2,3])

提交回复
热议问题