沿第二轴串联2个1D`numpy`数组 [英] Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

查看:95
本文介绍了沿第二轴串联2个1D`numpy`数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行

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超出范围?

why does it report that axis 1 is out of bounds?

推荐答案

您的标题对此进行了解释-一维数组没有第二轴!

Your title explains it - a 1d array does not have a 2nd axis!

但是话虽如此,在我的系统上与@Oliver W.一样,它不会产生错误

But having said that, on my system as on @Oliver W.s, it does not produce an error

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获得的结果:

This is the result I would have expected from 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版本中的新版本还是旧版本.

It looks like concatenate ignores the axis parameter when the arrays are 1d. I don't know if this is something new in my 1.9 version, or something old.

要获得更多控制权,请考虑使用vstackhstack包装器,以在需要时扩展数组尺寸:

For more control consider using the vstack and hstack wrappers that expand array dimensions if needed:

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]])

这篇关于沿第二轴串联2个1D`numpy`数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