I am want to calculate repetitive values from raster and save it into dict, how can i do it ? i have done some coding but its not working help me to solve the problem. below is my code:
import numpy as np import arcpy from arcpy import env env.workspace = r"D:Results" rasterlist = arcpy.ListRasters() array = arcpy.RasterToNumPyArray(raster,nodata_to_value=0) array1 = np.reshape(array, (1,np.product(array.shape))) mydict = {} for i in array1: if i in mydict: mydict += 1 else: mydict = 1 print mydict
Actually i want to implement the following formula:
where g is the number of tied groups and tp is the number of data in the pth group. For example, in the sequence {23, 24, trace, 6, trace, 24, 24, trace, 23} we have g = 3, t{ = 2 for the tied value 23, t2 = 3 for the tied value 24, and r3 = 3 for the three trace values, where as n = 1
To list the occurrence of values in the numpy array following your code you could change it to:
import numpy as np import arcpy from arcpy import env env.workspace = r"D:\Results" rasterlist = arcpy.ListRasters() for raster in rasterlist: array = arcpy.RasterToNumPyArray(raster,nodata_to_value=0) mydict = {} for row in array: for pix in row: if pix in mydict: mydict[pix] += 1 else: mydict[pix] = 1 print mydict
Numpy is much more powerful than a way to loop over the values of a raster. You probably should do some serious reading (NumPy — Numpy) . Maybe an expert on numpy (Dan Patterson) has some suggestions for you.
Yes Xander Bakker you are wright i don't know much about Numpy, i need some guide book...
start with
NumPy Reference — NumPy v1.9 Manual
for numpy python packages
http://www.lfd.uci.edu/~gohlke/pythonlibs/
other links
http://rintintin.colorado.edu/~wajo8931/docs/jochem_aag2011.pdf
I will put these on my reference page where I have posted a few articles
particular things like
Collections in numpy: producing frequency distributions and graphing
for the graphing component
you can take a numpy array, flatten it and even use collections to simplify class counts
>>> import numpy as np >>> import collections >>> arr = [[1,2,3],[1,2,3],[1,1,2]] # assume that this is a raster >>> arr = np.array(arr) # you will use RasterToNumpyArray to get this arr array([[1, 2, 3], [1, 2, 3], [1, 1, 2]]) >>> flat = arr.flatten() >>> flat array([1, 2, 3, 1, 2, 3, 1, 1, 2])
then use the following as a quick solution
>>>
but in your case collections will help but
>>> import collections >>> seq [23, 24, 6, 24, 24, 23] >>> dict = collections.Counter(seq) >>> dict Counter({24: 3, 23: 2, 6: 1}) >>> keys = dict.keys() >>> keys [24, 6, 23] >>> values = dict.values() >>> values [3, 1, 2] >>>
Thank you helping...
Ahsan just did a new one if you work with arrays and tabular data structures this might help as well