Python numpy cumsum() function returns the cumulative sum of the elements along the given axis.

Python numpy cumsum()函数返回沿给定轴的元素的累积和。

Python numpy cumsum()语法 (Python numpy cumsum() syntax)

The cumsum() method syntax is:

cumsum()方法的语法为:

cumsum(array, axis=None, dtype=None, out=None)
  • The array can be ndarray or array-like objects such as nested lists.数组可以是ndarray或类似数组的对象,例如嵌套列表。
  • The axis parameter defines the axis along which the cumulative sum is calculated. If the axis is not provided then the array is flattened and the cumulative sum is calculated for the result array.axis参数定义沿其计算累计总和的轴。 如果未提供轴,则将数组展平,并为结果数组计算累计和。
  • The dtype parameter defines the output data type, such as float and int.dtype参数定义输出数据类型,例如float和int。
  • The out optional parameter is used to specify the array for the result.out可选参数用于指定结果数组。

Python numpy cumsum()示例 (Python numpy cumsum() Examples)

Let’s look at some examples of calculating cumulative sum of numpy array elements.

让我们看一些计算numpy数组元素的累积和的示例。

1.无轴的numpy数组元素的累积和 (1. Cumulative Sum of Numpy Array Elements without axis)

import numpy as nparray1 = np.array([[1, 2],[3, 4],[5, 6]])total = np.cumsum(array1)
print(f'Cumulative Sum of all the elements is {total}')

Output: Cumulative Sum of all the elements is [ 1 3 6 10 15 21]

输出Cumulative Sum of all the elements is [ 1 3 6 10 15 21]

Here, the array is first flattened to [ 1 2 3 4 5 6]. Then the cumulative sum is calculated, resulting in [ 1 3 6 10 15 21].

在此,首先将数组展平为[1 2 3 4 5 6]。 然后,计算累加和,得出[1 3 6 10 15 21]。

2.沿轴的累计总和 (2. Cumulative Sum along the axis)

import numpy as nparray1 = np.array([[1, 2],[3, 4],[5, 6]])total_0_axis = np.cumsum(array1, axis=0)
print(f'Cumulative Sum of elements at 0-axis is:\n{total_0_axis}')total_1_axis = np.cumsum(array1, axis=1)
print(f'Cumulative Sum of elements at 1-axis is:\n{total_1_axis}')

Output:

输出:

Cumulative Sum of elements at 0-axis is:
[[ 1  2][ 4  6][ 9 12]]
Cumulative Sum of elements at 1-axis is:
[[ 1  3][ 3  7][ 5 11]]

3.指定累积和数组的数据类型 (3. Specifying data type for the cumulative sum array)

import numpy as nparray1 = np.array([[1, 2],[3, 4],[5, 6]])total_1_axis = np.cumsum(array1, axis=1, dtype=float)
print(f'Cumulative Sum of elements at 1-axis is:\n{total_1_axis}')

Output:

输出:

Cumulative Sum of elements at 1-axis is:
[[ 1.  3.][ 3.  7.][ 5. 11.]]

Reference: API Doc

参考 : API文档

翻译自: https://www.journaldev.com/32801/numpy-cumsum-in-python

Python中的numpy.cumsum()相关推荐

  1. python有哪些作用-Python中的NumPy是什么?有什么作用?

    Python中的NumPy是什么?NumPy或NumericPython是用于科学计算的通用数组处理python软件包.它包含许多强大的功能,其中包括:具有许多有用功能的健壮的多维数组对象.用于将其他 ...

  2. pythonchar中的拟合方法_在python中利用numpy求解多项式以及多项式拟合的方法

    构建一个二阶多项式:x^2 - 4x + 3 多项式求解 >>> p = np.poly1d([1,-4,3]) #二阶多项式系数 >>> p(0) #自变量为0时 ...

  3. [转载] python中的numpy模块和pandas模块的区别_numpy 模块和 pandas 模块

    参考链接: Python中的numpy.radians和deg2rad 一.numpy模块 1.1 numpy 简介 numpy是python的一种开源的数值计算扩展库,这种库可用来存储和处理大型nu ...

  4. [转载] Python中的numpy linalg模块

    参考链接: Python中的numpy.bmat 原文链接:https://www.cnblogs.com/xieshengsen/p/6836430.html # 线性代数 # numpy.lina ...

  5. [转载] [转载] python中的Numpy库入门

    参考链接: Python中的numpy.ones_like 参考链接: Python中的numpy.full_like 目录 N维数组对象: ndarray引例ndarray对象的属性ndarray数 ...

  6. [转载] python中的Numpy库入门

    参考链接: Python中的numpy.full_like 目录 N维数组对象: ndarray引例ndarray对象的属性ndarray数组的元素类型ndarray数组的创建方法从Python中的列 ...

  7. Python中的numpy.zeros()

    Python numpy.zeros() function returns a new array of given shape and type, where the element's value ...

  8. Python中利用numpy将数组(矩阵)存成csv文件,将csv文件读取为数组(矩阵)

    Python中利用numpy将数组(矩阵)存成csv文件,将csv文件读取为数组(矩阵) 本博客转载自:https://blog.csdn.net/vernice/article/details/50 ...

  9. 用Python中的Numpy实现简单高效的扑克牌API(附代码)

    用Python中的Numpy实现简单高效的扑克牌API 扑克牌可以用字典或者列表来表示,但是这样的话会浪费很多的空间.因此,本项目将使用Numpy来实现,这样的话能保证性能的情况下减少存储成本. 一般 ...

最新文章

  1. 网络表述学习经典论文——DeepWalk
  2. select for update
  3. 网络设置巨形帧_网络工程师笔记:VLAN技术
  4. 屏幕最左上角的 飞秋官方下载 字符
  5. pybamm库学习-tutorial
  6. plsql tables 没有表_技术分享 | 在磁盘上查找 MySQL 表的大小
  7. 【经验之谈】SAP中的普通屏幕使用定制控制
  8. 主成分分析(PCA)实现代码
  9. 开运算和闭运算的性质
  10. python读取海康威视摄像头价格_OpenCV+海康威视摄像头的实时读取
  11. sdk环境变量配置win10_sdk环境变量配置好检查
  12. IDEA设置字体和背景颜色以及快捷键大全
  13. Java 简单计算器
  14. 正则表达式清除空格和html标签中的nbsp;空格
  15. 人在囧途之tar命令
  16. 松鼠症 知识过载与疲于奔命
  17. 官方文件出炉!北京住建委权威解读商住限购
  18. python九宫格拼图_Python制作九宫格图片
  19. Android从启动到程序运行发生的事情
  20. 2020 CUMCM全国大学生数学建模竞赛 A题 Notes

热门文章

  1. 和信贷接入百行征信之后......
  2. Linux之setenv()/getenv()设置/获取环境变量
  3. 超强的波多黎各,值得尊敬的美国队
  4. 开篮球馆需要什么_开一个篮球馆怎么样?开篮球馆需要办理什么手续?
  5. Mac系统下使用SecureCRT连接Linux虚拟机及上传文件
  6. 堡垒机AccessClient插件在mac系统下闪退的解决办法
  7. linux 串口 延迟,linux串口操作及设置详解
  8. ArcGIS Engine环境下VS窗体与Unity虚拟现实地理空间的嵌套和交互
  9. 在excel中如何进行快速筛选
  10. 创新朋友圈植入广告,享受精准的朋友圈营销