Descriptive Statisctics with Scipy.stats¶
In [2]:
import scipy as sp
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
In [3]:
s = sp.randn(100) # 100 randomn numbers from gaussian distribution
In [6]:
s.mean() # mean
Out[6]:
In [5]:
s.min() # min
Out[5]:
In [7]:
s.max() # max
Out[7]:
In [8]:
s.var() # variance
Out[8]:
In [9]:
s.std() # standard vdeviation
Out[9]:
In [11]:
sp.median(s) # median
Out[11]:
In [12]:
from scipy import stats
In [13]:
n, min_max, mean, var, skew, kurt = stats.describe(s)
In [14]:
n # number of elements
Out[14]:
In [15]:
min_max[0] # minimum
Out[15]:
In [16]:
min_max[1] # maximum
Out[16]:
In [17]:
mean # mean
Out[17]:
In [18]:
var # variance
Out[18]:
In [19]:
skew
Out[19]:
In [20]:
kurt # kurtosis
Out[20]:
Let's generate a gaussian distribution with mean = 4.5 and standard deviation = 1.5¶
In [21]:
n = stats.norm(loc=4.5, scale=1.5)
In [22]:
n.rvs() # draw a random number from it
Out[22]:
In [23]:
stats.norm.rvs(loc=4.5, scale=1.5)
Out[23]:
In [25]:
stats.norm.pdf(4, loc=4.5, scale=1.5) # 0 is the given value
Out[25]:
In [27]:
stats.norm.pdf([2, 4, 4.5], loc=4.5, scale=1.5) # you can get pdf for a list
Out[27]:
In [28]:
tries = range(11)
stats.binom.pmf(tries, 10, 0.5) # discrete tries for PMF
Out[28]:
In [30]:
def binom_pmf(n=4, p=0.5):
# There are n+1 possible number of "successes": 0 to n.
x = range(n+1)
y = stats.binom.pmf(x, n, p)
plt.plot(x,y,"o", color="black")
plt.axis([-(max(x)-min(x))*0.05, max(x)*1.05, -0.01, max(y)*1.10])
plt.xticks(x)
plt.title("Binomial distribution PMF for tries = {0} & p ={1}".format(
n,p))
plt.xlabel("Variate")
plt.ylabel("Probability")
plt.show()
In [31]:
binom_pmf()
In [32]:
stats.norm.cdf(0.0, loc=0.0 ,scale=1.0 )
Out[32]:
In [33]:
stats.norm.ppf(0.5, loc=0.2, scale=0.5)
Out[33]:
In [34]:
stats.norm.sf(0.0, 1.0, scale=1.0)
Out[34]:
In [36]:
stats.norm.isf(0.5, loc=0.0, scale=1.0)
Out[36]: