• <small id='tyOFW'></small><noframes id='tyOFW'>

  • <tfoot id='tyOFW'></tfoot>

      • <bdo id='tyOFW'></bdo><ul id='tyOFW'></ul>
      <i id='tyOFW'><tr id='tyOFW'><dt id='tyOFW'><q id='tyOFW'><span id='tyOFW'><b id='tyOFW'><form id='tyOFW'><ins id='tyOFW'></ins><ul id='tyOFW'></ul><sub id='tyOFW'></sub></form><legend id='tyOFW'></legend><bdo id='tyOFW'><pre id='tyOFW'><center id='tyOFW'></center></pre></bdo></b><th id='tyOFW'></th></span></q></dt></tr></i><div id='tyOFW'><tfoot id='tyOFW'></tfoot><dl id='tyOFW'><fieldset id='tyOFW'></fieldset></dl></div>

    1. <legend id='tyOFW'><style id='tyOFW'><dir id='tyOFW'><q id='tyOFW'></q></dir></style></legend>

        Numpy追加:自动转换错误维度的数组

        Numpy append: Automatically cast an array of the wrong dimension(Numpy追加:自动转换错误维度的数组)
            <tfoot id='aoEXs'></tfoot>

            <small id='aoEXs'></small><noframes id='aoEXs'>

                <tbody id='aoEXs'></tbody>
              • <bdo id='aoEXs'></bdo><ul id='aoEXs'></ul>

                <i id='aoEXs'><tr id='aoEXs'><dt id='aoEXs'><q id='aoEXs'><span id='aoEXs'><b id='aoEXs'><form id='aoEXs'><ins id='aoEXs'></ins><ul id='aoEXs'></ul><sub id='aoEXs'></sub></form><legend id='aoEXs'></legend><bdo id='aoEXs'><pre id='aoEXs'><center id='aoEXs'></center></pre></bdo></b><th id='aoEXs'></th></span></q></dt></tr></i><div id='aoEXs'><tfoot id='aoEXs'></tfoot><dl id='aoEXs'><fieldset id='aoEXs'></fieldset></dl></div>
                <legend id='aoEXs'><style id='aoEXs'><dir id='aoEXs'><q id='aoEXs'></q></dir></style></legend>

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

                  问题描述

                  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追加:自动转换错误维度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                • <legend id='kQeyt'><style id='kQeyt'><dir id='kQeyt'><q id='kQeyt'></q></dir></style></legend>
                • <small id='kQeyt'></small><noframes id='kQeyt'>

                      <bdo id='kQeyt'></bdo><ul id='kQeyt'></ul>

                          <tbody id='kQeyt'></tbody>
                          <tfoot id='kQeyt'></tfoot>

                          1. <i id='kQeyt'><tr id='kQeyt'><dt id='kQeyt'><q id='kQeyt'><span id='kQeyt'><b id='kQeyt'><form id='kQeyt'><ins id='kQeyt'></ins><ul id='kQeyt'></ul><sub id='kQeyt'></sub></form><legend id='kQeyt'></legend><bdo id='kQeyt'><pre id='kQeyt'><center id='kQeyt'></center></pre></bdo></b><th id='kQeyt'></th></span></q></dt></tr></i><div id='kQeyt'><tfoot id='kQeyt'></tfoot><dl id='kQeyt'><fieldset id='kQeyt'></fieldset></dl></div>