반응형


Scikit-learn Gradient Boosting 모델 예측값이 매번 달라지는 문제와 해결


아래 코드는 k-fold cross-validation 을 통해 best parameter 를 찾은 후, test set 에 대해 예측하는 코드이다. 이 코드에서 random_state=2020 을 지정하지 않으면, GridSearchCV 를 통해 구한 best parameters set이 매번 달라졌다. 즉 이말은 Gradient Boosting Tree 가 fitting 할 때마다 달라진다는 뜻이다. 


def cv_and_prediction(x_vars, name, model=False): train_x = metadata_valid[x_vars] train_y = metadata_valid["case"] test_x = metadata_test[x_vars] test_y = metadata_test["case"] model = GradientBoostingClassifier(random_state=2020) param_test = { "n_estimators": range(50, 100, 25), "max_depth": [1, 2, 4], "learning_rate": [0.0001, 0.001, 0.01, 0.1], "subsample": [0.7, 0.9], "max_features": list(range(1, len(x_vars), 2)), } gsearch = GridSearchCV( estimator=model, param_grid=param_test, scoring="roc_auc", n_jobs=4, iid=False, cv=5, ) gsearch.fit(train_x, train_y) print(name) print("Best CV Score", gsearch.best_score_) print("Best Params", gsearch.best_params_) model = GradientBoostingClassifier(**gsearch.best_params_) model.fit(train_x, train_y) metadata_test[name] = model.predict_proba(test_x)[:, 1] return model


왜 이런문제가 발생하는가 ?


Decision trees can be unstable because small variations in the data might result in a completely different tree being generated. This problem is mitigated by using decision trees within an ensemble.


The problem of learning an optimal decision tree is known to be NP-complete under several aspects of optimality and even for simple concepts. Consequently, practical decision-tree learning algorithms are based on heuristic algorithms such as the greedy algorithm where locally optimal decisions are made at each node. Such algorithms cannot guarantee to return the globally optimal decision tree. This can be mitigated by training multiple trees in an ensemble learner, where the features and samples are randomly sampled with replacement.


구글링을 통해 문제가 발생할 수 있을만한 원인을 찾았다. 


1. 우선 Decision tree 를 적합하는 방법은 heuristic algorithm (greedy algorithm) 이다. 왜냐하면 optimal 한 decision tree 를 찾는 것은 np-complete 문제이기 때문이다. 하지만 직접적인 문제는 아닌듯하다. greedy 한 방법으로 tree 를 만들더라도 매번 같은 greedy 한 방법을 이용하면 같은 tree 가 생성되기 때문이다.


2. Gradient Boosting 과 같은 ensemble 방법에서는 매 iteration 마다 subsample 을 만들어서 negative gradient 를 예측하는 모델을 만들어서 합치게 되는데 이 때, subsample 이 random 하게 설정되기 때문에 매 번 모형이 달라지고 이로 인해 예측 값이 조금씩 달라지게 된다.  


정확한 원인이 무엇이든 Gradient boosting tree 를 만드는 것에는 randomness가 있으며 reproducible 한 코드를 작성하고 싶으면 GradientBoostingClassifier(random_state=2020) 와 같이 random_state 를 지정해야한다. 


참고

https://scikit-learn.org/stable/modules/tree.html#tree

반응형
반응형