Skip to content
Advertisement

How can I get descriptive statistics of a NumPy array?

I use the following code to create a numpy-ndarray. The file has 9 columns. I explicitly type each column:

JavaScript

Now I would like to get some descriptive statistics for each column (min, max, stdev, mean, median, etc.). Shouldn’t there be an easy way to do this?

I tried this:

JavaScript

but this returns an error: TypeError: cannot perform reduce with flexible type

How can I get descriptive statistics of the created NumPy array?

Advertisement

Answer

This is not a pretty solution, but it gets the job done. The problem is that by specifying multiple dtypes, you are essentially making a 1D-array of tuples (actually np.void), which cannot be described by stats as it includes multiple different types, incl. strings.

This could be resolved by either reading it in two rounds, or using pandas with read_csv.

If you decide to stick to numpy:

JavaScript

Note that in this example the final array has dtype as float, not int, but can easily (if necessary) be converted to int using arr.astype(int)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement