在Numpy中连接空数组 [英] Concatenating empty array in Numpy

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

问题描述

在 Matlab 中我这样做:

<代码>>>E = [];>>A = [1 2 3 4 5;10 20 30 40 50];>>E = [E ;一种]E =1 2 3 4 510 20 30 40 50

现在我想在 Numpy 中做同样的事情,但我有问题,看看这个:

<预><代码>>>>E = 数组([],dtype=int)>>>乙数组([],dtype=int64)>>>A = 数组([[1,2,3,4,5],[10,20,30,40,50]])>>>E = vstack((E,A))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/shape_base.py",第 226 行,在 vstack 中返回 _nx.concatenate(map(atleast_2d,tup),0)ValueError:除 d_0 外,数组维度必须一致

当我这样做时,我遇到了类似的情况:

<预><代码>>>>E = 串联((E,A),axis=0)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中ValueError:数组必须具有相同的维数

或者:

<预><代码>>>>E = append([E],[A],axis=0)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py",第 3577 行,附加返回连接((arr,值),轴=轴)ValueError:数组必须具有相同的维数

解决方案

如果你事先知道列数:

<预><代码>>>>xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])>>>ys = np.array([], dtype=np.int64).reshape(0,5)>>>是数组([], 形状=(0, 5), dtype=int64)>>>np.vstack([ys, xs])数组([[ 1., 2., 3., 4., 5.],[ 10., 20., 30., 40., 50.]])

如果不是:

<预><代码>>>>ys = np.array([])>>>ys = np.vstack([ys, xs]) 如果 ys.size else xs数组([[ 1, 2, 3, 4, 5],[10, 20, 30, 40, 50]])

in Matlab I do this:

>> E = [];
>> A = [1 2 3 4 5; 10 20 30 40 50];
>> E = [E ; A]

E =

     1     2     3     4     5
    10    20    30    40    50

Now I want the same thing in Numpy but I have problems, look at this:

>>> E = array([],dtype=int)
>>> E
array([], dtype=int64)
>>> A = array([[1,2,3,4,5],[10,20,30,40,50]])

>>> E = vstack((E,A))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/shape_base.py", line 226, in vstack
    return _nx.concatenate(map(atleast_2d,tup),0)
ValueError: array dimensions must agree except for d_0

I have a similar situation when I do this with:

>>> E = concatenate((E,A),axis=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: arrays must have same number of dimensions

Or:

>>> E = append([E],[A],axis=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 3577, in append
    return concatenate((arr, values), axis=axis)
ValueError: arrays must have same number of dimensions

解决方案

if you know the number of columns before hand:

>>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = np.array([], dtype=np.int64).reshape(0,5)
>>> ys
array([], shape=(0, 5), dtype=int64)
>>> np.vstack([ys, xs])
array([[  1.,   2.,   3.,   4.,   5.],
       [ 10.,  20.,  30.,  40.,  50.]])

if not:

>>> ys = np.array([])
>>> ys = np.vstack([ys, xs]) if ys.size else xs
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]])

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

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