Python Numpy 的 sum 函数中的 axis = 0 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40200070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does axis = 0 do in Numpy's sum function?
提问by Bishwajit Purkaystha
I am learning Python, and have encountered numpy.sum
. It has an optional parameter axis
. This parameter is used to get either column-wise summation or row-wise summation. When axis = 0
we imply to sum it over columns only. For example,
我正在学习 Python,并且遇到过numpy.sum
. 它有一个可选参数axis
。此参数用于获得按列求和或按行求和。当axis = 0
我们暗示仅对列求和时。例如,
a = np.array([[1, 2, 3], [4, 5, 6]])
np.sum(a, axis = 0)
This snippet of code produces output: array([5, 7, 9])
, fine. But if I do:
这段代码产生输出:array([5, 7, 9])
,很好。但如果我这样做:
a = np.array([1, 2, 3])
np.sum(a, axis = 0)
I get result: 6
, why is that? Shouldn't I get array([1, 2, 3])
?
我得到结果:6
,为什么?我不应该得到array([1, 2, 3])
吗?
回答by juanpa.arrivillaga
All that is going on is that numpy is summing across the first (0th) and only axis. Consider the following:
所发生的一切是 numpy 在第一个(第 0 个)也是唯一一个轴上求和。考虑以下:
In [2]: a = np.array([1, 2, 3])
In [3]: a.shape
Out[3]: (3,)
In [4]: len(a.shape) # number of dimensions
Out[4]: 1
In [5]: a1 = a.reshape(3,1)
In [6]: a2 = a.reshape(1,3)
In [7]: a1
Out[7]:
array([[1],
[2],
[3]])
In [8]: a2
Out[8]: array([[1, 2, 3]])
In [9]: a1.sum(axis=1)
Out[9]: array([1, 2, 3])
In [10]: a1.sum(axis=0)
Out[10]: array([6])
In [11]: a2.sum(axis=1)
Out[11]: array([6])
In [12]: a2.sum(axis=0)
Out[12]: array([1, 2, 3])
So, to be more explicit:
所以,更明确地说:
In [15]: a1.shape
Out[15]: (3, 1)
a1
is 2-dimensional, the "long" axis being the first.
a1
是二维的,“长”轴是第一个。
In [16]: a1[:,0] # give me everything in the first axis, and the first part of the second
Out[16]: array([1, 2, 3])
Now, sum along the first axis:
现在,沿第一个轴求和:
In [17]: a1.sum(axis=0)
Out[17]: array([6])
Now, consider a less trivial two-dimensional case:
现在,考虑一个不太重要的二维情况:
In [20]: b = np.array([[1,2,3],[4,5,6]])
In [21]: b
Out[21]:
array([[1, 2, 3],
[4, 5, 6]])
In [22]: b.shape
Out[22]: (2, 3)
The first axis is the "rows". Sum alongthe rows:
第一个轴是“行”。沿行求和:
In [23]: b.sum(axis=0)
Out[23]: array([5, 7, 9])
The second axis are the "columns". Sum alongthe columns:
第二个轴是“列”。沿列求和:
In [24]: b.sum(axis=1)
Out[24]: array([ 6, 15])
回答by Apoorve
The axis i in np.sum(a, axis=i)
is the ith index of the shape of that array (zero-indexed).
轴 i innp.sum(a, axis=i)
是该数组形状的第 i 个索引(零索引)。
Let's try and understand what that means with some examples:
让我们试着用一些例子来理解这意味着什么:
a = np.array([1, 2, 3])
print (a.shape) #prints (3,)
#so axis = 0 corresponds to 3 and axis = 1 corresponds to nothing
Let's see what axis = 0 and axis = 1 do to the sum:
让我们看看axis = 0和axis = 1对总和的作用:
sum = np.sum(a, axis=0) #sum = 6
So, sum = np.sum(a, axis=0)
would sum up all numbers that the 0th index of a.shape refers to, which in this case are 3 numbers. Since numpy arrays are, by default, row-major (which is just another way of saying that the row index is specified before the column index), thus axis=0 would sum the three numbers that the shape refers to.
因此,sum = np.sum(a, axis=0)
将总结 a.shape 的第 0 个索引所指的所有数字,在这种情况下是 3 个数字。由于默认情况下 numpy 数组是行优先的(这只是在列索引之前指定行索引的另一种说法),因此 axis=0 将对形状引用的三个数字求和。
sum = np.sum(a, axis=1) #gives an error
Similarly, np.sum(a, axis=1)
should sum up all the numbers that the 1st index of np.shape refers to, but since there is no first index of the shape, we get an error.
同样,np.sum(a, axis=1)
应该总结 np.shape 的第一个索引所指的所有数字,但由于没有形状的第一个索引,我们得到一个错误。
Let's take another example:
让我们再举一个例子:
b = np.array([[1,2,3],
[4,5,6]])
print(b.shape) #prints (2,3)
#axis = 0 corresponds to 2 and axis = 1 corresponds to 3
And now, let's see what changing the axis does:
现在,让我们看看改变轴的作用:
sum = np.sum(b, axis=0) #sum = [5, 7, 9] of shape(3,)
We know that axis = 0 should sum along the first index of the shape and we expect it to find two numbers along this axis (by looking at the shape). So [1+4, 2+5, 3+6]
.
我们知道 axis = 0 应该沿着形状的第一个索引求和,我们期望它沿着这个轴找到两个数字(通过查看形状)。所以[1+4, 2+5, 3+6]
。
sum = np.sum(b, axis=1) #sum = [6, 15] of shape(2,)
Now the sum is along axis = 1, and from the shape we can see this it is an axis along which there are 3 numbers to be summed. So, [1+2+3,4+5+6]
现在总和沿着轴 = 1,从形状我们可以看出这是一个轴,沿着轴有 3 个要求和的数字。所以,[1+2+3,4+5+6]
回答by Liang
axis
is the index of array index.
the array index starts from 0, counting from right to left.
axis
是数组索引的索引。数组索引从 0 开始,从右到左计数。
hence,
np.sum(a, axis = 0)
means sum along the right most index.
因此,
np.sum(a, axis = 0)
意味着沿着最右边的索引求和。