KFold의 경우 순차적으로 샘플을 추출 하기때문에 오류가 발생할 수 있다.
이에 StratifiedKFold를 활용하여 그 오류를 줄일 수 있다.
모델은 xgboost를 활용하고
검증은 MAE(Mean Absolute Error) 방식이다
from sklearn.model_selection import StratifiedKFold
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error
skf= StratifiedKFold(n_splits=5, shuffle=True, random_state=50)
print(skf)
n_itr = 0
#model 선언
xgb = XGBRegressor(learning_rate =0.1,
#StratifiedKFold
for train_index, test_index in skf.split(X, y):
n_itr += 1
print(f'--{n_itr}번째 KFold--')
print(f'train_idx_len : {len(train_index)} / test_idx_len : {len(test_index)}')
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
xgb.fit(X_train, y_train)
preds = xgb.predict(X_test)
score = mean_absolute_error(y_test, preds)
score_list.append(score)
print(f'{n_itr}번째 정확도 : {score}')
print(f'최종 평균 정확도 : {sum(score_list)/len(score_list)}')
'소소한 > #Python' 카테고리의 다른 글
Optuna를 활용 xgboost hyperparameter 튜닝 (0) | 2022.11.07 |
---|---|
모델검증 train_test_split (0) | 2022.11.02 |
[ML-Python] 회귀 분류 학습 (0) | 2022.08.30 |
[Python] Pip install 안될 때 (0) | 2022.08.13 |
#01.python : List 만들기 및 출력 (0) | 2014.12.17 |