python - 连接两个numpy数组问题

标签 python numpy

我有两个形状的 numpy 数组

(129873, 12)
(129873,)

我想将它们连接起来,使它们的形状为:

(129873, 13)

我尝试过 numpy.concatenatenumpy.vstack 但似乎出现错误:

ValueError: all the input arrays must have same number of dimensions

关于如何做到这一点有什么建议吗?

最佳答案

我认为这已经在这里得到了回答:

Numpy: Concatenating multidimensional and unidimensional arrays

import numpy
a = numpy.array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]])
b = numpy.array([5, 6, 6])
c = numpy.column_stack((a,b))
print a
print b
print c
print a.shape
print b.shape
print c.shape

这给你:

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

关于python - 连接两个numpy数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252109/

相关文章:

python - 不规则多面体的体积

python - 为什么模块不能成为上下文管理器(到 'with' 语句)?

python - Point() 接受 0 个位置子模式(给定 2 个)

python - DRF - 向可浏览的 api 发送额外数据

python - 从 python 列表中删除重复(非精确)元组

python - NumPy 在浮点运算中将较低的精度转换为较高的精度

python - 不使用 Itertools 组合两个列表

python - Opencv:旋转矩形感兴趣区域外的零像素?

用于创建和操作列表的 Python 函数

python-3.x - 使用 Pandas 数据帧时,无法将存储为 excel 中的字符串的矩阵转换为 numpy 数组