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

后端 未结 6 1936
长情又很酷
长情又很酷 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:44

    I would explain this as follow. It concats your first array into the last dimension (axis) of your last array in the function.

    For example:

    # both are 2 dimensional array
    a = array([[1, 2, 3], [4, 5, 6]])
    b = array([[7, 8, 9], [10, 11, 12]])
    

    Now, let's take a look at np.c_(a, b):

    First, let's look at the shape:

    The shape of both a and b are (2, 3). Concating a (2, 3) into the last axis of b (3), while keeping other axises unchanged (1) will become

    (2, 3 + 3) = (2, 6)
    

    That's the new shape.

    Now, let's look at the result:

    In b, the 2 items in the last axis are:

    1st: [7, 8, 9]
    2nd: [10, 11, 12]
    

    Adding a to it means:

    1st item: [1,2,3] + [7,8,9] = [1,2,3,7,8,9]
    2nd item: [4,5,6] + [10,11,12] = [4,5,6,10,11,12]
    

    So, the result is

    [
      [1,2,3,7,8,9],
      [4,5,6,10,11,12]
    ]
    

    It's shape is (2, 6)

提交回复
热议问题