numpy ndarray 形状有什么作用? [英] what does numpy ndarray shape do?

查看:37
本文介绍了numpy ndarray 形状有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 .shape 函数的简单问题,这让我很困惑.

a = np.array([1, 2, 3]) # 创建一个秩为 1 的数组print(type(a)) # 打印"print(a.shape) # 打印 "(3,)"b = np.array([[1,2,3],[4,5,6]]) # 创建一个秩为 2 的数组print(b.shape) # 打印 "(2, 3)"

.shape 到底做了什么?计算多少行,多少列,那么 a.shape 应该是,(1,3),一行三列,对吗?

解决方案

yourarray.shapenp.shape()np.ma.shape() 将 ndarray 的形状作为 tuple 返回;您可以使用 获取数组的(数量)维数yourarray.ndimnp.ndim().(即它给出了 ndarrayn,因为 NumPy 中的所有数组都只是 n 维数组(简称为 ndarrays))

对于 1D 数组,形状将是 (n,),其中 n 是数组中元素的数量.

对于 2D 数组,形状将是 (n,m) 其中 n 是行数,m 是数组中的列数.

请注意,在 1D 情况下,形状将只是 (n, ) 而不是您所说的 (1, n)(n, 1) 分别用于行向量和列向量.

这是遵循以下约定:

对于一维数组,返回一个只有 1 元素的 shape 元组  (即(n,))
对于二维数组,返回一个只有 2 个元素的 shape 元组(即 (n,m))
对于 3D 数组,返回一个只有 3 个元素的 形状元组(即 (n,m,k))
对于 4D 数组,返回一个只有 4 个元素的 shape 元组(即 (n,m,k,j))

等等.

另外,请看下面的例子,看看如何np.shape()np.ma.shape()1D 数组和标量一起工作:

# 示例数组在 [10] 中:u = np.arange(10)# 获取它的形状在 [11]: np.shape(u) # u.shape出[11]:(10,)# 使用 `np.ndim` 获取数组维度在 [12]: np.ndim(u)出[12]:1在 [13] 中:np.shape(np.mean(u))Out[13]: () # 空元组(表示标量是一个 0D 数组).# 使用 `numpy.ndim` 检查在 [14] 中:np.ndim(np.mean(u))出[14]:0

P.S.:因此,形状元组与我们对空间维度的理解一致,至少在数学上是这样.

I have a simple question about the .shape function, which confused me a lot.

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # Prints "<class 'numpy.ndarray'>"
print(a.shape)            # Prints "(3,)"

b = np.array([[1,2,3],[4,5,6]])    # Create a rank 2 array
print(b.shape)                     # Prints "(2, 3)"

What did the .shape exactly do? count how many rows, how many columns, then the a.shape suppose to be, (1,3), one row three columns, right?

解决方案

yourarray.shape or np.shape() or np.ma.shape() returns the shape of your ndarray as a tuple; And you can get the (number of) dimensions of your array using yourarray.ndim or np.ndim(). (i.e. it gives the n of the ndarray since all arrays in NumPy are just n-dimensional arrays (shortly called as ndarrays))

For a 1D array, the shape would be (n,) where n is the number of elements in your array.

For a 2D array, the shape would be (n,m) where n is the number of rows and m is the number of columns in your array.

Please note that in 1D case, the shape would simply be (n, ) instead of what you said as either (1, n) or (n, 1) for row and column vectors respectively.

This is to follow the convention that:

For 1D array, return a shape tuple with only 1 element   (i.e. (n,))
For 2D array, return a shape tuple with only 2 elements (i.e. (n,m))
For 3D array, return a shape tuple with only 3 elements (i.e. (n,m,k))
For 4D array, return a shape tuple with only 4 elements (i.e. (n,m,k,j))

and so on.

Also, please see the example below to see how np.shape() or np.ma.shape() behaves with 1D arrays and scalars:

# sample array
In [10]: u = np.arange(10)

# get its shape
In [11]: np.shape(u)    # u.shape
Out[11]: (10,)

# get array dimension using `np.ndim`
In [12]: np.ndim(u)
Out[12]: 1

In [13]: np.shape(np.mean(u))
Out[13]: ()       # empty tuple (to indicate that a scalar is a 0D array).

# check using `numpy.ndim`
In [14]: np.ndim(np.mean(u))
Out[14]: 0

P.S.: So, the shape tuple is consistent with our understanding of dimensions of space, at least mathematically.

这篇关于numpy ndarray 形状有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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