如何改进 catboostregressor?
How to improve the catboostregressor?
我正在研究一个数据科学回归问题,训练集上有大约 90,000 行,测试集上有 8500 行。有 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] 值不同但性能相同。
谁能帮助我如何通过最小化 MAE 和 MSE 来提高 R2 值?
简单方法-
您可以使用 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 小时)。当您有很多参数需要调整时,最好使用此方法。否则,使用网格搜索 CV。
我正在研究一个数据科学回归问题,训练集上有大约 90,000 行,测试集上有 8500 行。有 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] 值不同但性能相同。
谁能帮助我如何通过最小化 MAE 和 MSE 来提高 R2 值?
简单方法-
您可以使用 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 小时)。当您有很多参数需要调整时,最好使用此方法。否则,使用网格搜索 CV。