# Reclassifying raster image in Google Colab with NumPy

In Google Earth Engine's Java API, I have the following code that works and am trying to use this in Google Colab with Python.

How do you reclassify the pixels in a raster image in Python?

``````var FinalResult= datasetA.where(sdA.gt(sdAthreshold),56).where(sdA.lte(sdAthreshold).and(mA.gte(All_A)),102).where(sdA.lte(sdAthreshold).and(mA.lt(ALL_A)),76).rename('Result');

``````

I have tried the following after reading NumPy reclassification documentation to no avail. This did not work.

``````import numpy as np
FinalResult= np.select.datasetA([sdA > sdAthreshold, (sdA <= sdAthreshold) & (mA >= AllA), (sdA <= sdAthreshold) & (mA < AllA)],[56,102,76])
``````

And neither did this.

``````datasetA[np.where(sdA > sdAthreshold)] = 1
datasetA[np.where((sdA <= sdAthreshold) & (mA >= AllA))] = 2
datasetA[np.where((sdA <= sdAthreshold) & (mA < AllA))] = 3
``````
• Could you please explain what you want to do? I don't know aboud earth engine but I'm good at python so I may be able to help you if you explain what you mean by reclassifying. Commented Aug 9, 2021 at 22:39
• Thank you! I have a raster image collection, 10 years of data (datasetA). I want to classify the land area (each 30x30m pixel within my area of interest) in the raster collection as '76', '101' or '56' based on the standard deviation of a pixel across those 10 years and the mean of all pixels within a single year (single raster in the collection). For example, if the standard deviation of a pixel across the 10 yrs of data is greater than X, and the mean of the pixel across the 10 yrs of data is more often greater than the mean of pixels within a year, I want the pixel to be labeled 101, etc. Commented Aug 10, 2021 at 12:23
• ok thanks I think I got what you want. what are the shapes of your arrays datasetA, sdA, sdAthreshold, mA and AllA? providing this I can explain what you want. by the way I post an answer, if it didn't work, tell me the shapes and I will try to give a proper answer. Commented Aug 10, 2021 at 13:28
• datasetA is an image collection of Landsat 8 composite NDVI, sdA is the standard deviation of datasetA (sdA = datasetA.reduce(ee.Reducer.stdDev()); ), the sdAthreshold is just a constant value (20), mA is the mean of datasetA (mA = datasetA.reduce(ee.Reducer.mean()); ), AllA is the average of all the pixels across all years. Commented Aug 10, 2021 at 13:35
• yeah but I meant the shapes. for example sdA.shape if it's a numpy array. it should be a tuple for exmaple (1000,1000, 3) Commented Aug 10, 2021 at 13:40

If your variables where numpy arrays, you could simply do this:

``````datasetA[sdA > sdAthreshold] = 1
datasetA[np.logical_and(sdA <= sdAthreshold, mA >= AllA)] = 2
datasetA[np.logical_and(sdA <= sdAthreshold, mA < AllA)] = 3
``````

``````FinalResult = sdG.where(sdG.gt(sdGthreshold),56) \