빅데이터 분석기사/실기 요약

[빅데이터분석기사/실기] 제2유형. 모의문제

✨️데이터분석가✨️ 2025. 6. 8. 01:26
728x90
728x90

제2유형 모의문제 입니다.

총 3문제이며, 모두 다른 유형의 문제이니 다 풀어보시는 것을 추천드립니다.

 

 

 


문1. 분류 (신용카드를 떠나는 고객을 찾아라)

1) 데이터 불러오기

import pandas as pd

train = pd.read_csv(" ~ ")
test =  pd.read_csv(" ~ ")

 

 

2) 데이터 확인하기

print(train.shape, test.shape)
print(train.head())
print(test.head())
print(train.info())
print(train['Attrition_Flag'].value_counts()) # target 값의 분포
print(train.isnull().sum())
print(test.isnull().sum())

 

 

3) 데이터 전처리

(8101, 21) (2026, 20)
(8101, 21) (2026, 20)

 

 

4) 피처엔지니어링

#  라벨 인코딩 (위 drop 2줄만 제외하고 다시 실행)
from sklearn.preprocessing import LabelEncoder
for col in cols:
    le = LabelEncoder()
    train[col] = le.fit_transform(train[col])
    test[col] = le.transform(test[col])
train[cols].head()
 
# 원핫 인코딩 (위 drop 2줄, 라벨 인코딩 제외하고 다시 실행)
train = pd.get_dummies(train, columns=cols)
test = pd.get_dummies(test, columns=cols)

 

 

5) 검증 데이터 분리

from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(
    train.drop('Attrition_Flag', axis=1), train['Attrition_Flag'], test_size=0.2, random_state=2022
)
X_tr.head()

 

 

6) 랜덤포레스트 분류 모델

array([0, 0, 0, ..., 0, 0, 0])

 

 

7) 평가지표

 

 

 

8) 예측 및 csv 제출

pred = model.predict_proba(test)
submit = pd.DataFrame({
    'CLIENTNUM':test_id,
    'Attrition_Flag':pred[:,1]
})
submit.to_csv('00000.csv', index=False)
pd.read_csv("00000.csv") # 파일 확인

 

 

 


문2. 회귀 (에어비앤비 가격)

1) 데이터 불러오기

import pandas as pd
train = pd.read_csv(" ~ ")
test =  pd.read_csv(" ~ ")

 

 

2) 데이터 확인하기

print(train.shape, test.shape)
print(train.head(3))
print(test.head(3))
print(train.isnull().sum())
print(test.isnull().sum())
print(train['price'].describe()) # 분포 확인
print(train.info()) # 타입 확인

 

 

3) 데이터 전처리

cols = ['name', 'host_name', 'last_review', 'host_id'] # 결측치 컬럼 삭제
print(train.shape)
train = train.drop(cols, axis=1)
test = test.drop(cols, axis=1)
print(train.shape)
train = train.drop('id', axis=1) # 데이터와 관련 없는 id 컬럼 삭제
test_id = test.pop('id')

train['reviews_per_month'] = train['reviews_per_month'].fillna(0) # 결측치 0으로 보정
test['reviews_per_month'] = test['reviews_per_month'].fillna(0)
train.isnull().sum()
(39116, 16)
(39116, 12)
 

 

4) 피처엔지니어링

# 라벨 인코딩 (object 타입을 라벨로 변경)
cols =['neighbourhood_group', 'neighbourhood', 'room_type']
from sklearn.preprocessing import LabelEncoder
for col in cols:
    le = LabelEncoder()
    train[col] = le.fit_transform(train[col])
    test[col] = le.transform(test[col])
train[cols]

 

 

5) 검증 데이터 분리

from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(train.drop('price', axis=1), train['price'], test_size=0.15, random_state=2022)
X_tr.head()

 

 

6) 평가지표

import numpy as np
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
def rmse(y_test, y_pred): # RMSE
    return np.sqrt(mean_squared_error(y_test, y_pred))
def rmsle(y_test, y_pred): # RMSLE
    return np.sqrt(np.mean(np.power(np.log1p(y_test) - np.log1p(y_pred), 2)))
def mape(y_test, y_pred): # MAPE
    return np.mean(np.abs((y_test - y_pred) / y_test)) * 100

 

 

7) 선형회귀

 
r2:  0.08443940934632932
mae:  74.00660016028452
mse:  44817.58349299408
rmse:  211.7016379081515
rmsle:  0.6341218108203222
mape:  58.97544770577991

 

 

8) 릿지

0.08449983846382447
r2:  0.08449983846382447
mae:  73.96841697454546
mse:  44814.625428780324
rmse:  211.69465139388933
rmsle:  0.624550834150591
mape:  58.93648696395185

 

 

