Announcement: All noncommercial projects registered to use Earth Engine before April 15, 2025 must verify noncommercial eligibility to maintain Earth Engine access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-13 UTC."],[[["\u003cp\u003e\u003ccode\u003eImage.arrayReduce()\u003c/code\u003e reduces elements of each array pixel along the specified axes using a provided reducer.\u003c/p\u003e\n"],["\u003cp\u003eIt takes an input image, a reducer function, a list of axes to reduce, and an optional field axis for reducers with multiple inputs or outputs.\u003c/p\u003e\n"],["\u003cp\u003eThe output image has a length of 1 in all reduced axes.\u003c/p\u003e\n"],["\u003cp\u003eFor reducers with multiple outputs, you may need to add dimensions to the input array to store the results.\u003c/p\u003e\n"],["\u003cp\u003eExamples show reducing 1D and 2D array images using \u003ccode\u003eee.Reducer.sum()\u003c/code\u003e and \u003ccode\u003eee.Reducer.minMax()\u003c/code\u003e.\u003c/p\u003e\n"]]],["The `arrayReduce` method reduces elements within each array pixel of an image. It takes a `reducer`, a list of `axes` to reduce, and optionally, a `fieldAxis` for reducers with multiple inputs or outputs. The method collapses the specified axes to a length of 1. Example operations include summing elements in 1D or 2D arrays along specified axes or calculating the min/max with multiple outputs, requiring adjusted dimensions. The results are displayed as array pixels.\n"],null,["# ee.Image.arrayReduce\n\nReduces elements of each array pixel.\n\n\u003cbr /\u003e\n\n| Usage | Returns |\n|---------------------------------------------------|---------|\n| Image.arrayReduce`(reducer, axes, `*fieldAxis*`)` | Image |\n\n| Argument | Type | Details |\n|---------------|------------------------|------------------------------------------------------------------------------------------------------------------|\n| this: `input` | Image | Input image. |\n| `reducer` | Reducer | The reducer to apply. |\n| `axes` | List | The list of array axes to reduce in each pixel. The output will have a length of 1 in all these axes. |\n| `fieldAxis` | Integer, default: null | The axis for the reducer's input and output fields. Only required if the reducer has multiple inputs or outputs. |\n\nExamples\n--------\n\n### Code Editor (JavaScript)\n\n```javascript\n// A function to print arrays for a selected pixel in the following examples.\nfunction sampArrImg(arrImg) {\n var point = ee.Geometry.Point([-121, 42]);\n return arrImg.sample(point, 500).first().get('array');\n}\n\n// Create a 1D array image with length 6.\nvar arrayImg1D = ee.Image([0, 1, 2, 3, 4, 5]).toArray();\nprint('1D array image (pixel)', sampArrImg(arrayImg1D));\n// [0, 1, 2, 3, 4, 5]\n\n// Sum the elements in the 1D array image.\nvar arrayImg1Dsum = arrayImg1D.arrayReduce(ee.Reducer.sum(), [0]);\nprint('1D array image sum (pixel)', sampArrImg(arrayImg1Dsum));\n// [15]\n\n// Create a 2D array image with 2 rows and 3 columns.\nvar arrayImg2D = arrayImg1D.arrayReshape(ee.Image([2, 3]).toArray(), 2);\nprint('2D array image (pixel)', sampArrImg(arrayImg2D));\n// [[0, 1, 2],\n// [3, 4, 5]]\n\n// Sum 2D array image along 0-axis.\nvar arrayImg2DsumRow = arrayImg2D.arrayReduce(ee.Reducer.sum(), [0]);\nprint('2D array image sum rows (pixel)', sampArrImg(arrayImg2DsumRow));\n// [[3, 5, 7]]\n\n// Sum 2D array image along 1-axis.\nvar arrayImg2DsumCol = arrayImg2D.arrayReduce(ee.Reducer.sum(), [1]);\nprint('2D array image sum columns (pixel)', sampArrImg(arrayImg2DsumCol));\n// [[3],\n// [12]]\n\n// Sum 2D array image 0-axis and 1-axis.\nvar arrayImg2DsumRowCol = arrayImg2D.arrayReduce(ee.Reducer.sum(), [0, 1]);\nprint('2D array image sum columns (pixel)', sampArrImg(arrayImg2DsumRowCol));\n// [[15]]\n\n// For reducers that provide several outputs (like minMax and percentile),\n// you need to ensure you have a dimension to hold the results. For instance,\n// if you want minMax for a 1D array, add a second dimension.\nvar arrayImg1Dto2D = arrayImg1D.toArray(1);\nprint('1D array image to 2D', sampArrImg(arrayImg1Dto2D));\n// [[0],\n// [1],\n// [2],\n// [3],\n// [4],\n// [5]]\n\n// Calculate min and max for 2D array, use the fieldAxis parameter.\nvar minMax1D = arrayImg1Dto2D.arrayReduce(ee.Reducer.minMax(), [0], 1);\nprint('1D array image minMax (pixel)', sampArrImg(minMax1D));\n// [[0, 5]]\n\n// If your array image is 2D and you want min and max, add a third dimension.\nvar arrayImg2Dto3D = arrayImg2D.toArray(2);\nprint('2D array image to 3D', sampArrImg(arrayImg2Dto3D));\n// [[[0], [1], [2]],\n// [[3], [4], [5]]]\n\n// Calculate min and max along the 0-axis, store results in 2-axis.\nvar minMax2D = arrayImg2Dto3D.arrayReduce(ee.Reducer.minMax(), [0], 2);\nprint('2D array image minMax (pixel)', sampArrImg(minMax2D));\n// [[[0, 3],\n// [1, 4],\n// [2, 5]]]\n```\nPython setup\n\nSee the [Python Environment](/earth-engine/guides/python_install) page for information on the Python API and using\n`geemap` for interactive development. \n\n```python\nimport ee\nimport geemap.core as geemap\n```\n\n### Colab (Python)\n\n```python\n# A function to print arrays for a selected pixel in the following examples.\ndef samp_arr_img(arr_img):\n point = ee.Geometry.Point([-121, 42])\n return arr_img.sample(point, 500).first().get('array')\n\n# Create a 1D array image with length 6.\narray_img_1d = ee.Image([0, 1, 2, 3, 4, 5]).toArray()\nprint('1D array image (pixel):', samp_arr_img(array_img_1d).getInfo())\n# [0, 1, 2, 3, 4, 5]\n\n# Sum the elements in the 1D array image.\narray_img_1d_sum = array_img_1d.arrayReduce(ee.Reducer.sum(), [0])\nprint('1D array image sum (pixel):', samp_arr_img(array_img_1d_sum).getInfo())\n# [15]\n\n# Create a 2D array image with 2 rows and 3 columns.\narray_img_2d = array_img_1d.arrayReshape(ee.Image([2, 3]).toArray(), 2)\nprint('2D array image (pixel):', samp_arr_img(array_img_2d).getInfo())\n# [[0, 1, 2],\n# [3, 4, 5]]\n\n# Sum 2D array image along 0-axis.\narray_img_2d_sum_row = array_img_2d.arrayReduce(ee.Reducer.sum(), [0])\nprint(\n '2D array image sum rows (pixel):',\n samp_arr_img(array_img_2d_sum_row).getInfo()\n)\n# [[3, 5, 7]]\n\n# Sum 2D array image along 1-axis.\narray_img_2d_sum_col = array_img_2d.arrayReduce(ee.Reducer.sum(), [1])\nprint(\n '2D array image sum columns (pixel):',\n samp_arr_img(array_img_2d_sum_col).getInfo()\n)\n# [[3],\n# [12]]\n\n# Sum 2D array image 0-axis and 1-axis.\narray_img_2d_sum_row_col = array_img_2d.arrayReduce(ee.Reducer.sum(), [0, 1])\nprint(\n '2D array image sum columns (pixel):',\n samp_arr_img(array_img_2d_sum_row_col).getInfo()\n)\n# [[15]]\n\n# For reducers that provide several outputs (like minMax and percentile),\n# you need to ensure you have a dimension to hold the results. For instance,\n# if you want minMax for a 1D array, add a second dimension.\narray_img_1d_to_2d = array_img_1d.toArray(1)\nprint('1D array image to 2D:', samp_arr_img(array_img_1d_to_2d).getInfo())\n# [[0],\n# [1],\n# [2],\n# [3],\n# [4],\n# [5]]\n\n# Calculate min and max for 2D array, use the fieldAxis parameter.\nmin_max_1d = array_img_1d_to_2d.arrayReduce(ee.Reducer.minMax(), [0], 1)\nprint('1D array image minMax (pixel):', samp_arr_img(min_max_1d).getInfo())\n# [[0, 5]]\n\n# If your array image is 2D and you want min and max, add a third dimension.\narray_img_2d_to_3d = array_img_2d.toArray(2)\nprint('2D array image to 3D:', samp_arr_img(array_img_2d_to_3d).getInfo())\n# [[[0], [1], [2]],\n# [[3], [4], [5]]]\n\n# Calculate min and max along the 0-axis, store results in 2-axis.\nmin_max_2d = array_img_2d_to_3d.arrayReduce(ee.Reducer.minMax(), [0], 2)\nprint('2D array image minMax (pixel):', samp_arr_img(min_max_2d).getInfo())\n# [[[0, 3],\n# [1, 4],\n# [2, 5]]]\n```"]]