ee.Array.accum

沿著指定軸累加陣列元素,方法是將結果的每個元素設為沿著該軸縮減的元素,直到目前位置 (含目前位置) 為止。可用於計算累計總和、單調遞增序列等。

用量傳回
Array.accum(axis, reducer)陣列
引數類型詳細資料
這個:array陣列要累加的陣列。
axis整數要執行累計的軸。
reducerReducer,預設值:null用來累加值的 Reducer。預設值為 SUM,沿著指定軸產生每個向量的累計總和。

範例

程式碼編輯器 (JavaScript)

print(ee.Array([-1]).accum(0));  // [-1]
print(ee.Array([-2, 1]).accum(0));  // [-2, -1]
print(ee.Array([-2, 1, 9]).accum(0));  // [-2, -1, 8]

// accum over 2D arrays with different axes.
print(ee.Array([[1, 3], [5, 7]]).accum(0));  // [[1,3],[6,10]]
print(ee.Array([[1, 3], [5, 7]]).accum(1));  // [[1,4],[5,12]]

// sum is the default reducer.
print(ee.Array([2, -2, 3, 1]).accum(0));  // [2,0,3,4]
print(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.sum()));  // [2,0,3,4]

// Some example reducers.
print(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.max()));  // [2,2,3,3]
print(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.mean()));  // [2,0,1,1]
print(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.min()));  // [2,-2,-2,-2]
print(ee.Array([2, -2, 3]).accum(0, ee.Reducer.product()));  // [2,-4,-12]

Python 設定

請參閱 Python 環境頁面,瞭解 Python API 和如何使用 geemap 進行互動式開發。

import ee
import geemap.core as geemap

Colab (Python)

display(ee.Array([-1]).accum(0))  # [-1]
display(ee.Array([-2, 1]).accum(0))  # [-2, -1]
display(ee.Array([-2, 1, 9]).accum(0))  # [-2, -1, 8]

# accum over 2D arrays with different axes.
display(ee.Array([[1, 3], [5, 7]]).accum(0))  # [[1, 3],[6, 10]]
display(ee.Array([[1, 3], [5, 7]]).accum(1))  # [[1, 4],[5, 12]]

# sum is the default reducer.
display(ee.Array([2, -2, 3, 1]).accum(0))  # [2, 0, 3, 4]

# [2, 0, 3, 4]
display(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.sum()))


# Some example reducers.
# [2, 2, 3, 3]
display(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.max()))


# [2, 0, 1, 1]
display(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.mean()))

# [2, -2, -2, -2]
display(ee.Array([2, -2, 3, 1]).accum(0, ee.Reducer.min()))

# [2, -4, -12]
display(ee.Array([2, -2, 3]).accum(0, ee.Reducer.product()))