파이썬

파이썬 기초 통계분석

박범준2 2020. 10. 20. 14:04
반응형

파이썬에서 기본적인 추리 통계분석 패키지를 사용하는 방법입니다.

 

 

통계연습용
In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container {width:90% !important;}</style>"))
In [2]:
import pandas as pd
import numpy as np
from scipy import stats 

추리통계

t 검증

독립표본 t 검증

In [3]:
sample1=np.random.randint(1000,size=50)
sample2=np.random.randint(1000,size=50)

print("평균")
print(np.mean(sample1))
print(np.mean(sample2))

print("표준편차")
print(np.std(sample1))
print(np.std(sample2))

print("등분산검증")
print(stats.levene(sample1, sample2))

print("t검정")
stats.ttest_ind(sample1,sample2,equal_var=False)
평균
530.72
435.9
표준편차
281.89622487717
262.5549275865909
등분산검증
LeveneResult(statistic=0.3078982355875669, pvalue=0.580237067092227)
t검정
Out[3]:
Ttest_indResult(statistic=1.7229798724683143, pvalue=0.0880620772784276)

교차분석

일원 교차분석

In [4]:
obs=[324,78,261]
exp=[371,80,212]

stats.chisquare(obs,exp)
Out[4]:
Power_divergenceResult(statistic=17.329649595687332, pvalue=0.00017254977751013492)

이원교차분석

In [5]:
female=[269,83,215]
male=[155,57,181]

chi2, p, dof, expected= stats.chi2_contingency([female,male])

msg="Test Statistic: {:.2f}\np-value: {:.3f}\nDegree of Freedom: {}"
print(msg.format(chi2,p,dof))
print(expected)
Test Statistic: 7.09
p-value: 0.029
Degree of Freedom: 2
[[250.425   82.6875 233.8875]
 [173.575   57.3125 162.1125]]
In [37]:
"I eat {} apples {}".format(1,2)
Out[37]:
'I eat 1 apples 2'

상관관계 분석

In [32]:
x=[1,2,3,4,5,6,7,8,9,10]
y=[3,6,9,10,12,15,16,17,18,20]
z=[1,1,1,1,1,2,1,1,1,1]
w=[-1,-2,-3,-4,-5,-6,-7,-8,-9,-10]
In [33]:
df=pd.DataFrame([x,y,z,w]).T
df.head(3)
Out[33]:
0 1 2 3
0 1 3 1 -1
1 2 6 1 -2
2 3 9 1 -3
In [34]:
corr=df.corr(method="pearson")
print(corr)
          0         1         2         3
0  1.000000  0.986712  0.058026 -1.000000
1  0.986712  1.000000  0.152167 -0.986712
2  0.058026  0.152167  1.000000 -0.058026
3 -1.000000 -0.986712 -0.058026  1.000000
In [ ]:
 
반응형