2026 빅데이터 분석기사 실기 작업형 2 유형

2026. 3. 8. 03:24ㆍ빅데이터분석기사

11111

1. 2유형 개요

항목 내용
배점 40점 (1문제)
형식 분류 또는 회귀 예측 모델 구축 후 결과 파일 제출
데이터 train.csv(레이블 포함) + test.csv(레이블 없음)
분류 제출 predict_proba()[:, 1] → 양성 클래스 확률
회귀 제출 predict() → 예측값
평가지표 분류: AUC-ROC / 회귀: RMSE

 

2. 분류 기본 템플릿 (암기 필수)

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import roc_auc_score

import warnings; warnings.filterwarnings('ignore')

 

# 1. 데이터 로드

train = pd.read_csv('train.csv')

test  = pd.read_csv('test.csv')

 

# 2. 타겟·ID 분리

target_col = 'target'   # 실제 타겟 컬럼명으로 변경

id_col = 'id'           # 실제 ID 컬럼명으로 변경

 

y = train[target_col]

X = train.drop(columns=[target_col, id_col], errors='ignore')

X_test = test.drop(columns=[id_col], errors='ignore')

test_id = test[id_col]  # 제출용

 

# 3. 결측치 처리

num_cols = X.select_dtypes(include='number').columns.tolist()

cat_cols = X.select_dtypes(include='object').columns.tolist()

 

for col in num_cols:

    med = X[col].median()

    X[col] = X[col].fillna(med)

    X_test[col] = X_test[col].fillna(med)  # train 중앙값으로 test도

 

for col in cat_cols:

    mode = X[col].mode()[0]

    X[col] = X[col].fillna(mode)

    X_test[col] = X_test[col].fillna(mode)

 

# 4. 범주형 인코딩

for col in cat_cols:

    le = LabelEncoder()

    combined = pd.concat([X[col], X_test[col]]).astype(str)

    le.fit(combined)

    X[col] = le.transform(X[col].astype(str))

    X_test[col] = le.transform(X_test[col].astype(str))

 

# 5. 모델 학습

model = RandomForestClassifier(n_estimators=100, random_state=42)

model.fit(X, y)

 

# (선택) 검증 AUC 확인

X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2,

                                              random_state=42, stratify=y)

model.fit(X_tr, y_tr)

val_prob = model.predict_proba(X_val)[:, 1]

print('Val AUC:', roc_auc_score(y_val, val_prob))

model.fit(X, y)  # 전체 데이터로 재학습

 

# 6. 예측 및 저장

pred_prob = model.predict_proba(X_test)[:, 1]

result = pd.DataFrame({id_col: test_id, target_col: pred_prob})

result.to_csv('result.csv', index=False)

print(result.head())

 

3. 회귀 기본 템플릿

from sklearn.ensemble import GradientBoostingRegressor

from sklearn.metrics import mean_squared_error

import numpy as np

 

# (전처리는 분류 템플릿과 동일 - 생략)

 

# 모델 학습 (회귀)

model = GradientBoostingRegressor(n_estimators=100,

                                  learning_rate=0.1,

                                  max_depth=4,

                                  random_state=42)

model.fit(X, y)

 

# 검증 RMSE

X_tr, X_val, y_tr, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

model.fit(X_tr, y_tr)

y_pred_val = model.predict(X_val)

rmse = np.sqrt(mean_squared_error(y_val, y_pred_val))

print(f'Val RMSE: {rmse:.4f}')

model.fit(X, y)   # 전체 재학습

 

# 예측 및 저장

pred = model.predict(X_test)

result = pd.DataFrame({id_col: test_id, target_col: pred})

result.to_csv('result.csv', index=False)

print(result.head())

 

4. 주요 모델 및 파라미터

모델 import 경로 주요 파라미터 비고
RandomForestClassifier sklearn.ensemble n_estimators=100, max_depth, random_state=42 불균형에 class_weight='balanced'
RandomForestRegressor sklearn.ensemble n_estimators=100, random_state=42 회귀용
GradientBoostingClassifier sklearn.ensemble n_estimators, learning_rate=0.1, max_depth=3 느리지만 성능 좋음
GradientBoostingRegressor sklearn.ensemble n_estimators, learning_rate, max_depth 회귀용
LogisticRegression sklearn.linear_model C=1.0, max_iter=1000, solver='lbfgs' 선형 분류
LinearRegression sklearn.linear_model fit_intercept=True 기본 회귀
Ridge sklearn.linear_model alpha=1.0 L2 정규화 회귀
Lasso sklearn.linear_model alpha=1.0 L1 정규화 회귀
DecisionTreeClassifier sklearn.tree max_depth=5, random_state=42 해석 가능
DecisionTreeRegressor sklearn.tree max_depth=5, random_state=42 회귀 트리
SVC sklearn.svm C=1.0, kernel='rbf', probability=True 확률 출력 시 probability=True
KNeighborsClassifier sklearn.neighbors n_neighbors=5 게으른 학습
XGBClassifier xgboost n_estimators=100, eval_metric='logloss' xgboost 별도 설치
LGBMClassifier lightgbm n_estimators=100, num_leaves=31 lightgbm 별도 설치

 

