Student2014
Student2014

Reputation: 109

Calculating kurtosis from a numpy array

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

x = arcpy.RasterToNumPyArray(input_Raster)
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?

Upvotes: 9

Views: 25705

Answers (1)

Nick T
Nick T

Reputation: 26747

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])

Upvotes: 16

Related Questions