# Scipy統計函數

1

`rv_continuous`

2

`rv_discrete`

3

`rv_histogram`

## 正態連續隨機變量

``````from scipy.stats import norm
import numpy as np
cdfarr = norm.cdf(np.array([1,-1., 0, 1, 3, 4, -2, 6]))
print(cdfarr)``````

``````array([ 0.84134475, 0.15865525, 0.5 , 0.84134475, 0.9986501 ,
0.99996833, 0.02275013, 1. ])``````

``````from scipy.stats import norm
ppfvar = norm.ppf(0.5)
print(ppfvar)``````

``0.0``

``````from scipy.stats import norm
rvsvar = norm.rvs(size = 5)
print(rvsvar)``````

``[-0.25993892  1.46653546 -0.53932984 -1.22796601  0.06542478]``

## 均勻分佈

``````from scipy.stats import uniform
cvar = uniform.cdf([0, 1, 2, 3, 4, 5], loc = 1, scale = 4)
print(cvar)``````

``array([ 0. , 0. , 0.25, 0.5 , 0.75, 1. ])``

``````from scipy.stats import uniform
cvar = uniform.cdf([0, 1, 2, 3, 4, 5], loc = 1, scale = 4)

print(cvar)``````

``array([ 0. , 0. , 0.25, 0.5 , 0.75, 1. ])``

## 描述性統計

`Min``Max``Mean``Variance`等基本統計數據將NumPy數組作爲輸入並返回相應的結果。 下表描述了`scipy.stats`包中的一些基本統計函數。

1

`describe()`

2

`gmean()`

3

`hmean()`

4

`kurtosis()`

5

`mode()`

6

`skew()`

7

`f_oneway()`

8

`iqr()`

9

`zscore()`

10

`sem()`

``````from scipy import stats
import numpy as np
x = np.array([1,2,3,4,5,6,7,8,9])
print (x.max(),x.min(),x.mean(),x.var())``````

``(9, 1, 5.0, 6.666666666666667)``

T-檢驗

ttest_1samp

``````from scipy import stats
rvs = stats.norm.rvs(loc = 5, scale = 10, size = (50,2))
sta = stats.ttest_1samp(rvs,5.0)
print(sta)``````

``````Ttest_1sampResult(statistic = array([-1.40184894, 2.70158009]),
pvalue = array([ 0.16726344, 0.00945234]))``````

`ttest_ind` - 計算兩個獨立樣本得分的T檢驗。 對於兩個獨立樣本具有相同平均(預期)值的零假設，這是一個雙側檢驗。 該測試假設人口默認具有相同的差異。

``````from scipy import stats
rvs1 = stats.norm.rvs(loc = 5,scale = 10,size = 500)
rvs2 = stats.norm.rvs(loc = 5,scale = 10,size = 500)
print (stats.ttest_ind(rvs1,rvs2))``````

``Ttest_indResult(statistic = -0.67406312233650278, pvalue = 0.50042727502272966)``