numpy的追加:自动投错维数组 [英] Numpy append: Automatically cast an array of the wrong dimension

查看:274
本文介绍了numpy的追加:自动投错维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到以下几点没有,如果条款?

我读了一组与pupynere的NetCDF文件,并​​希望建立与numpy的追加数组。有时在输入数据是多维(见变量a下),有时一维(B),但在第一维中的元素的数目总是相同的(在下面的例子中9)。

 >导入numpy的是NP
> A = np.arange(27).reshape(3,9)
> B = np.arange(9)
>一个形状
(3,9)
> b.shape
(9)

这按预期工作:

 > np.append(一,一,轴= 0)
阵列([0,1,2,3,4,5,6,7,8],
   [9,10,11,12,13,14,15,16,17],
   [18,19,20,21,22,23,24,25,26]
   [0,1,2,3,4,5,6,7,8],
   [9,10,11,12,13,14,15,16,17],
   [18,19,20,21,22,23,24,25,26])

不过,追加B不那么优雅的工作:

 > np.append(A,B,轴= 0)
ValueError错误:数组必须具有相同的维数

与追加的问题是(从numpy的手册)


  

当指定轴,值必须有正确的形状


我得先投,以获得正确的结果。

 > np.append(A,b.reshape(1,9),轴= 0)
阵列([0,1,2,3,4,5,6,7,8],
   [9,10,11,12,13,14,15,16,17],
   [18,19,20,21,22,23,24,25,26]
   [0,1,2,3,4,5,6,7,8]])

所以,在我的文件读取循环,我目前如果这样的条款使用

 为我在[A,B]
    如果np.size(i.shape)== 2:
        结果= np.append(因此,我,轴= 0)
    其他:
        结果= np.append(结果,i.reshape(1,9),轴= 0)

有没有一种方法追加A和B没有if语句?

编辑:虽然@Sven完美地回答了原来的问题(使用 np.atleast_2d()),他(和其他人)指出,在code是低效的。在下面的答案,我结合自己的建议,并取代我原来的code。它应该是更有效了。谢谢你。


解决方案

您可以使用 numpy.atleast_2d()

 结果= np.append(结果,np.atleast_2d(I),轴= 0)

不过,请注意,重复使用 numpy.append的()是要建立一个numpy的阵列非常低效的方式 - 它在每一个步骤都被重新分配。如果可能的话,preallocate与所需的最终大小的数组,并用切片后填充它。

is there a way to do the following without an if clause?

I'm reading a set of netcdf files with pupynere and want to build an array with numpy append. Sometimes the input data is multi-dimensional (see variable "a" below), sometimes one dimensional ("b"), but the number of elements in the first dimension is always the same ("9" in the example below).

> import numpy as np
> a = np.arange(27).reshape(3,9)
> b = np.arange(9)
> a.shape
(3, 9)
> b.shape
(9,)

this works as expected:

> np.append(a,a, axis=0)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
   [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
   [18, 19, 20, 21, 22, 23, 24, 25, 26],
   [ 0,  1,  2,  3,  4,  5,  6,  7,  8],
   [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
   [18, 19, 20, 21, 22, 23, 24, 25, 26]])

but, appending b does not work so elegantly:

> np.append(a,b, axis=0)
ValueError: arrays must have same number of dimensions

The problem with append is (from the numpy manual)

"When axis is specified, values must have the correct shape."

I'd have to cast first in order to get the right result.

> np.append(a,b.reshape(1,9), axis=0)
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
   [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
   [18, 19, 20, 21, 22, 23, 24, 25, 26],
   [ 0,  1,  2,  3,  4,  5,  6,  7,  8]])

So, in my file reading loop, I'm currently using an if clause like this:

for i in [a, b]:
    if np.size(i.shape) == 2:
        result = np.append(result, i, axis=0)
    else:
        result = np.append(result, i.reshape(1,9), axis=0)

Is there a way to append "a" and "b" without the if statement?

EDIT: While @Sven answered the original question perfectly (using np.atleast_2d()), he (and others) pointed out that the code is inefficient. In an answer below, I combined their suggestions and replaces my original code. It should be much more efficient now. Thanks.

解决方案

You can use numpy.atleast_2d():

result = np.append(result, np.atleast_2d(i), axis=0)

That said, note that the repeated use of numpy.append() is a very inefficient way to build a NumPy array -- it has to be reallocated in every step. If at all possible, preallocate the array with the desired final size and populate it afterwards using slicing.

这篇关于numpy的追加:自动投错维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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