NumPy 的 cumprod(~)
方法返回一個包含輸入數組的累積乘積的數組。
參數
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.cumprod(x)
array([1, 2, 6])
在這裏,我們正在執行以下計算:
[0] 1 = 1
[1] 1 * 2 = 2
[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.cumprod(x)
array([ 1, 2, 6, 24])
按列
要按列計算累積乘積,請設置 axis=0
:
x = np.array([[1,2], [3,4]])
np.cumprod(x, axis=0)
array([[1, 2],
[3, 8]])
逐行
要按行計算累積乘積,請設置 axis=1
:
x = np.array([[1,2], [3,4]])
np.cumprod(x, axis=1)
array([[ 1, 2],
[ 3, 12]])
指定數據類型
要獲取數據類型 float
的數組:
x = np.array([1,2,3])
np.cumprod(x, dtype=float)
array([1., 2., 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 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 NumPy cumsum方法用法及代碼示例
- 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 | cumprod method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。