Calculating kurtosis from a numpy array?

Student2014 picture Student2014 · Oct 15, 2014 · Viewed 14k times · Source

I am trying to calculate 'kurtosis', as well as other statistics from a numpy array. Calculating Min, Max, Mean and Standard Deviation are easy as I've just done.

import arcpy
arr = arcpy.RasterToNumPyArray(input_Raster) 
x = arr
print 'Min =', x.min()
print 'Max =', x.max() 
print 'Mean =', x.mean()
print 'Standard Deviation =', x.std()

Which outputs:

Min = 1.87895
Max = 16.8343
Mean = 8.03462
Standard Deviation = 1.52192

But this method doesn't work for Kurtosis! As I've tried

print 'Kurtosis =', x.kurtosis()

And I get: AttributeError: 'numpy.ndarray' object has no attribute 'kurtosis'

What would be the simplest code I could use to incorporate into my own to calculate a kurtosis result? Thanks.

Answer

Nick T picture Nick T · Oct 15, 2014

Numpy is restricted to fairly basic array operations, you need to reach out to it's more educated brother, Scipy, to get more advanced stats functions.

scipy.stats.kurtosis(a, axis=0, fisher=True, bias=True)

Computes the kurtosis (Fisher or Pearson) of a dataset.

So, from scipy.stats import kurtosis, then kurtosis(x).

In general, the methods on Numpy arrays are restricted to only the most basic operations (max, min, etc.). Slightly more functionality is eked out of Numpy methods (e.g. numpy.diff), and more still from Scipy (scipy.optimize.[whatever] scipy.signal.[whatever], scipy.stats.[whatever])