累积在 Numpy 中对所有元素应用运算符的结果
要累积对所有元素应用运算符的结果,请在 Python Numpy 中使用 **numpy.accumulate()** 方法。我们展示了加法和乘法的示例。**add.accumulate()** 等效于 np.cumsum()。
numpy.ufunc 具有对整个数组逐元素操作的功能。ufunc 使用 C 编写(为了速度)并与 NumPy 的 ufunc 功能链接到 Python 中。
通用函数(简称 ufunc)是在逐元素基础上对 ndarrays 进行操作的函数,支持数组广播、类型转换和几个其他标准功能。也就是说,ufunc 是对函数的“矢量化”包装器,该函数接受固定数量的特定输入并产生固定数量的特定输出。
步骤
首先,导入所需的库 -
import numpy as np
创建一个一维数组 -
arr = np.array([2, 3, 5])
显示数组 -
print("Array...
", arr)获取数组的类型 -
print("
Our Array type...
", arr.dtype)
获取数组的维度 -
print("
Our Array Dimensions...
",arr.ndim)要累积对所有元素应用运算符的结果,请在 Python Numpy 中使用 numpy.accumulate() 方法 -
加法累积:add.accumulate() 等效于 np.cumsum() -
print("
Add accumulate...
",np.add.accumulate(arr))
乘法累积 -
print("
Multiply accumulate...
",np.multiply.accumulate(arr))示例
import numpy as np
import numpy.ma as ma
# The numpy.ufunc has functions that operate element by element on whole arrays.
# ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility
# Create a 1d array
arr = np.array([2, 3, 5])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimensions...
",arr.ndim)
# To Accumulate the result of applying the operator to all elements, use the numpy.accumulate() method in Python Numpy
# Add accumulate
# The add.accumulate() is equivalent to np.cumsum().
print("
Add accumulate...
",np.add.accumulate(arr))输出
Array... [2 3 5] Our Array type... int64 Our Array Dimensions... 1 Add accumulate... [ 2 5 10]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP