执行中
import numpy as np
t1 = np.arange(1,10)
t2 = np.arange(11,20)
t3 = np.concatenate((t1,t2),axis=1)
导致
Traceback (most recent call last):
File "<ipython-input-264-85078aa26398>", line 1, in <module>
t3 = np.concatenate((t1,t2),axis=1)
IndexError: axis 1 out of bounds [0, 1)
为什么报告轴1超出范围?
最佳答案
您的标题说明了这一点-一维数组没有第二轴!
但话虽如此,在我的系统上,在@Oliver W.
上,它不会产生错误
In [655]: np.concatenate((t1,t2),axis=1)
Out[655]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])
这是我对
axis=0
的预期结果:In [656]: np.concatenate((t1,t2),axis=0)
Out[656]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])
数组为1d时,看起来
concatenate
会忽略axis
参数。我不知道这是我的1.9版本中的新版本还是旧版本。要获得更多控制权,请考虑使用
vstack
和hstack
包装器(如果需要)来扩展数组的尺寸:In [657]: np.hstack((t1,t2))
Out[657]:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18,
19])
In [658]: np.vstack((t1,t2))
Out[658]:
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9],
[11, 12, 13, 14, 15, 16, 17, 18, 19]])
关于arrays - 沿第二轴连接2个1D`numpy`数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35401041/