本文介绍了连接两个一维到两列阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = np.array([1, 2, 3])
aa = np.array([1], [2], [3])
b = np.array([1, 2, 3])
bb = np.array([1], [2], [3])
np.concatenate((a, b), axis = 1)
array([1, 2, 3, 1, 2, 3])     # It's ok, that's what I was expecting
np.concatenate((a, b), axis = 0)
array([1, 2, 3, 1, 2, 3])     # It's not ok, that's not what I was expecting

我所期待的:

array([[1, 2, 3],
       [1, 2, 3]])

即使AA和BB我得到同样的不一致。
那么有没有一个简单的解决方案,以沿轴线串联0两个一维数组?

even with aa and bb I get the same inconsistency.so is there a simple solution to concatenate along axis 0 two one-dimensional arrays?

推荐答案

注意 A B 都是一维;有没有轴1沿着串联。你想:

Note that a and b are both one-dimensional; there's no axis 1 to concatenate along. You want vstack:

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = a.copy()
>>> np.vstack([a,b])
array([[1, 2, 3],
       [1, 2, 3]])

另外,你可以重塑 A B 第一:

>>> np.concatenate([a[np.newaxis,:],b[np.newaxis,:]],axis = 0)
array([[1, 2, 3],
       [1, 2, 3]])

这篇关于连接两个一维到两列阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:48