Skip to content

Instantly share code, notes, and snippets.

@kusal1990
Last active June 2, 2022 18:28
Show Gist options
  • Save kusal1990/82676da50a8a6b1bdc258f7cf16e516f to your computer and use it in GitHub Desktop.
Save kusal1990/82676da50a8a6b1bdc258f7cf16e516f to your computer and use it in GitHub Desktop.
# Parameters to tune for random forest model
params = {'n_estimators': [10, 50, 100, 500, 1000],
'max_depth': [2, 3, 4, 5, 6],
'min_samples_split': [0.02, 0.04, 0.08, 0.16, 0.32, 0.50]}
# 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
rf_clf = RandomForestClassifier(random_state=42,
class_weight='balanced')
# Perform stratified 5-fold cross validation
rand_clf = RandomizedSearchCV(rf_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