현실의 데이터는 연속적이지 않고 군데군데 비어있는 경우가 많이 존재한다.
1. 데이터가 one-hot encoding 되어있는 경우
2. missing value가 존재하는 경우
3. 통계적으로 0같은 특정값이 많이 존재하는 경우
-> 이런 경우들에 대하여 값을 한쪽 브랜치로 몰아넣음으로써 해결
import optuna
from optuna.samplers import TPESampler
from optuna import Trial
def objective(trial):
X = train.drop("Strength",axis=1)
y = train["Strength"]
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.2,random_state=42)
param = {
'lambda': trial.suggest_float('lambda', 1e-3, 0.1),
'alpha': trial.suggest_float('alpha', 1e-3, 1.0),
'colsample_bytree': trial.suggest_float('colsample_bytree', 0.4, 1),
'subsample': trial.suggest_float('subsample', 0.4, 1),
'learning_rate': trial.suggest_float('learning_rate',0.0001, 0.1),
'n_estimators': trial.suggest_int('n_estimators', 100, 1000),
'max_depth': trial.suggest_int('max_depth', 4,8),
'min_child_weight': trial.suggest_int('min_child_weight', 2, 50),
}
model =XGBRegressor(**param)
model.fit(train_x,train_y,eval_set=[(test_x,test_y)],early_stopping_rounds=100)
preds = model.predict(test_x)
return
sampler = TPESampler()
study_xgb = optuna.create_study(
direction='minimize',
study_name = 'Xgboost Optuna',
sampler=sampler
)
study_xgb.optimize(objective, n_trials=50)