Select to view content in your preferred language

Calculating percentiles of a raster

3844
1
04-09-2012 12:14 PM
by Anonymous User
Not applicable
I have a floating point raster with a range of values from 0-200,000.  I want to find the top 5% of the cells based on this value.  I thought the slice function, into 20 groups, would do this, but it doesn't seem to work.  Plus, I'd like to keep the data as floating so it doesn't round the values.  Any thoughts on how to do this? Thanks.
0 Kudos
1 Reply
ChrisBater
Deactivated User
You can convert the raster to a numpy array, flatten and sort the array, then determine the percentiles based on the flattened array's size. It would look something like this:

def firstPercentile(array):
    #remove no data values, in this case zeroes, returning a flattened array
    print 'removing no data values and flattening array.....'
    flatArray = array[array != 0.0]
    print 'sorting array....'
    flatArray = np.sort(flatArray)
    
    #report some summary stats
    print 'min = ', np.min(flatArray)   
    print 'median = ', flatArray[int(np.size(flatArray) * 0.50)]
    print 'max = ', np.max(flatArray)  
    firstPercentile = flatArray[int(np.size(flatArray) * 0.01)]
    print '1st percentile = ', firstPercentile



Chris
0 Kudos