What does the c underscore expression `c_` do exactly?

后端 未结 6 1953
长情又很酷
长情又很酷 2020-12-12 14:23

It seems to be some kind of horizontal concatenation, but I could not find any documentation online. Here a minimal working example:

In [1]: from numpy impo         


        
6条回答
  •  执念已碎
    2020-12-12 14:58

    It took me a lot of time to understand but it seems I finally got it.

    All you have to do is add along second axis.

    let's take :

    np.c_[np.array([1,2,3]), np.array([4,5,6])]
    

    But there isn't second axis. So we mentally add one.

    so shape of both array becomes (3,1).

    So resultant shape would be (3,1+1) which is (3,2). which is the shape of result -

    array([[1, 4],
           [2, 5],
           [3, 6]])
    

    Another ex.:

    np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
    

    shapes:

    np.array([[1,2,3]]) = 1,3

    np.array([[4,5,6]]) = 1,3

    0 so we can think of it as [[0]] = 1,1

    So result 1,3+1+1+3 = 1,8

    which is the shape of result : array([[1, 2, 3, 0, 0, 4, 5, 6]])

提交回复
热议问题