Numpy之sum函数用法

文档原文在这:
https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.sum.html


numpy.sum用法如下:

numpy.sum(a, axis=None, dtype=None, out=None, keepdims=False)

求一个数组中给定轴上的元素的总和。

参数如下:

aarray_like 类型

待求和的数组。

axis :可选择Noneint类型 或 整型的tuple类型

在选定的轴上执行求和。如果是默认值(axis=None),就会在所有的轴上执行求和。axis可以是负数,负数的话就代表倒着数的意思,和列表索引访问差不多(N表示第N个,-N表示倒数第N个(没有倒数第0个))。

在新版本1.7.0中,如果axis是一个整型的元组类型,则在多个轴上执行求和,而不是在单个轴上执行求和了。

dtypedtype 类型,可选的

返回值的类型(array类型或者是array内元素累加后的类型),默认情况下是参数a的类型。

有个例外是,当参数a的一个整型类型的精度低于默认的平台整数(platform integer)时,就会使用默认的平台整数。(在下不才,不知这里的platform integer是什么意思)

下面两个参数暂时不常用,懒得翻译了 =.=。

outndarray 类型,可选的

Array into which the output is placed. By default, a new array is created. If out is given, it must be of the appropriate shape (the shape of a with axis removed, i.e., numpy.delete(a.shape, axis)). Its type is preserved. See doc.ufuncs (Section “Output arguments”) for more details.

keepdimsbool 类型,可选的

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.

函数返回:

sum_along_axisndarray

返回和a相同shape的数组,不过要删除掉参数axis指定的轴。

如果a是一个0维数组,或者axis是默认值None,则返回一个标量。

如果指定了一个输出数组,则返回对out的引用 。

注意:
当使用整数类型时算法是模块化的,所以溢出时不会出现错误。
空数组的和是中性元素0

>>> np.sum([])
0.0

举例如下:

>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])

如果累加器太小的话,就会出现溢出:

>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
-128
  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
numpy函数是一种用于处理数组的科学计算库,其中包含了许多常用的数学、统计和线性代数函数,以下是一些常见的numpy函数用法: 1. 创建数组 numpy中可以通过numpy.array()函数创建数组,函数接受一个可迭代对象作为参数,例如: ```python import numpy as np # 创建一维数组 a = np.array([1, 2, 3]) print(a) # 输出:[1 2 3] # 创建二维数组 b = np.array([[1, 2], [3, 4]]) print(b) # 输出:[[1 2] # [3 4]] ``` 2. 索引和切片 numpy中的数组索引和切片与Python中的列表相似,可以使用整数索引和切片语法。例如: ```python # 一维数组的索引和切片 a = np.array([1, 2, 3, 4, 5]) print(a[2]) # 输出:3 print(a[:3]) # 输出:[1 2 3] # 多维数组的索引和切片 b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(b[1, 2]) # 输出:6 print(b[:2, 1:]) # 输出:[[2 3] # [5 6]] ``` 3. 数学函数 numpy中包含了许多常用的数学函数,例如: ```python # 平方函数 a = np.array([1, 2, 3]) print(np.square(a)) # 输出:[1 4 9] # 开方函数 b = np.array([1, 4, 9]) print(np.sqrt(b)) # 输出:[1. 2. 3.] # 取绝对值函数 c = np.array([-1, -2, 3]) print(np.absolute(c)) # 输出:[1 2 3] # 求和函数 d = np.array([1, 2, 3]) print(np.sum(d)) # 输出:6 # 求平均值函数 e = np.array([1, 2, 3]) print(np.mean(e)) # 输出:2.0 # 求标准差函数 f = np.array([1, 2, 3]) print(np.std(f)) # 输出:0.816496580927726 ``` 4. 线性代数函数 numpy中包含了许多常用的线性代数函数,例如: ```python # 点积函数 a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) print(np.dot(a, b)) # 输出:[[19 22] # [43 50]] # 矩阵求逆函数 c = np.array([[1, 2], [3, 4]]) print(np.linalg.inv(c)) # 输出:[[-2. 1. ] # [ 1.5 -0.5]] # 特征值与特征向量函数 d = np.array([[1, 2], [2, 1]]) eig_values, eig_vectors = np.linalg.eig(d) print(eig_values) # 输出:[ 3. -1.] print(eig_vectors) # 输出:[[ 0.70710678 -0.70710678] # [ 0.70710678 0.70710678]] ``` 5. 随机数函数 numpy还包含了许多常用的随机数函数,例如: ```python # 随机整数函数 a = np.random.randint(1, 10, (3, 4)) print(a) # 输出:[[5 8 7 4] # [4 4 4 4] # [9 9 9 2]] # 随机浮点数函数 b = np.random.rand(2, 3) print(b) # 输出:[[0.81317738 0.24850358 0.98823489] # [0.35087941 0.98767991 0.17130925]] ``` 以上是numpy函数的一些常见用法numpy函数的功能非常丰富,还有很多其他函数可以满足各种应用需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值