Python中的numpy.sum()

Python numpy sum() function is used to get the sum of array elements over a given axis.

Python numpy sum()函数用于获取给定轴上数组元素的总和。

Python numpy sum()函数语法 (Python numpy sum() function syntax)

Python NumPy sum() method syntax is:

Python NumPy sum()方法语法为:

sum(array, axis, dtype, out, keepdims, initial)
  • The array elements are used to calculate the sum.

    数组元素用于计算总和。
  • If the axis is not provided, the sum of all the elements is returned. If the axis is a tuple of ints, the sum of all the elements in the given axes is returned.

    如果未提供 ,则返回所有元素的总和。 如果轴是整数元组,则返回给定轴中所有元素的总和。
  • We can specify dtype to specify the returned output data type.

    我们可以指定dtype来指定返回的输出数据类型。
  • The out variable is used to specify the array to place the result. It’s an optional parameter.

    out变量用于指定要放置结果的数组。 这是一个可选参数。
  • The keepdims is a boolean parameter. If this is set to True, the axes which are reduced are left in the result as dimensions with size one.

    keepdims是一个布尔参数。 如果将其设置为True,则缩小的轴将保留为尺寸1的尺寸。
  • The initial parameter specifies the starting value for the sum.

    初始参数指定总和的起始值。

Python numpy sum()示例 (Python numpy sum() Examples)

Let’s look at some of the examples of numpy sum() function.

让我们看一些numpy sum()函数的示例。

1.数组中所有元素的总和 (1. Sum of All the Elements in the Array)

If we pass only the array in the sum() function, it’s flattened and the sum of all the elements is returned.

如果仅在sum()函数中传递数组,则将其展平并返回所有元素的总和。

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4],
     [5, 6]])

total = np.sum(array1)
print(f'Sum of all the elements is {total}')

Output: Sum of all the elements is 21

输出Sum of all the elements is 21

2.沿轴的数组元素之和 (2. Sum of Array Elements Along the Axis)

If we specify the axis value, the sum of elements along that axis is returned. If the array shape is (X, Y) then the sum along 0-axis will be of shape (1, Y). The sum along 1-axis will be of shape (1, X).

如果指定轴值,则返回沿该轴的元素之和。 如果数组形状为(X,Y),则沿0轴的和将为(1,Y)。 沿1轴的和将为(1,X)。

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4],
     [5, 6]])

total_0_axis = np.sum(array1, axis=0)
print(f'Sum of elements at 0-axis is {total_0_axis}')

total_1_axis = np.sum(array1, axis=1)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output:

输出:

Sum of elements at 0-axis is [ 9 12]
Sum of elements at 1-axis is [ 3  7 11]

3.指定总和的输出数据类型 (3. Specifying Output Data Type of Sum)

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4]])

total_1_axis = np.sum(array1, axis=1, dtype=float)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output: Sum of elements at 1-axis is [3. 7.]

输出Sum of elements at 1-axis is [3. 7.] Sum of elements at 1-axis is [3. 7.]

4.总和的初始值 (4. Initial Value for the Sum)

import numpy as np

array1 = np.array(
    [[1, 2],
     [3, 4]])

total_1_axis = np.sum(array1, axis=1, initial=10)
print(f'Sum of elements at 1-axis is {total_1_axis}')

Output: Sum of elements at 1-axis is [13 17]

输出Sum of elements at 1-axis is [13 17]

Reference: API Doc

参考API文档

翻译自: https://www.journaldev.com/32788/numpy-sum-in-python

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值