我正在处理一个数据科学回归问题,训练集大约有90,000行,测试集有8,500行。有9个分类列,没有缺失数据。对于这种情况,我使用了CatBoostRegressor,得到了非常好的R2值(98.51)和MAE值(3.77)。其他模型如LGBM和XGBOOST的表现不如CatBoost。
现在我想提高R2值并降低MAE值,以获得更准确的结果。这也是需求所要求的。
我多次调整过参数,添加了’loss_function’: [‘MAE’],’l2_leaf_reg’:[3],’random_strength’: [4],’bagging_temperature’:[0.5],使用了不同的值,但性能没有变化。
谁能帮我提升R2值,同时最小化MAE和MSE?
回答:
简单方法 –
你可以使用Scikit-Learn的GridSearchCV
来寻找你的CatBoostRegressor
模型的最佳超参数。你可以传递一个超参数字典,GridSearchCV
会遍历所有超参数,并告诉你哪些参数是最好的。你可以这样使用它 –
from sklearn.model_selection import GridSearchCV
model = CatBoostRegressor()
parameters = {'depth' : [6,8,10],
'learning_rate' : [0.01, 0.05, 0.1],
'iterations' : [30, 50, 100]
}
grid = GridSearchCV(estimator=model, param_grid = parameters, cv = 2, n_jobs=-1)
grid.fit(X_train, y_train)
另一种方法 –
如今,模型复杂且有许多参数需要调整。人们正在使用贝叶斯优化技术,如Optuna,来调整超参数。你可以使用Optuna来调整CatBoostClassifier
,像这样:
!pip install optuna
import catboost
import optuna
def objective(trial):
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2)
param = {
"objective": trial.suggest_categorical("objective", ["Logloss", "CrossEntropy"]),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.001, 0.3),
"colsample_bylevel": trial.suggest_float("colsample_bylevel", 0.01, 0.1),
"max_depth": trial.suggest_int("max_depth", 1, 15),
"boosting_type": trial.suggest_categorical("boosting_type", ["Ordered", "Plain"]),
"bootstrap_type": trial.suggest_categorical(
"bootstrap_type", ["Bayesian", "Bernoulli", "MVS"]),
}
if param["bootstrap_type"] == "Bayesian":
param["bagging_temperature"] = trial.suggest_float("bagging_temperature", 0, 10)
elif param["bootstrap_type"] == "Bernoulli":
param["subsample"] = trial.suggest_uniform("subsample", 0.1, 1)
gbm = catboost.CatBoostClassifier(**param, iterations = 10000)
gbm.fit(X_train, y_train, eval_set = [(X_val, y_val)], verbose = 0, early_stopping_rounds = 100)
preds = gbm.predict(X_val)
pred_labels = np.rint(preds)
accuracy = accuracy_score(y_val, pred_labels)
return accuracy
study = optuna.create_study(direction = "maximize")
study.optimize(objective, n_trials = 200, show_progress_bar = True)
这种方法需要很长时间(1-2小时,可能)。当你有许多参数需要调整时,这种方法是最好的。否则,使用Grid Search CV。