๋ฌธ1) titanic ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์นด์ด์ ๊ณฑ ๊ฒ์ ํ์์ค.
<๋จ๊ณ1> ์์กด์ฌ๋ถ(survived), ์ฌํ์ ์ง์(pclass) ๋ณ์๋ฅผ ์ด์ฉํ์ฌ ๊ต์ฐจ๋ถํ ํ ์์ฑ
<๋จ๊ณ2> ์นด์ด์ ๊ณฑ ๊ฒ์ ํต๊ณ๋, ์ ์ํ๋ฅ , ์์ ๋, ๊ธฐ๋๊ฐ ์ถ๋ ฅ
<๋จ๊ณ3> ๊ฐ์ค๊ฒ์ ๊ฒฐ๊ณผ ํด์ค
import seaborn as sn
import pandas as pd
from scipy import stats # ํ๋ฅ ๋ถํฌ ๊ฒ์
titanic dataset load
titanic = sn.load_dataset('titanic')
print(titanic.info())
1. ๊ต์ฐจ๋ถํ ํ
tab = pd.crosstab(index=titanic.survived,
columns=titanic.pclass)
print(tab)
pclass 1 2 3
survived
0 80 97 372
1 136 87 119
2. ์นด์ด์ ๊ณฑ ๊ฒ์ ํต๊ณ๋, ์ ์ํ๋ฅ , ์์ ๋, ๊ธฐ๋๊ฐ
chi2, pvalue, df, evalue = stats.chi2_contingency(observed= tab) # ์ด์ chi-square ๊ฒ์
print('chi2 = %.6f, pvalue = %.8f, d.f = %d'%(chi2, pvalue, df))
# chi2 = 102.888989, pvalue = 0.00000000, d.f = 2
pvalue # 4.549251711298793e-23
3. ๊ฐ์ค๊ฒ์ ํด์ค
๋งค์ฐ ์ ์๋ฏธํ ์์ค์์ ์์กด์ฌ๋ถ์ ์ฌํ์ ์ง์ ๊ฐ์ ๊ด๋ จ์ฑ์ด ์๋ค.
๋ฌธ2) winequality-both.csv ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ฒ๋ฆฌํ์์ค.
<์กฐ๊ฑด1> quality, type ์นผ๋ผ์ผ๋ก ๊ต์ฐจ๋ถํ ํ ์์ฑ
<์กฐ๊ฑด2> ๊ต์ฐจ๋ถํ ํ๋ฅผ ๋์์ผ๋ก white ์์ธ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
<์กฐ๊ฑด3> red ์์ธ๊ณผ white ์์ธ์ quality์ ๋ํ ๋ ์ง๋จ ํ๊ท ๊ฒ์ -> ๊ฐ ์ง๋จ ํ๊ท ํต๊ณ๋ ์ถ๋ ฅ
<์กฐ๊ฑด4> alcohol ์นผ๋ผ๊ณผ ๋ค๋ฅธ ์นผ๋ผ ๊ฐ์ ์๊ด๊ณ์ ์ถ๋ ฅ
import pandas as pd
import os
from scipy import stats
os.chdir('c:/itwill/4_python-ii/data')
wine = pd.read_csv('winequality-both.csv')
print(wine.info())
<์กฐ๊ฑด1> quality, type ์นผ๋ผ์ผ๋ก ๊ต์ฐจ๋ถํ ํ ์์ฑ
wine_tab = pd.crosstab(index=wine['quality'],
columns=wine['type'])
print(wine_tab)
type red white
quality
3 10 20
4 53 163
5 681 1457
6 638 2198
7 199 880
8 18 175
9 0 5
<์กฐ๊ฑด2> ๊ต์ฐจ๋ถํ ํ๋ฅผ ๋์์ผ๋ก white ์์ธ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
wine_tab_sort = wine_tab.sort_values('white', ascending=False)
print(wine_tab_sort)
type red white
quality
6 638 2198
5 681 1457
7 199 880
8 18 175
4 53 163
3 10 20
9 0 5
<์กฐ๊ฑด3> red ์์ธ๊ณผ white ์์ธ์ quality์ ๋ํ ๋ ์ง๋จ ํ๊ท ๊ฒ์
red_wine = wine.loc[wine['type']=='red', 'quality']
white_wine = wine.loc[wine['type']=='white', 'quality']
two_sample = stats.ttest_ind(red_wine, white_wine)
print(two_sample)
print('t๊ฒ์ ํต๊ณ๋ = %.3f, pvalue = %.5f'%(two_sample))
* t๊ฒ์ ํต๊ณ๋ = -9.686, pvalue = 0.00000 < 0.05
<์กฐ๊ฑด4> alcohol ์นผ๋ผ๊ณผ ๋ค๋ฅธ ์นผ๋ผ ๊ฐ์ ์๊ด๊ณ์ ์ถ๋ ฅ
corr = wine.corr()
print(corr)
print(corr['alcohol'])
๋ฌธ3) score ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋จ์์ ํํ๊ท๋ชจ๋ธ์ ์ด์ฉํ์ฌ ๊ฐ์ค๊ฒ์ ์ผ๋ก ์ํํ์์ค.
๊ท๋ฌด๊ฐ์ค : academy๋ score์ ์ํฅ์ ๋ฏธ์น์ง ์๋๋ค.
๋๋ฆฝ๊ฐ์ค : academy๋ score์ ์ํฅ์ ๋ฏธ์น๋ค.
<์กฐ๊ฑด1> y๋ณ์ : score, x๋ณ์ : academy
<์กฐ๊ฑด2> ํ๊ท๋ชจ๋ธ ์์ฑ๊ณผ ๊ฒฐ๊ณผํ์ธ(ํ๊ท๊ณ์, ์ค๋ช ๋ ฅ, pvalue, ํ์ค์ค์ฐจ)
<์กฐ๊ฑด3> ํ๊ท์ ์ ์ฉ ์๊ฐํ
from scipy import stats
import pandas as pd
import matplotlib.pyplot as plt # ํ๊ท๋ชจ๋ธ ๊ด๋ จ ์๊ฐํ
dataset ๊ฐ์ ธ์ค๊ธฐ
score = pd.read_csv(r'c:/itwill/4_python-2/data/score_iq.csv')
print(score.info())
print(score.head())
1. ๋ณ์ ์ ํ
x = score.academy # ๋
๋ฆฝ๋ณ์
y = score.score # ์ข
์๋ณ์
2. ๋จ์ ์ ํํ๊ท๋ถ์(stats)
model = stats.linregress(x, y)
print(model)
print('x ๊ธฐ์ธ๊ธฐ : ', model.slope)
print('y ์ ํธ :', model.intercept)
print('์ค๋ช
๋ ฅ : ', model.rvalue)
print('p๊ฐ : ', model.pvalue) # F๊ฒ์ ํต๊ณ๋
print('x ํ์ค์ค์ฐจ :' , model.stderr)
3. ํ๊ท์ ์๊ฐํ
a = model.slope
b = model.intercept
y_pred = (x*a) + b # ์์ธก์น
์ฐ์ ๋
plt.plot(score['academy'], score['score'], 'b.') # ํ๋์
plt.plot(score['academy'], y_pred, 'r.-') # ๋นจ๊ฐ์
plt.title('regression plotting') # ์ ๋ชฉ
plt.legend(['x y scatter', 'regression line']) # ๋ฒ๋ก
plt.show()
๋ฌธ4) irsi.csv ๋ฐ์ดํฐ์ ์ ์ด์ฉํ์ฌ ๋ค์ค์ ํํ๊ท๋ชจ๋ธ์ ์์ฑํ์์ค.
<์กฐ๊ฑด1> ์นผ๋ผ๋ช ์ ํฌํจ๋ '.' ์ '_'๋ก ์์
iris.columns = iris.columns.str.replace('.', '_')
<์กฐ๊ฑด2> model์ formula ๊ตฌ์ฑ
y๋ณ์ : 1๋ฒ์งธ ์นผ๋ผ, x๋ณ์ : 2 ~ 3๋ฒ์งธ ์นผ๋ผ
<์กฐ๊ฑด3> ํ๊ท๊ณ์ ํ์ธ
<์กฐ๊ฑด4> ํ๊ท๋ชจ๋ธ ๊ฒฐ๊ณผ ํ์ธ ๋ฐ ํด์ : summary()ํจ์ ์ด์ฉ
import pandas as pd
import statsmodels.formula.api as sm # ๋ค์คํ๊ท๋ชจ๋ธ
dataset ๊ฐ์ ธ์ค๊ธฐ
iris = pd.read_csv('c:/itwill/4_python-ii/data/iris.csv')
print(iris.head())
1. iris ์นผ๋ผ๋ช
์์
iris.columns = iris.columns.str.replace('.', '_')
print(iris.info())
2. formula ๊ตฌ์ฑ ๋ฐ ๋ค์คํ๊ท๋ชจ๋ธ ์์ฑ
iris_model = sm.ols(formula="Sepal_Length ~ Sepal_Width+Petal_Length", data=iris).fit()
3. ํ๊ท๊ณ์ ํ์ธ
print(iris_model.params)
Intercept 2.249140
Sepal_Width 0.595525
Petal_Length 0.471920
4. ํ๊ท๋ชจ๋ธ ๊ฒฐ๊ณผ ํ์ธ
print(iris_model.summary())
OLS Regression Results
==============================================================================
Dep. Variable: Sepal_Length R-squared: 0.840
Model: OLS Adj. R-squared: 0.838
Method: Least Squares F-statistic: 386.4
Date: Tue, 30 Nov 2021 Prob (F-statistic): 2.93e-59
Time: 21:02:08 Log-Likelihood: -46.513
No. Observations: 150 AIC: 99.03
Df Residuals: 147 BIC: 108.1
Df Model: 2
Covariance Type: nonrobust
================================================================================
coef std err t P>|t| [0.025 0.975]
--------------------------------------------------------------------------------
Intercept 2.2491 0.248 9.070 0.000 1.759 2.739
Sepal_Width 0.5955 0.069 8.590 0.000 0.459 0.733
Petal_Length 0.4719 0.017 27.569 0.000 0.438 0.506
==============================================================================
Omnibus: 0.164 Durbin-Watson: 2.021
Prob(Omnibus): 0.921 Jarque-Bera (JB): 0.319
Skew: -0.044 Prob(JB): 0.853
Kurtosis: 2.792 Cond. No. 48.3
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.