Numpy 的 cumsum(~)
方法返回一個保存輸入數組的累積和的數組。
參數
1. a
| array_like
輸入數組。
2. axis
| None
或 int
| optional
允許的值如下:
軸 |
意義 |
---|---|
0 |
累積和按列計算 |
1 |
累積和按行計算 |
None |
使用整個數組計算累積和 |
默認情況下,axis=None
。
3. dtype
| string
或 type
| optional
返回數組的所需數據類型。默認情況下,數據類型與輸入數組的數據類型相同。
4. out
| Numpy array
| optional
用於放置結果的 Numpy 數組。
返回值
一個 Numpy 數組,保存輸入元素的累積和。
例子
一維數組
計算一維數組的累積和:
x = np.array([1,2,3])
np.cumsum(x)
array([1, 3, 6])
在這裏,我們正在執行以下計算:
[0] 1 = 1
[1] 1 + 2 = 3
[2] 1 + 2 + 3 = 6
二維數組
考慮以下二維數組:
x = np.array([[1,2], [3,4]])
x
array([[1, 2],
[3, 4]])
所有值
要計算所有值的累積和:
x = np.array([[1,2], [3,4]])
np.cumsum(x)
array([ 1, 3, 6, 10])
按列
要按列計算累積和,請設置 axis=0
:
x = np.array([[1,2], [3,4]])
np.cumsum(x, axis=0)
array([[1, 2],
[4, 6]])
逐行
要按行計算累積和,請設置 axis=1
:
x = np.array([[1,2], [3,4]])
np.cumsum(x, axis=1)
array([[1, 3],
[3, 7]])
指定數據類型
要獲取數據類型 float
的數組:
x = np.array([1,2,3])
np.cumsum(x, dtype=float)
array([1., 3., 6.])
這裏,.
表示數字是浮點數。
相關用法
- Python cuml.metrics.pairwise_distances.pairwise_distances用法及代碼示例
- Python cuml.neighbors.KNeighborsClassifier用法及代碼示例
- Python cuml.ensemble.RandomForestRegressor用法及代碼示例
- Python cuml.svm.SVC用法及代碼示例
- Python cuml.svm.SVR用法及代碼示例
- Python NumPy cumprod方法用法及代碼示例
- Python cuml.Lasso用法及代碼示例
- Python cuml.tsa.ARIMA.predict用法及代碼示例
- Python cuml.multiclass.OneVsRestClassifier用法及代碼示例
- Python cuml.random_projection.GaussianRandomProjection用法及代碼示例
- Python cuml.MBSGDRegressor用法及代碼示例
- Python cuml.experimental.preprocessing.PolynomialFeatures用法及代碼示例
- Python cuml.PCA用法及代碼示例
- Python cuml.feature_extraction.text.HashingVectorizer用法及代碼示例
- Python cuml.DBSCAN用法及代碼示例
- Python cuml.dask.feature_extraction.text.TfidfTransformer用法及代碼示例
- Python cuml.TruncatedSVD用法及代碼示例
- Python cuml.common.memory_utils.using_output_type用法及代碼示例
- Python cuml.preprocessing.text.stem.PorterStemmer用法及代碼示例
- Python cuml.experimental.preprocessing.SimpleImputer用法及代碼示例
- Python cuml.experimental.preprocessing.add_dummy_feature用法及代碼示例
- Python cuml.dask.manifold.UMAP用法及代碼示例
- Python cuml.LogisticRegression用法及代碼示例
- Python cuml.dask.decomposition.PCA用法及代碼示例
- Python cuml.experimental.preprocessing.KBinsDiscretizer用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | cumsum method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。