将一个 NumPy 数组连接到另一个 NumPy 数组 [英] Concatenate a NumPy array to another NumPy array

查看:70
本文介绍了将一个 NumPy 数组连接到另一个 NumPy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 numpy_array.类似于 [ a b c ].

然后我想将它与另一个 NumPy 数组连接起来(就像我们创建一个列表一样).我们如何创建一个包含 NumPy 数组的 NumPy 数组?

我尝试执行以下操作而没有任何运气

<预><代码>>>>M = np.array([])>>>米数组([],dtype=float64)>>>M.append(a,axis=0)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.AttributeError: 'numpy.ndarray' 对象没有属性 'append'>>>一种数组([1, 2, 3])

解决方案

In [1]: import numpy as np在 [2] 中:a = np.array([[1, 2, 3], [4, 5, 6]])在 [3] 中:b = np.array([[9, 8, 7], [6, 5, 4]])在 [4] 中: np.concatenate((a, b))出[4]:数组([[1, 2, 3],[4, 5, 6],[9, 8, 7],[6, 5, 4]])

或者这个:

在[1]中:a = np.array([1, 2, 3])在 [2] 中:b = np.array([4, 5, 6])在 [3] 中: np.vstack((a, b))出[3]:数组([[1, 2, 3],[4, 5, 6]])

I have a numpy_array. Something like [ a b c ].

And then I want to concatenate it with another NumPy array (just like we create a list of lists). How do we create a NumPy array containing NumPy arrays?

I tried to do the following without any luck

>>> M = np.array([])
>>> M
array([], dtype=float64)
>>> M.append(a,axis=0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'append'
>>> a
array([1, 2, 3])

解决方案

In [1]: import numpy as np

In [2]: a = np.array([[1, 2, 3], [4, 5, 6]])

In [3]: b = np.array([[9, 8, 7], [6, 5, 4]])

In [4]: np.concatenate((a, b))
Out[4]: 
array([[1, 2, 3],
       [4, 5, 6],
       [9, 8, 7],
       [6, 5, 4]])

or this:

In [1]: a = np.array([1, 2, 3])

In [2]: b = np.array([4, 5, 6])

In [3]: np.vstack((a, b))
Out[3]: 
array([[1, 2, 3],
       [4, 5, 6]])

这篇关于将一个 NumPy 数组连接到另一个 NumPy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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