# 하이퍼파라미터 분포 설정 (랜덤 서치를 위해)
param_dist = {
'learning_rate': np.linspace(0.01, 0.2, 20), # 0.01부터 0.2까지 균등 분포
'n_estimators': [50, 100, 200, 500], # 트리 개수
'max_depth': [3, 5, 7, 10, 15], # 트리의 최대 깊이
'num_leaves': [31, 50, 70, 100], # 리프의 개수
'subsample': np.linspace(0.6, 1.0, 5), # 샘플링 비율
'colsample_bytree': np.linspace(0.6, 1.0, 5) # 특성 샘플링 비율
}
# 모델 정의
light_model = lgb.LGBMClassifier()
# RandomizedSearchCV 정의
random_search = RandomizedSearchCV(
estimator=light_model,
param_distributions=param_dist,
n_iter=100, # 100번 랜덤 샘플링을 시도
cv=5, # 5-폴드 교차 검증
n_jobs=-1, # 모든 코어 사용
verbose=1, # 진행 상황 출력
random_state=42 # 랜덤 시드 고정
)
0.5932875554
처음으로 0.6 넘김!
cat_model = CatBoostClassifier(verbose=0)
# 하이퍼파라미터 그리드 설정
param_grid = {
'iterations': [500, 1000, 1500],
'learning_rate': [0.01, 0.05, 0.1],
'depth': [4, 6, 8],
'l2_leaf_reg': [1, 3, 5],
}
randomized_cat = cat_model.randomized_search(
param_distributions=param_grid,
X=X_train,
y=y_train,
cv=5,
n_iter=100, # 100개의 랜덤 조합만 탐색
partition_random_seed=42,
stratified=True,
refit=True
)
# 최적의 하이퍼파라미터 가져오기
best_params = randomized_cat['params']
print("Best parameters found:", best_params)
# ⚡ 최적 하이퍼파라미터로 새 모델 훈련 (이때 얼리 스토핑 적용!)
best_model = CatBoostClassifier(**best_params, verbose=100)
best_model.fit(
X_train, y_train,
eval_set=(X_val, y_val), # 검증 데이터 추가
early_stopping_rounds=50, # 얼리 스토핑 적용
use_best_model=True
)
0.6014728566
원핫 인코딩 사용하지 않고 자체 parameter 사용
=> 이게 더 결과값이 떨어짐
from catboost import CatBoostClassifier
categorical_col = [
'주거 형태',
'현재 직장 근속 연수',
'대출 목적',
'대출 상환 기간'
]
cat_model = CatBoostClassifier(
verbose=0,
cat_features=categorical_col)
# 하이퍼파라미터 그리드 설정
param_grid = {
'iterations': [500, 1000, 1500],
'learning_rate': [0.01, 0.05, 0.1],
'depth': [4, 6, 8],
'l2_leaf_reg': [1, 3, 5],
}
randomized_cat = cat_model.randomized_search(
param_distributions=param_grid,
X=X_train,
y=y_train,
cv=5,
n_iter=100, # 100개의 랜덤 조합만 탐색
partition_random_seed=42,
stratified=True,
refit=True,
verbose=1
)
# 최적의 하이퍼파라미터 가져오기
best_params = randomized_cat['params']
print("Best parameters found:", best_params)
# 최적 하이퍼파라미터로 새 모델 훈련
best_model = CatBoostClassifier(**best_params, cat_features=categorical_col, verbose=100)
best_model.fit(
X_train, y_train,
eval_set=(X_val, y_val), # 검증 데이터 추가
early_stopping_rounds=50, # 얼리 스토핑 적용
use_best_model=True
)
0.5984574812
# 구글 코랩에 한글 폰트를 설치
!apt-get update -qq
!apt-get install -qq fonts-nanum
# 설치된 폰트를 확인
!fc-list :lang=ko
plt.rcParams['font.family'] ='NanumGothic'
plt.rcParams['axes.unicode_minus'] =False