Hi shouvik jha ,
The easiest way would be to follow the instruction provided by Dan Patterson and Jayanta Poddar . You will have two zonas statistics tables as result, one for the positive values and one for the negative values both with the respective count you're after.
Since in recent occasions we have done some things with using rasters converted to numpy arrays, below the an example of doing it the "hard way". This will only work if both the zone raster and the value raster are aligned (same extent and cell size and coordinate system).
The value raster with the zone:
The resulting statistics:
The code used:
def main():
print "load modules..."
import arcpy
arcpy.env.overwriteOutput = True
import numpy as np
zones_ras = r'C:\GeoNet\CountPosNegPixels\data2.gdb\test'
values_ras = r'C:\GeoNet\CountPosNegPixels\data2.gdb\corr'
nodata = -3.4028235e+38
print "create numpy rasters..."
np_zones = arcpy.RasterToNumPyArray(zones_ras)
np_values = arcpy.RasterToNumPyArray(values_ras)
print "read props numpy raster..."
rows = np_zones.shape[0]
cols = np_zones.shape[1]
print " - rows:", rows
print " - cols:", cols
print "loop through pixels..."
pix_cnt = 0
dct = {}
for row in range(rows):
for col in range(cols):
pix_cnt += 1
if pix_cnt % 5000 == 0:
print " - row:", row, " col:", col, " pixel:", pix_cnt
zone = np_zones[row, col]
value = np_values[row, col]
if zone < 255:
if zone in dct:
stats = dct[zone]
stats = UpdateStats(stats, value, nodata)
else:
stats = CreateStats(value, nodata)
dct[zone] = stats
print "\nZone\tsum\tcnt\tmean\tmin\tmax\tcnt_neg\tcnt_pos\tcnt_nodata"
for zone, stats in sorted(dct.items()):
print zone, '\t', '\t'.join(str(a).replace('.', ',') for a in stats)
def UpdateStats(stats, value, nodata):
if value > nodata / 2.0:
stats[0] += value
stats[1] += 1
stats[2] = float(stats[0]) / float(stats[1])
if value < stats[3]:
stats[3] = value
if value > stats[4]:
stats[4] = value
if value < 0:
stats[5] += 1
if value > 0:
stats[6] += 1
else:
stats[7] += 1
return stats
def CreateStats(value, nodata):
if value > nodata / 2.0:
return [value, 1, value, value, value, 1 if value < 0 else 0, 1 if value > 0 else 0, 0]
else:
return [0, 0, None, 99999999, -9999999, 0, 0, 1]
if __name__ == '__main__':
main()
But once again, it is much easier following the steps as described by Dan and Jayanta.