9) 라쏘

r2:  0.07707144198918314
mae:  74.75696045484243
mse:  45178.25267816161
rmse:  212.55176470253454
rmsle:  0.6275723008930043
mape:  60.91168715198994

 

 

10) 랜덤포레스트

r2:  0.24029689715169567
mae:  65.38766246307658
mse:  37188.20751937533
rmse:  192.84244221481777
rmsle:  0.49140644777864845
mape:  45.72220287985546

 

 

11) XGBRegressor

r2:  0.1418973207473755
mae:  69.43030765665861
mse:  42004.95546462088
rmse:  204.95110505830624
rmsle:  0.54789599862578
mape:  50.834984242418045

 

 

12) 예측 및 csv 제출

pred = model.predict(test)
pd.DataFrame({'id':test_id, 'output':pred}).to_csv("22222.csv", index=False)
pd.read_csv("22222.csv")

 

 

 


문3. 분류 (심장마비 확률이 높은 사람)

1) 데이터 불러오기

import pandas as pd
train = pd.read_csv(" ~ ")
test =  pd.read_csv(" ~ ")

 

 

2) 데이터 확인하기

print(train.shape, test.shape)
print(train.head(3))
print(test.head(3))
print(train['output'].value_counts()) # 분포 확인
print(train.isnull().sum())
print(test.isnull().sum())
print(train.describe())
print(train.info())
print(train.nunique())

 

 

3) 데이터 전처리

train = train.drop('id', axis=1)
test_id = test.pop('id')
test.head()

 

 

4) 검증 데이터 분리

from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(train.drop('output', axis=1), train['output'], test_size=0.15, random_state=2022)
X_tr.head()

 

 

5) 모델 및 평가

from sklearn.ensemble import RandomForestClassifier # 모델
rf = RandomForestClassifier(random_state=2022, max_depth=5, n_estimators=200)
rf.fit(X_tr, y_tr)
pred = rf.predict(X_val)
pred_proba = rf.predict_proba(X_val)

from sklearn.metrics import roc_auc_score, f1_score, accuracy_score # 평가
print(roc_auc_score(y_val, pred_proba[:,1]))
print(f1_score(y_val, pred))
print(accuracy_score(y_val, pred))

# 기본
# 0.9301242236024845
# 0.7906976744186046
# 0.7567567567567568

# max_depth=3
# 0.9378881987577639
# 0.8636363636363636
# 0.8378378378378378

# max_depth=5
# 0.9409937888198757
# 0.8444444444444444
# 0.8108108108108109

# max_depth=5, n_estimators=200 (가장 값이 높아서, 확정)
# 0.9440993788819876
# 0.8444444444444444
# 0.8108108108108109

 

 

6) xgb

from xgboost import XGBClassifier
xgb = XGBClassifier(random_state=2022, max_depth=5, n_estimators=400, learning_rate=0.01)
# xgb는 n_estimators와 learning_rate 같이 사용 (값은 반비례로)
xgb.fit(X_tr, y_tr)
pred = xgb.predict(X_val)
pred_proba = xgb.predict_proba(X_val)

print(roc_auc_score(y_val, pred_proba[:,1]))
print(f1_score(y_val, pred))
print(accuracy_score(y_val, pred))

# 기본
# 0.9192546583850931
# 0.8444444444444444
# 0.8108108108108109

# max_depth=5
# 0.906832298136646
# 0.8181818181818182
# 0.7837837837837838

# max_depth=5, n_estimators=400, learning_rate=0.01
# 0.9161490683229814
# 0.8372093023255814
# 0.8108108108108109

 

 

7) 예측 및 csv 제출

pred_proba = xgb.predict_proba(test)
pd.DataFrame({'id':test_id, 'output':pred_proba[:,1]}).to_csv("33333.csv", index=False)
pd.read_csv("33333.csv")

 

 

 

 

 

 

[빅데이터분석기사/실기] 제2유형. 머신러닝 및 평가지표

제2유형은 머신러닝에 대한 문제이며, 1문제이지만 배점이 40점으로 가장 높습니다.주어진 라이브러리와 데이터를 불러온 뒤, 데이터 전처리를 거쳐 모델을 학습시키고 평가한 결과를 CSV 코드

dataslog.tistory.com

 

 

[빅데이터분석기사/실기] 제2유형. 평가지표

제2유형 평가지표에 대한 내용입니다.통계에 대한 내용이라 비전공자는 다소 어렵기 때문에 그냥 통째로 암기!ㅎㅎ 1. 이진분류 평가지표(0과 1로 이루어진 데이터) import pandas as pdfrom sklearn.ensembl

dataslog.tistory.com

 

 

 

 

728x90
728x90