Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

前端 未结 5 1083
不知归路
不知归路 2021-02-18 22:59

Executing

import numpy as np
t1 = np.arange(1,10)
t2 = np.arange(11,20)

t3 = np.concatenate((t1,t2),axis=1)

results in a

Trac         


        
5条回答
  •  温柔的废话
    2021-02-18 23:15

    This is because of Numpy's way of representing 1D arrays. The following using reshape() will work:

    t3 = np.concatenate((t1.reshape(-1,1),t2.reshape(-1,1),axis=1)
    

    Explanation: This is the shape of the 1D array when initially created:

    t1 = np.arange(1,10)
    t1.shape
    >>(9,)
    

    'np.concatenate' and many other functions don't like the missing dimension. Reshape does the following:

    t1.reshape(-1,1).shape
    >>(9,1) 
    

提交回复
热议问题