close

How can I get descriptive statistics of a NumPy array?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I get descriptive statistics of a NumPy array in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I get descriptive statistics of a NumPy array?

  1. How can I get descriptive statistics of a NumPy array?

    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.

  2. get descriptive statistics of a NumPy array

    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.

Method 1

import pandas as pd
import numpy as np

df_describe = pd.DataFrame(dataset)
df_describe.describe()

please note that dataset is your np.array to describe.

import pandas as pd
import numpy as np

df_describe = pd.DataFrame('your np.array')
df_describe.describe()

Method 2

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:

import numpy as np
a = np.genfromtxt('sample.txt', delimiter=",",unpack=True,usecols=range(1,9))
s = np.genfromtxt('sample.txt', delimiter=",",unpack=True,usecols=0,dtype='|S1')

from scipy import stats
for arr in a: #do not need the loop at this point, but looks prettier
    print(stats.describe(arr))
#Output per print:
DescribeResult(nobs=6, minmax=(0.34999999999999998, 0.70999999999999996), mean=0.54500000000000004, variance=0.016599999999999997, skewness=-0.3049304880932534, kurtosis=-0.9943046886340534)

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)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read