Skip to content

Instantly share code, notes, and snippets.

@kusal1990
Created June 2, 2022 18:31
Show Gist options
  • Save kusal1990/c88c32c2d3eb5ca66dfa85769fc055b3 to your computer and use it in GitHub Desktop.
Save kusal1990/c88c32c2d3eb5ca66dfa85769fc055b3 to your computer and use it in GitHub Desktop.
# Parameters to tune for XGBoost model
params = {'n_estimators': [10, 50, 100, 500, 1000],
'max_depth': [2, 3, 4, 5, 6],
'learning_rate': [0.0001,0.005,0.001,0.05, 0.1],
'reg_alpha': [1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2],
'reg_lambda': [1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2]}
# Create a custom (MCC) metric for evaluation of the model performance while
# hyperparameter tuning the XGBoost model
mcc = make_scorer(matthews_corrcoef, greater_is_better=True)
# Create an XGBoost classifier object with log-loss as the loss function to minimize
xgb_clf = xgb.XGBClassifier(random_state=42)
# Perform stratified 5-fold cross validation
rand_clf = RandomizedSearchCV(xgb_clf, param_distributions=params, scoring=mcc,
cv=5, random_state=42, return_train_score=True,
n_jobs=-1)
# Fit the model
rand_clf.fit(X_train, y_train)
print(f"Best CV score: {rand_clf.best_score_}")
@kusal1990
Copy link
Author

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment