K-Fold Cross Validation

지리산근육곰·2021년 12월 21일
0
post-thumbnail

K-Fold Cross Validation

1 Import libraries

# import K-folds library 
from sklearn.model_selection import KFold

# Evaluation Score Library
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score

2 Define Functions for K-Folds

2.1 Print Function

  • MAE, MSE, R2 스코어를 프린트해주는 Function이다.
  • 원하는 평가 지표에 따라 수정하여 사용한다.
def print_function(scores):

    score1, score2, score3, score4, score5, score6 = scores
    print("------ MAE ------")
    print("Train loss : %.4f" % score1)
    print("Validation loss : %.4f" % score2)
    print()
    print("------ MSE ------")
    print("Train loss : %.4f" % score3)
    print("Validation loss : %.4f" % score4)
    print()
    print("------ R2 ------")
    print("Train R2 score : %.4f" % score5)
    print("Validation R2 score : %.4f" % score6)
    print()

2.2 Calculating CV Score

  • 위에서 정의한 print_function의 스코어들을 계산하는 함수이다.
  • 원하는 스코어에 따라 사용하면 된다.

    만약 스코어의 평균값 뿐만 아니라 각 fold별 값을 보고 싶을 경우 * 표시된 곳을 활성화 한다.

def train_and_validation(train_data, validation_data, model, metrics, print_mode):
    # 
    X_train, y_train = train_data
    X_val, y_val = validation_data
    
    model.fit(X_train, y_train)
    train_pred = model.predict(X_train)
    val_pred = model.predict(X_val)

    score1 = metrics[0](y_train, train_pred)
    score2 = metrics[0](y_val, val_pred)
    score3 = metrics[1](y_train, train_pred)
    score4 = metrics[1](y_val, val_pred)
    score5 = metrics[2](y_train, train_pred)
    score6 = metrics[2](y_val, val_pred)

    scores = [score1, score2]
	
    ### *각 fold의 스코어를 보고 싶을 경우
    # if print_mode:
    #     print_function(scores)
    
    return np.array(scores)

3 K-Fold CV

3.1 Model Selection and Setting

# Choose K
K = 5

# K-folds
kfcv = KFold(n_splits=K, shuffle=True, random_state=42)

# evalution Score
evalution = [mean_absolute_error, mean_squared_error, r2_score]

# models 
## 원하는 모델을 설정한다
lr = LinearRegression(normalize=True)
lgbm = LGBMRegressor()
catb = CatBoostRegressor(silent=True)
xgbm = XGBRegressor(silent=True)

## 선택한 모델을 리스트로 생성한다.
models = [lr, lgbm, catb, xgbm]

print_mode = True

3.2 Run K-Fold CV

  • 실행 할 경우 K-fold의 평균값이 리턴된다.

    만약 각 fold의 값을 원할 경우 *표시된 곳을 홝성화한다.

# 몇몇 모델의 경우 장문의 메세지가 뜨는데 원치 않을경우 사용한다.
import warnings
warnings.filterwarnings("ignore")

for index, model in enumerate(models):
    if print_mode:
        print(f"\n====== Model {model} ======\n")

    # generate a blank fold
    folds = []

    # model's scores
    model_scores = []

    X = X_iter
    # Generate K-fold
    for train_index, val_index in kfcv.split(X, y):
        folds.append((train_index, val_index))
    
    # fold 별 학습 및 검증
    for i in range(K):
        ### * 각 fold의 값을 원할경우
        # if print_mode:
            # print(f"{i+1}th folds in {K} folds.")
        
        train_index, val_index = folds[i]

        X_train = X.iloc[train_index, :]
        X_val = X.iloc[val_index, :]
        y_train = y[train_index]
        y_val = y[val_index]

        # 모델별 score 산축
        scores = train_and_validation((X_train, y_train), (X_val, y_val), model, evalution, print_mode)
        model_scores.append(scores)

    # mean of scores
    model_scores = np.array(model_scores)
    if print_mode:
        print("Average Score in %dfolds." % K)
        print_function(model_scores.mean(axis=0))

print("Done.")

0개의 댓글