累積在 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