在解释昏暗numpy的,形状,等级,尺寸,轴之间的差异 [英] Explaining the differences between dim, shape, rank, dimension and axis in numpy

查看:290
本文介绍了在解释昏暗numpy的,形状,等级,尺寸,轴之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是新来的蟒蛇和numpy的。我读了几教程,仍然在昏暗的,职级,形状,aixes和尺寸的差异之间如此混乱。我的心似乎在矩阵重新presentation被卡住。所以,如果你说A是一个矩阵,看起来像这样:

I'm new to python and numpy in general. I read several tutorials and still so confused between the differences in dim, ranks, shape, aixes and dimensions. My mind seems to be stuck at the matrix representation. So if you say that A is a matrix that looks like this:

A = 

1 2 3
4 5 6

那么所有我能想到的是2×3的矩阵(两行三列)。在这里,我明白形状的2x3。但我真的我不能出去边二维矩阵的思考。我不明白,例如点()文​​档当它说对于N维它是超过一个的最后一个轴和第二个到最后为b的总和产品。我很困惑,无法理解这一点。我不理解一样,如果V是N:1的矢量,M是N:N的矩阵,如何点(V,M)或点(M,V)的工作和它们之间的差值

then all I can think of is a 2x3 matrix (two rows and three columns). Here I understand that the shape is 2x3. But I really I am unable to go out side the thinking of a 2D matrices. I don't understand for example the dot() documentation when it says "For N dimensions it is a sum product over the last axis of a and the second-to-last of b". I'm so confused and unable to understand this. I don't understand like if V is a N:1 vector and M is N:N matrix, how dot(V,M) or dot(M,V) work and the difference between them.

任何人都可以,那么请给我解释一下什么是A N维数组,什么形状,什么是轴,它是如何涉及到圆点()函数的文档?如果解释形象化的想法将是巨大的。

Can anyone then please explain to me what is a N dimensional array, what's a shape, what's an axis and how does it relate to the documentation of the dot() function? It would be great if the explanation visualizes the ideas.

推荐答案

np.dot 是矩阵乘法的推广。
在常规的矩阵乘法,第(N,M) - 形矩阵乘以一(M,P)形矩阵的结果在第(N,P)形矩阵。所得形状可以被认为是由两个形状挤压在一起((N,M,M,P)),然后除去中间的数字,<$ C形成$ C> M (产生(N,P))。这是 np.dot preserves而推广到高维数组的财产。

np.dot is a generalization of matrix multiplication. In regular matrix multiplication, an (N,M)-shape matrix multiplied with a (M,P)-shaped matrix results in a (N,P)-shaped matrix. The resultant shape can be thought of as being formed by squashing the two shapes together ((N,M,M,P)) and then removing the middle numbers, M (to produce (N,P)). This is the property that np.dot preserves while generalizing to arrays of higher dimension.

在文档说,

对于N维它是超过一个和最后一个轴线的和积
  第二个到最后的B。

"For N dimensions it is a sum product over the last axis of a and the second-to-last of b".

这是讲了这一点。形状的数组(U,V,M)点缀着形状的阵列(W,X,Y,M,Z)将导致形状的阵列(U,v,W,X,Y,Z)

it is speaking to this point. An array of shape (u,v,M) dotted with an array of shape (w,x,y,M,z) would result in an array of shape (u,v,w,x,y,z).

让我们来看看,当应用于此规则的外观

Let's see how this rule looks when applied to

In [25]: V = np.arange(2); V
Out[25]: array([0, 1])

In [26]: M = np.arange(4).reshape(2,2); M
Out[26]: 
array([[0, 1],
       [2, 3]])

首先,最容易的部分:

First, the easy part:

In [27]: np.dot(M, V)
Out[27]: array([1, 3])

这里没有惊喜;这仅仅是矩阵向量乘法。

There is no surprise here; this is just matrix-vector multiplication.

现在考虑

In [28]: np.dot(V, M)
Out[28]: array([2, 3])

看V和M的形状:

Look at the shape of V and M:

In [29]: V.shape
Out[29]: (2,)

In [30]: M.shape
Out[30]: (2, 2)

所以 np.dot(V,M)就像是一个(2)的矩阵乘法 - 用(2,2)形矩阵,形矩阵应导致(2,) - 成形矩阵。

So np.dot(V,M) is like matrix multiplication of a (2,)-shaped matrix with a (2,2)-shaped matrix, which should result in a (2,)-shaped matrix.

V M 第二到最后一个轴(又名的最后一个(也是唯一)轴第一轴 M )相乘,求和,只留下 M

The last (and only) axis of V and the second-to-last axis of M (aka the first axis of M) are multiplied and summed over, leaving only the last axis of M.

如果您要显示这样的: np.dot(V,M)看起来好像V具有1行和2列:

If you want to visualize this: np.dot(V, M) looks as though V has 1 row and 2 columns:

[[0, 1]] * [[0, 1],
            [2, 3]] 

等等,当V被M乘, np.dot(V,M)等于

[[0*0 + 1*2],     [2, 
 [0*1 + 1*3]]   =  3] 

不过,我真的不建议尝试以可视化numpy的阵列这种方式 - 至少我不会做。我几乎完全集中在形状。

However, I don't really recommend trying to visualize NumPy arrays this way -- at least I never do. I focus almost exclusively on the shape.

(2,) * (2,2)
   \   /
    \ /
    (2,)

您只要想想中间轴所点缀,从得到的形状消失。

You just think about the "middle" axes being dotted, and disappearing from the resultant shape.

np.sum(ARR,轴= 0)告诉numpy的总结在元素改编消除的第0轴。如果改编是2维的,第0轴都行。因此,例如,如果改编是这样的:

np.sum(arr, axis=0) tells NumPy to sum the elements in arr eliminating the 0th axis. If arr is 2-dimensional, the 0th axis are the rows. So for example, if arr looks like this:

In [1]: arr = np.arange(6).reshape(2,3); arr
Out[1]: 
array([[0, 1, 2],
       [3, 4, 5]])

然后 np.sum(ARR,轴= 0)将沿列求和,从而取消的第0轴(即各行) 。

then np.sum(arr, axis=0) will sum along the columns, thus eliminating the 0th axis (i.e. the rows).

In [2]: np.sum(arr, axis=0)
Out[2]: array([3, 5, 7])

3是0 + 3的结果,5等于1 + 4,7等于2 + 5

The 3 is the result of 0+3, the 5 equals 1+4, the 7 equals 2+5.

通知改编有形状(2,3),和求和后,第0轴的删除的所以结果是形状(3 )。第0轴有长度为2,每一笔由添加这些2元素。形状(2,3)变为(3)。您可以知道最终形状前进!这可以帮助指导你的思维。

Notice arr had shape (2,3), and after summing, the 0th axis is removed so the result is of shape (3,). The 0th axis had length 2, and each sum is composed of adding those 2 elements. The shape (2,3) "becomes" (3,). You can know the resultant shape in advance! This can help guide your thinking.

要测试你的理解,认为 np.sum(ARR,轴= 1)。现在1轴被除去。这样所得到的形状将是(2,),并在结果元素将是3个值的总和。

To test your understanding, consider np.sum(arr, axis=1). Now the 1-axis is removed. So the resultant shape will be (2,), and element in the result will be the sum of 3 values.

In [3]: np.sum(arr, axis=1)
Out[3]: array([ 3, 12])

3等于0 + 1 + 2,和12等于3 + 4 + 5

The 3 equals 0+1+2, and the 12 equals 3+4+5.

所以我们看到,从相加的结果轴的消除的轴。这对轴承 np.dot ,由于计算由 np.dot 执行是的产品。由于 np.dot 执行沿着一定的轴求和操作,即轴从结果中删除。这就是为什么在形状(2)的阵列应用 np.dot 来的形状(2)和(2,2)的结果阵列。在两个阵列的第2被求和,消除两者,只有第二阵列中离开第二2。

So we see that summing an axis eliminates that axis from the result. This has bearing on np.dot, since the calculation performed by np.dot is a sum of products. Since np.dot performs a summing operation along certain axes, that axis is removed from the result. That is why applying np.dot to arrays of shape (2,) and (2,2) results in an array of shape (2,). The first 2 in both arrays is summed over, eliminating both, leaving only the second 2 in the second array.

这篇关于在解释昏暗numpy的,形状,等级,尺寸,轴之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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