자격증/빅데이터분석기사

[빅분기] 제 3유형 연습

K_Hyul 2024. 7. 4. 14:00
728x90

제 3유형 같은 경우 제일 많이 나온 것은 카이제곱, pvalue, t분포 등 통계량에 대한 것이다.

또한 회귀 문제로 회귀계수, pvalue값, 오즈비, 잔차 이탈  등을 구한다.

 

 

- 샤피로 검정을 통해 표본이 정규 분포에서 추출 된 것인지 확인 유의 수준이 5%일 때 pvalue값을 확인

from scipy.stats import shapiro

print(shapiro(df))

 

- 카이제곱 독립성 검정 : 유의 수준이 5%일 때 pvalue값을 확인

from scipy.stats import chi2_contingency

# 데이터 배열 생성 (빈도로 변환)
data = np.array([[do_male, dont_do_male], [do_female, dont_do_female]])

# 카이제곱 검정 수행
chi2_stat, p_val, dof, expected = chi2_contingency(data)

 

 

 

- 회귀 분석 OLS 

import statsmodels.api as sm

X = sm.add_constant(df[['age', 'Cholesterol']]) 
model = sm.OLS(df['weight'], X)
ans = model.fit()

print(ans.params['age'])

pred = ans.predict([1,55,72.6])
print(pred)

 

- zscore : 데이터가 평균으로부터 얼마나 떨어져 있는가

from scipy.stats import zscore

ans = df['age'].mean()
print(ans)

 

- 상관계수 찾기

# 선형관계 가장 큰 변수 찾아 상관계수를 구하여라
ans = df.corr()['Target'].abs().sort_values(ascending=False).values[1]
print(ans)

 

- 오즈비 구하기

import statsmodels.api as sm
import numpy as np
y = df['target']
x = df.drop(columns=['target'])

model = sm.Logit(y,x).fit()

odds_ratios = np.exp(model.params['age'])
odds_ratios

 

- 로지스틱 회귀를 했을 때 잔차 이탈도

import statsmodels.api as sm
y = df['target']
x = df.drop(columns=['target'])

model2 = sm.GLM(y, x, family=sm.families.Binomial()).fit()

# 잔차 이탈도(residual deviance) 계산
residual_deviance = model2.deviance
print(residual_deviance)

 

 

728x90

'자격증 > 빅데이터분석기사' 카테고리의 다른 글

[빅분기] 제 1유형 연습  (0) 2024.07.04
[빅분기] 제 2유형 연습  (0) 2024.07.04
(pandas) file 다루기  (0) 2023.12.27
(pandas) csv 파일 불러오기  (1) 2023.12.27