DAY49. Python Statis Scipy (์นด์ด์ ๊ณฑ๊ฒ์ , T๊ฒ์ , ๊ณต๋ถ์ฐ, ํ๊ท๋ถ์)
statistics
statistics ๋ชจ๋
๊ธฐ์ ํต๊ณ : ๋ํ๊ฐ, ์ฐํฌ๋, ์๋/์ฒจ๋ ๋ฑ
import statistics as st #๊ธฐ์ ํต๊ณ ์ ๊ณต
import pandas as pd #csv file read
๊ธฐ์ ํต๊ณ : ๋น์จ์ฒ๋ or ๋ฑ๊ฐ์ฒ๋
dataset = pd.read_csv(r'C:\ITWILL\4_Python-II\data\descriptive.csv')
dataset.info()
x = dataset['cost'] #๊ตฌ๋งค๋น์ฉ
x
1. ๋ํ๊ฐ
print('ํ๊ท =', st.mean(x))
print('์ค์์ =', st.median(x))
print('์ต๋น์ =', st.mode(x)) #์ต๋น์ = 6.0
x.value_counts() #6.0 21
2. ์ฐํฌ๋ : ๋ถ์ฐ, ํ์คํธ์ฐจ, ์ฌ๋ถ์์
var = st.variance(x)
var # 6.0 21
std = st.stdev(x)
std # 1.1421532501476073
st.quantiles(x)
# [4.425000000000001, 5.4, 6.2]
x.describe()
25% 4.475000
50% 5.400000
75% 6.200000
import scipy.stats as sts
3. ์๋/์ฒจ๋
์๋ = 0 : ์ ๊ท๋ถํฌ
์๋ > 0 : ์ผ์ชฝ ๊ธฐ์ธ์ด์ง
์๋ < 0 : ์ค๋ฅธ์ชฝ ๊ธฐ์ธ์ด์ง
sts.skew(x) # -0.1531779106237012
์ฒจ๋ = 0 or 3
sts.kurtosis(x, fisher=True) #fisher = 0
sts.kurtosis(x, fisher=False) #pearson = 3
์ ๊ท๋ถํฌ = 0 or 3
์ฒจ๋ > ์ ๊ท๋ถํฌ : ์๋ก ๋พฐ์กฑํจ
์ฒจ๋ < ์ ๊ท๋ถํฌ : ์๋งํจ
ํ์คํ ๊ทธ๋จ + ๋ฐ๋๋ถํฌ๊ณก์
import seaborn as sn
sn.displot(data=x, kde=True)
chisquare_test
ํ๋ฅ ๋ณ์์ ์ ํฉ์ฑ ๊ฒ์ - ์ผ์
๋ ์ง๋จ๋ณ์ ๊ฐ์ ๋
๋ฆฝ์ฑ ๊ฒ์ - ์ด์
from scipy import stats #ํ๋ฅ ๋ถํฌ+๊ฐ์ค๊ฒ์
import numpy as np #์์ ๊ณ์ฐ
import pandas as pd #csv file read
1. ์ผ์ chi-square(1๊ฐ ๋ณ์) : ์ ํฉ์ฑ ๊ฒ์
๊ท๋ฌด๊ฐ์ค : ๊ด์ธก์น์ ๊ธฐ๋์น๋ ์ฐจ์ด๊ฐ ์๋ค.
๋๋ฆฝ๊ฐ์ค : ๊ด์ธก์น์ ๊ธฐ๋์น๋ ์ฐจ์ด๊ฐ ์๋ค.
real_data = [4, 6, 17, 16, 8, 9] # ๊ด์ธก์น
exp_data = [10,10,10,10,10,10] # ๊ธฐ๋์น
chis = stats.chisquare(real_data, exp_data)
print(chis)
(statistic=14.200000000000001, pvalue=0.014387678176921308)
print('๊ฒ์ ํต๊ณ๋ =', chis[0])
print('์ ์ํ๋ฅ =', chis[1])
์ ์ํ๋ฅ = 0.014387678176921308
real_arr = np.array(real_data)
exp_arr = np.array(exp_data)
statistic = sum((real_arr - exp_arr)**2 / exp_arr)
statistic # 14.200000000000001
[ํด์ค] ์ ์๋ฏธํ ์์ค์์ ์ฐจ์ด๊ฐ ์๋ค.
2. ์ด์ chi-square(2๊ฐ ๋ณ์) : ๊ต์ฐจ๋ถํ ํ
๊ท๋ฌด๊ฐ์ค : ๊ต์ก์์ค๊ณผ ํก์ฐ์จ ๊ฐ์ ๊ด๋ จ์ฑ์ด ์๋ค.(๊ธฐ๊ฐ)
๋๋ฆฝ๊ฐ์ค : ๊ต์ก์์ค๊ณผ ํก์ฐ์จ ๊ฐ์ ๊ด๋ จ์ฑ์ด ์๋ค.(์ฑํ)
smoke = pd.read_csv(r'C:\ITWILL\4_Python-II\data\smoke.csv')
smoke.info()
0 education 355 non-null int64
1 smoking 355 non-null int64
smoke.education.value_counts()
1 211
3 92
2 52
smoke.smoking.value_counts()
2 141
1 116
3 98
๋จ๊ณ1 : ๋ณ์ ์ ํ
education = smoke.education
smoking = smoke.smoking
๋จ๊ณ2 : ๊ต์ฐจ๋ถํ ํ : ๊ด์ธก์น
tab = pd.crosstab(index=education, columns=smoking, margins=True)
tab
smoking 1 2 3 All
education
1 51 92 68 211
2 22 21 9 52
3 43 28 21 92
All 116 141 98 355
๋จ๊ณ3 : ์นด์ด์ ๊ณฑ ๊ฒ์ : ๊ต์ฐจ๋ถํ ํ ์ด์ฉ
chi2, pvalue, df, evalue = stats.chi2_contingency(observed=tab)
print('๊ฒ์ ํต๊ณ๋ : %.6f, ์ ์ํ๋ฅ : %.6f, ์์ ๋ : %d'%(chi2, pvalue,df))
๊ฒ์ ํต๊ณ๋ : 18.910916, ์ ์ํ๋ฅ : 0.000818, ์์ ๋ : 4
๋จ๊ณ4 : ๊ธฐ๋์น
print(evalue)
[[68.94647887 83.8056338 58.24788732]
[16.9915493 20.65352113 14.35492958]
[30.06197183 36.54084507 25.3971831 ]]
[ํด์] ์ ์๋ฏธํ ์์ค์์ ๊ต์ก์์ค๊ณผ ํก์ฐ์จ ๊ฐ์ ๊ด๋ จ์ฑ์ด ์๋ค.
51 ๊ธฐ์ค ๊ธฐ๋์น = (ํํฉ * ์ดํฉ) / ์ ์ฒดํฉ
e11 = (211 * 116) / 355
e11 # 68.94647887323944
51 ๊ธฐ์ค ๊ธฐ๋๋น์จ = sum((๊ด์ธก์น-๊ธฐ๋์น)**2) / ๊ธฐ๋์น
e11_ratio = (51.0-e11)**2 / e11
e11_ratio # 4.671393074906378
t๊ฒ์
t ๋ถํฌ์ ๋ํ ๊ฐ์ค๊ฒ์
t๊ฒ์ : ๋ชจ์ง๋จ์ด ์ ๊ท๋ถํฌ์ด๊ณ , ๋ชจ๋ถ์ฐ์ด ์๋ ค์ง์ง ์์ ๊ฒฝ์ฐ
z๊ฒ์ : ๋ชจ์ง๋จ์ด ์ ๊ท๋ถํฌ์ด๊ณ , ๋ชจ๋ถ์ฐ์ด ์๋ ค์ง ๊ฒฝ์ฐ
1. ํ ์ง๋จ ํ๊ท ๊ฒ์ : ๋ชจํ๊ท ๊ฒ์
2. ๋ ์ง๋จ ํ๊ท ๊ฒ์
3. ๋์ ๋ ์ง๋จ
from scipy import stats #t-test
import numpy as np #sampling
import pandas as pd #csv file read
1. ํ ์ง๋จ ํ๊ท ๊ฒ์ : ๋จ์ ํ๊ท ํค(๋ชจํ๊ท ) : 175.5cm
sample_data = np.random.uniform(172,179, size=29)
print(sample_data)
๊ธฐ์ ํต๊ณ
print('ํ๊ท ํค =', sample_data.mean())
๋จ์ผ์ง๋จ ํ๊ท ์ฐจ์ด ๊ฒ์
one_group_test = stats.ttest_1samp(sample_data, 175.5)
print('t๊ฒ์ ํต๊ณ๋ = %.3f, pvalue = %.5f'%(one_group_test))
t๊ฒ์ ํต๊ณ๋ = 0.381, pvalue = 0.70619
[ํด์ค] ํ๋ณธ์ ํ๊ท ์ ๋ชจํ๊ท ๊ณผ ์ฐจ์ด๊ฐ ์๋ค.
2. ๋ ์ง๋จ ํ๊ท ๊ฒ์ : ๋จ์ฌ ํ๊ท ์ ์ ์ฐจ์ด ๊ฒ์
female_score = np.random.uniform(50, 100, size=30) # ์ฌ์ฑ
male_score = np.random.uniform(45, 95, size=30) # ๋จ์ฑ
two_sample = stats.ttest_ind(female_score, male_score)
print(two_sample)
print('๋ ์ง๋จ ํ๊ท ์ฐจ์ด ๊ฒ์ = %.3f, pvalue = %.3f'%(two_sample))
๋ ์ง๋จ ํ๊ท ์ฐจ์ด ๊ฒ์ = 0.321, pvalue = 0.750
[ํด์ค] ๋จ์ฌ ํ๊ท ์ ์ ์ฐจ์ด๊ฐ ์๋ค.
file ์๋ฃ ์ด์ฉ : ๊ต์ก๋ฐฉ๋ฒ์ ๋ฐ๋ฅธ ์ค๊ธฐ์ ์์ ํ๊ท ์ฐจ์ด ๊ฒ์
sample = pd.read_csv('c:/itwill/4_python-ii/data/two_sample.csv')
print(sample.info())
two_df = sample[['method', 'score']]
print(two_df)
two_df.method.value_counts()
1 120
2 120
๊ต์ก๋ฐฉ๋ฒ ๊ธฐ์ค subset
method1 = two_df[two_df.method==1]
method2 = two_df[two_df.method==2]
method1.info()
0 method 120 non-null int64
1 score 88 non-null float64
score ์นผ๋ผ ์ถ์ถ
score1 = method1.score
score2 = method2.score
์ ์ฒ๋ฆฌ : ๊ฒฐ์ธก์น ์ฒ๋ฆฌ
score1 = score1.fillna(0)
score2 = score2.fillna(0)
๋ ์ง๋จ ํ๊ท ์ฐจ์ด ๊ฒ์
two_sample = stats.ttest_ind(score1, score2)
print(two_sample)
Ttest_indResult(statistic=-0.7833843755616479, pvalue=0.4341802874737909)
[ํด์ค] ๋ ๊ต์ก๋ฐฉ๋ฒ์ ๋ฐ๋ฅธ ํ๊ท ์ ์ฐจ์ด๊ฐ ์๋ค.
3. ๋์ ๋ ์ง๋จ : ๋ณต์ฉ์ 65 -> ๋ณต์ฉํ 60 ๋ชธ๋ฌด๊ฒ ๋ณํ
before = np.random.randint(60, 65, size=30)
after = np.random.randint(59, 64, size=30)
paired_sample = stats.ttest_rel(before, after)
print(paired_sample)
print('t๊ฒ์ ํต๊ณ๋ = %.5f, pvalue = %.5f'%paired_sample)
๊ณต๋ถ์ฐ vs ์๊ด๊ณ์
๊ณตํต์ : ๋ณ์(๋น์จ,๋ฑ๊ฐ ์ฒ๋) ๊ฐ์ ์๊ด์ฑ ๋ถ์
1. ๊ณต๋ถ์ฐ : ๋ ํ๋ฅ ๋ณ์ ๊ฐ์ ๋ถ์ฐ(ํ๊ท ์์ ํผ์ง ์ ๋)๋ฅผ ๋ํ๋ด๋ ํต๊ณ
ํ๋ฅ ๋ณ์ : X, Y
์ : Cov(X,Y) = sum( (X-xmu) * (Y-ymu) ) / n
Cov(X, Y) > 0 : X๊ฐ ์ฆ๊ฐํ ๋ Y๋ ์ฆ๊ฐ
Cov(X, Y) < 0 : X๊ฐ ์ฆ๊ฐํ ๋ Y๋ ๊ฐ์
Cov(X, Y) = 0 : ๋ ๋ณ์๋ ์ ํ๊ด๊ณ ์๋(์๋ก ๋
๋ฆฝ์ ๊ด๊ณ)
๋ฌธ์ ์ : ๊ฐ์ด ํฐ ๋ณ์์ ์ํฅ์ ๋ฐ๋๋ค.(๊ฐ ํฐ ๋ณ์๊ฐ ์๊ด์ฑ ๋์)
2. ์๊ด๊ณ์ : ๊ณต๋ถ์ฐ์ ๊ฐ๊ฐ์ ํ์คํธ์ฐจ๋ก ๋๋์ด ์ ๊ทํํ ํต๊ณ
๊ณต๋ถ์ฐ ๋ฌธ์ ์ ํด๊ฒฐ
๋ถํธ๋ ๊ณต๋ถ์ฐ๊ณผ ๋์ผ, ๊ฐ์ ์ ๋๊ฐ 1์ ๋์ง ์์(-1 ~ 1)
์ : Corr(X, Y) = Cov(X,Y) / std(X) * std(Y)
import pandas as pd
score_iq = pd.read_csv(r'c:/itwill/4_python-ii/data/score_iq.csv')
print(score_iq)
1. ํผ์ด์จ ์๊ด๊ณ์ ํ๋ ฌ
corr = score_iq.corr(method='pearson')
print(corr)
sid score iq academy game tv
sid 1.000000 -0.014399 -0.007048 -0.004398 0.018806 0.024565
score -0.014399 1.000000 0.882220 0.896265 -0.298193 -0.819752
iq -0.007048 0.882220 1.000000 0.671783 -0.031516 -0.585033
academy -0.004398 0.896265 0.671783 1.000000 -0.351315 -0.948551
game 0.018806 -0.298193 -0.031516 -0.351315 1.000000 0.239217
tv 0.024565 -0.819752 -0.585033 -0.948551 0.239217 1.000000
2. ๊ณต๋ถ์ฐ ํ๋ ฌ
cov = score_iq.cov()
print(cov)
sid score iq academy game tv
sid 1887.500000 -4.100671 -2.718121 -0.231544 1.208054 1.432886
score -4.100671 42.968412 51.337539 7.119911 -2.890201 -7.214586
iq -2.718121 51.337539 78.807338 7.227293 -0.413691 -6.972975
academy -0.231544 7.119911 7.227293 1.468680 -0.629530 -1.543400
game 1.208054 -2.890201 -0.413691 -0.629530 2.186309 0.474899
tv 1.432886 -7.214586 -6.972975 -1.543400 0.474899 1.802640
3. ๊ณต๋ถ์ฐ vs ์๊ด๊ณ์ ์ ์ ์ฉ
1) ๊ณต๋ถ์ฐ : Cov(X, Y) = sum( (X-xmu) * (Y-ymu) ) / n-1
X = score_iq['score']
Y = score_iq['iq']
ํ๋ณธํ๊ท
muX = X.mean()
muY = Y.mean()
ํ๋ณธ์ ๊ณต๋ถ์ฐ
Cov = sum((X - muX) * (Y - muY)) / (len(X)-1)
print('Cov =', Cov)
2) ์๊ด๊ณ์ : Corr(X, Y) = Cov(X,Y) / std(X) * std(Y)
stdX = X.std()
stdY = Y.std()
Corr = Cov / (stdX * stdY)
print('Corr =', Corr)
regression
scipy ํจํค์ง ์ด์ฉ
1. ๋จ์์ ํํ๊ท๋ถ์
2. ๋ค์ค์ ํํ๊ท๋ถ์
from scipy import stats #ํ๊ท๋ถ์
import pandas as pd #csv file read
import matplotlib.pyplot as plt
score_iq = pd.read_csv(r'C:\ITWILL\4_Python-2\data\score_iq.csv')
score_iq.info()
1. ๋จ์์ ํํ๊ท๋ถ์ x -> y
1) ๋ณ์ ์ ํ
x = score_iq['iq'] # score_iq.iq
y = score_iq['score']
2) model ์์ฑ
model = stats.linregress(x, y) # iq -> score
print(model)
LinregressResult
(slope=0.6514309527270075, : x๊ธฐ์ธ๊ธฐ(ํ๊ท๊ณ์)
intercept=-2.8564471221974657, : y์ ํธ
rvalue=0.8822203446134699, : R^2 - ์ค๋ช
๋ ฅ(๊ฒฐ์ ๊ณ์)
pvalue=2.8476895206683644e-50, : F๊ฒ์ - ์ ์์ฑ๊ฒ์
stderr=0.028577934409305443, : ํ์ค์ค์ฐจ
intercept_stderr=3.546211918048538)
a = model.slope #๊ธฐ์ธ๊ธฐ
b = model.intercept #์ ํธ
score_iq.head()
ํ๊ท๋ฐฉ์ ์ -> y์์ธก์น
X = 140
Y = 90
y_pred = X*a + b
print(y_pred) # 88.34388625958358
err = Y - y_pred
print('error = ', err) # error = 1.6561137404164157
์ ์ฒด ๊ด์ธก์น ๋์
len(x) # 150
y_pred = x*a + b
print(y_pred)
๊ด์ธก์น vs ์์ธก์น
y.mean() # 77.77333333333333
y_pred.mean() # 77.77333333333334
2. ํ๊ท๋ชจ๋ธ ์๊ฐํ
์ฐ์ ๋
plt.plot(score_iq['iq'], score_iq['score'], 'b.')
ํ๊ท์
plt.plot(score_iq['iq'], y_pred, 'r-')
plt.title('line regression')
plt.legend(['x y scatter', 'line regression'])
plt.show()
3. ๋ค์ค์ ํํ๊ท๋ถ์ : formula ํ์(y ~ x1 + x2, ...)
๋ณ์๋ช
: ์ (.), ๊ณต๋ฐฑ -> '_' ๊ต์ฒด
from statsmodels.formula.api import ols
์๊ด๊ณ์ ํ๋ ฌ
corr = score_iq.corr()
corr['score'] # x1 = iq, x2 = academy, x3 = tv
obj = ols(formula='score ~ iq + academy + tv', data=score_iq)
model = obj.fit()
model #object info
ํ๊ท๋ถ์ ๊ฒฐ๊ณผ ์ ๊ณต
print(model.summary()) #R : summary(model)
OLS Regression Results
==============================================================================
Dep. Variable: score R-squared: 0.946
Model: OLS Adj. R-squared: 0.945
Method: Least Squares F-statistic: 860.1
Date: Fri, 12 Nov 2021 Prob (F-statistic): 1.50e-92
Time: 11:18:16 Log-Likelihood: -274.84
No. Observations: 150 AIC: 557.7
Df Residuals: 146 BIC: 569.7
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 24.7223 2.332 10.602 0.000 20.114 29.331
iq 0.3742 0.020 19.109 0.000 0.335 0.413
academy 3.2088 0.367 8.733 0.000 2.483 3.935
tv 0.1926 0.303 0.636 0.526 -0.406 0.791
==============================================================================
Omnibus: 36.802 Durbin-Watson: 1.905
Prob(Omnibus): 0.000 Jarque-Bera (JB): 57.833
Skew: 1.252 Prob(JB): 2.77e-13
Kurtosis: 4.728 Cond. No. 2.32e+03
==============================================================================
'''
# Adj. R-squared: 0.945 : ๋ชจ๋ธ ์ค๋ช
๋ ฅ
# Prob (F-statistic): 1.50e-92 : ๋ชจ๋ธ ์ ์์ฑ ๊ฒ์ (<0.05)
# coef std err t P>|t| : x๋ณ์ ์ ์์ฑ ๊ฒ์
# Durbin-Watson: 1.905 : (1~4) ๋ค์ค๊ณต์ ์ฑ ๋ฌธ์
dir(model)
ํ๊ท๊ณ์ ๋ฐํ
model.params
Intercept 24.722251 - y์ ํธ
iq 0.374196 - x1 ๊ธฐ์ธ๊ธฐ
academy 3.208802 - x2 ๊ธฐ์ธ๊ธฐ
tv 0.192573 - x3 ๊ธฐ์ธ๊ธฐ
y ์ ํฉ์น
model.fittedvalues
ํ๋ ฌ๊ณฑ(@) ์ด์ฉ ํ๊ท๋ฐฉ์ ์
X = score_iq[['iq', 'academy', 'tv']]
X.shape # (150, 3)
import numpy as np
a = np.array([[0.374196],[3.208802],[0.192573]])
a.shape # (3, 1) - ๊ธฐ์ธ๊ธฐ ํ๋ ฌ
b = 24.722251 # ์ ํธ
# y = x1*a1 + x2*a2 + x3*a3 + b
y_fitted = X @ a + b
print(y_fitted)
์ฐจํธ ๋ณด๊ธฐ : y vs y_fitted
y = score_iq['score']
plt.plot(y[:50], label='real value')
plt.plot(y_fitted[:50], label='predicted value')
plt.legend(loc= 'best')
plt.show()