Skip to content

Instantly share code, notes, and snippets.

@ImadDabbura
Created August 3, 2018 21:05
Show Gist options
  • Select an option

  • Save ImadDabbura/2ff0b523c1c9ef31488d438cc8b5ae48 to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/2ff0b523c1c9ef31488d438cc8b5ae48 to your computer and use it in GitHub Desktop.
# Build random forest classifier (same config)
rf_clf = RandomForestClassifier(n_estimators=500,
max_features=0.25,
criterion="entropy",
class_weight="balanced")
# Build model with no sampling
pip_orig = make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
rf_clf)
scores = cross_val_score(pip_orig,
X_train, y_train,
scoring="roc_auc", cv=10)
print(f"Original model's average AUC: {scores.mean():.3f}")
# Build model with undersampling
pip_undersample = imb_make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
RandomUnderSampler(),
rf_clf)
scores = cross_val_score(pip_undersample,
X_train, y_train,
scoring="roc_auc", cv=10)
print(f"Under-sampled model's average AUC: {scores.mean():.3f}")
# Build model with oversampling
pip_oversample = imb_make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
RandomOverSampler(),
rf_clf)
scores = cross_val_score(pip_oversample,
X_train, y_train,
scoring="roc_auc", cv=10)
print(f"Over-sampled model's average AUC: {scores.mean():.3f}")
# Build model with EasyEnsemble
resampled_rf = BalancedBaggingClassifier(base_estimator=rf_clf,
n_estimators=10,
random_state=123)
pip_resampled = make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
resampled_rf)
scores = cross_val_score(pip_resampled,
X_train, y_train,
scoring="roc_auc", cv=10)
print(f"EasyEnsemble model's average AUC: {scores.mean():.3f}")
# Build model with SMOTE
pip_smote = imb_make_pipeline(Imputer(strategy="mean"),
RobustScaler(),
SMOTE(),
rf_clf)
scores = cross_val_score(pip_smote,
X_train, y_train,
scoring="roc_auc", cv=10)
print(f"SMOTE model's average AUC: {scores.mean():.3f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment