【问题标题】:Numpy array dimensionsNumpy 数组维度
【发布时间】:2011-03-04 22:56:29
【问题描述】:

我目前正在尝试学习 Numpy 和 Python。给定以下数组:

import numpy as np
a = np.array([[1,2],[1,2]])

是否有返回a 尺寸的函数(例如,a 是一个 2 x 2 数组)?

size() 返回 4 并没有多大帮助。

【问题讨论】:

  • 一条建议:你的“维度”在 NumPy 中被称为shape。 NumPy 所说的维度是 2,在你的情况下(ndim)。了解常用的 NumPy 术语很有用:这使阅读文档更容易!

标签: python arrays numpy dimensions


【解决方案1】:

.shape:

ndarray.形状
数组维度的元组。

因此:

>>> a.shape
(2, 2)

【讨论】:

  • 注意:shape 可能更准确地描述为 属性 而不是 函数,因为它不是使用函数调用语法调用的.
  • @nobar 实际上它是一个属性(它既是属性又是函数,真的)
  • @wim 更具体地说是property is a class。对于类属性(您放入类中的属性),它们是作为类的属性公开的属性类型的对象。一个属性,在 python 中,is the name following the dot.
  • 如果你真的要吹毛求疵,那就是描述符。虽然property 本身是一个类,但ndarray.shape 不是一个类,它是属性类型的一个实例。
【解决方案2】:

第一:

按照惯例,在 Python 世界中,numpy 的快捷方式是 np,所以:

In [1]: import numpy as np

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

第二:

在 Numpy 中,dimensionaxis/axesshape 是相关且有时相似的概念:

尺寸

数学/物理学中,维度或维度被非正式地定义为指定空间内任何点所需的最小坐标数。但是在Numpy中,根据numpy doc,它和axis/axes是一样的:

在 Numpy 中,维度被称为轴。轴数为rank。

In [3]: a.ndim  # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2

轴/轴

nth 坐标在 Numpy 中索引 array。并且多维数组每个轴可以有一个索引。

In [4]: a[1,0]  # to index `a`, we specific 1 at the first axis and 0 at the second axis.
Out[4]: 3  # which results in 3 (locate at the row 1 and column 0, 0-based index)

形状

描述沿每个可用轴有多少数据(或范围)。

In [5]: a.shape
Out[5]: (2, 2)  # both the first and second axis have 2 (columns/rows/pages/blocks/...) data

【讨论】:

    【解决方案3】:
    import numpy as np   
    >>> np.shape(a)
    (2,2)
    

    如果输入不是 numpy 数组而是列表列表,也可以使用

    >>> a = [[1,2],[1,2]]
    >>> np.shape(a)
    (2,2)
    

    或者一个元组的元组

    >>> a = ((1,2),(1,2))
    >>> np.shape(a)
    (2,2)
    

    【讨论】:

    • np.shape 如果没有 shape 属性,则首先将其参数转换为数组,这就是它适用于列表和元组示例的原因。
    【解决方案4】:

    你可以使用 .shape

    In: a = np.array([[1,2,3],[4,5,6]])
    In: a.shape
    Out: (2, 3)
    In: a.shape[0] # x axis
    Out: 2
    In: a.shape[1] # y axis
    Out: 3
    

    【讨论】:

      【解决方案5】:

      您可以使用.ndim 获取尺寸,使用.shape 了解确切尺寸:

      >>> var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])
      
      >>> var.ndim
      2
      
      >>> varshape
      (2, 6) 
      

      您可以使用.reshape 函数更改尺寸:

      >>> var_ = var.reshape(3, 4)
      
      >>> var_.ndim
      2
      
      >>> var_.shape
      (3, 4)
      

      【讨论】:

        【解决方案6】:

        shape 方法要求 a 是一个 Numpy ndarray。但是 Numpy 也可以计算纯 python 对象的迭代形状:

        np.shape([[1,2],[1,2]])
        

        【讨论】:

          【解决方案7】:

          a.shape 只是np.info() 的限定版本。看看这个:

          import numpy as np
          a = np.array([[1,2],[1,2]])
          np.info(a)
          

          出来

          class:  ndarray
          shape:  (2, 2)
          strides:  (8, 4)
          itemsize:  4
          aligned:  True
          contiguous:  True
          fortran:  False
          data pointer: 0x27509cf0560
          byteorder:  little
          byteswap:  False
          type: int32
          

          【讨论】:

            【解决方案8】:
            rows = a.shape[0] # 2 
            cols = a.shape[1] # 2
            a.shape #(2,2)
            a.size # rows * cols = 4
            

            【讨论】:

              【解决方案9】:

              在 python notebook 中执行下面的代码块。

              import numpy as np
              a = np.array([[1,2],[1,2]])
              print(a.shape)
              print(type(a.shape))
              print(a.shape[0])
              

              输出

              (2, 2)

              2

              然后你意识到a.shape 是一个元组。 所以你可以通过a.shape[index of dimention]得到任何维度的大小

              【讨论】:

                猜你喜欢
                • 2017-09-12
                • 2021-03-20
                • 1970-01-01
                • 1970-01-01
                • 2014-07-19
                • 2014-02-20
                • 2015-07-29
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多