5. 평가 지표 코드

from sklearn.metrics import (

    accuracy_score, precision_score, recall_score,

    f1_score, roc_auc_score, confusion_matrix,

    mean_squared_error, mean_absolute_error, r2_score)

import numpy as np

 

# 분류 평가

y_pred = model.predict(X_val)

y_prob = model.predict_proba(X_val)[:, 1]

 

print('Accuracy :', round(accuracy_score(y_val, y_pred), 4))

print('Precision:', round(precision_score(y_val, y_pred), 4))

print('Recall   :', round(recall_score(y_val, y_pred), 4))

print('F1-Score :', round(f1_score(y_val, y_pred), 4))

print('AUC-ROC  :', round(roc_auc_score(y_val, y_prob), 4))

print('혼동행렬:')

print(confusion_matrix(y_val, y_pred))

 

# 회귀 평가

y_pred = model.predict(X_val)

mse  = mean_squared_error(y_val, y_pred)

rmse = np.sqrt(mse)

mae  = mean_absolute_error(y_val, y_pred)

r2   = r2_score(y_val, y_pred)

print(f'MSE : {mse:.4f}')

print(f'RMSE: {rmse:.4f}')

print(f'MAE : {mae:.4f}')

print(f'R²  : {r2:.4f}')

 

6. 교차검증 코드

from sklearn.model_selection import (

    cross_val_score, StratifiedKFold, KFold)

 

# 분류 Stratified K-Fold

skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

scores = cross_val_score(model, X, y, cv=skf, scoring='roc_auc')

print(f'CV AUC: {scores.mean():.4f} ± {scores.std():.4f}')

 

# 회귀 K-Fold

kf = KFold(n_splits=5, shuffle=True, random_state=42)

scores = cross_val_score(model, X, y, cv=kf,

                         scoring='neg_mean_squared_error')

rmse_scores = np.sqrt(-scores)

print(f'CV RMSE: {rmse_scores.mean():.4f} ± {rmse_scores.std():.4f}')

 

7. 하이퍼파라미터 튜닝

from sklearn.model_selection import GridSearchCV

 

# Grid Search

param_grid = {

    'n_estimators': [50, 100, 200],

    'max_depth'   : [3, 5, 7, None],

    'min_samples_split': [2, 5, 10]

}

gs = GridSearchCV(

    RandomForestClassifier(random_state=42),

    param_grid, cv=5, scoring='roc_auc', n_jobs=-1, verbose=0)

gs.fit(X, y)

print('Best params:', gs.best_params_)

print('Best AUC  :', gs.best_score_)

best_model = gs.best_estimator_

 

# Random Search (파라미터 많을 때)

from sklearn.model_selection import RandomizedSearchCV

from scipy.stats import randint, uniform

 

param_dist = {

    'n_estimators': randint(50, 300),

    'max_depth'   : randint(3, 15),

    'max_features': ['sqrt', 'log2', None]

}

rs = RandomizedSearchCV(

    RandomForestClassifier(random_state=42),

    param_dist, n_iter=20, cv=5, scoring='roc_auc',

    n_jobs=-1, random_state=42)

rs.fit(X, y)

print('Best params:', rs.best_params_)

 

8. 불균형 데이터 처리

# 클래스 비율 확인

print(y.value_counts())

print(y.value_counts(normalize=True))

 

# 방법 1: class_weight 설정 (가장 간단)

model = RandomForestClassifier(class_weight='balanced', random_state=42)

model.fit(X, y)

 

# 방법 2: SMOTE 오버샘플링

from imblearn.over_sampling import SMOTE

smote = SMOTE(random_state=42)

X_res, y_res = smote.fit_resample(X, y)

print('리샘플링 후:', pd.Series(y_res).value_counts())

model.fit(X_res, y_res)

 

# 방법 3: 임계값 조정 (0.5 → 0.3)

y_prob = model.predict_proba(X_val)[:, 1]

threshold = 0.3  # 조정할 임계값

y_pred_adj = (y_prob >= threshold).astype(int)

print('Recall(0.3):', recall_score(y_val, y_pred_adj))

 

9. 특성 중요도

# 랜덤 포레스트 특성 중요도

model = RandomForestClassifier(n_estimators=100, random_state=42)

model.fit(X, y)

 

importance = pd.DataFrame({

    'feature'   : X.columns,

    'importance': model.feature_importances_

}).sort_values('importance', ascending=False)

 

print(importance.head(10))

 

# 중요도 낮은 변수 제거 (0.01 미만)

important_features = importance[importance['importance'] >= 0.01]['feature'].tolist()

X_reduced = X[important_features]

X_test_reduced = X_test[important_features]

2222