Skip to content

Instantly share code, notes, and snippets.

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

  • Save ImadDabbura/30f0560aa9056e76f540142345f9ae0f to your computer and use it in GitHub Desktop.

Select an option

Save ImadDabbura/30f0560aa9056e76f540142345f9ae0f to your computer and use it in GitHub Desktop.
# Impute the missing data using features means
imp = Imputer()
imp.fit(X_train)
X_train = imp.transform(X_train)
X_test = imp.transform(X_test)
# Standardize the data
std = RobustScaler()
std.fit(X_train)
X_train = std.transform(X_train)
X_test = std.transform(X_test)
# Implement RandomUnderSampler
random_undersampler = RandomUnderSampler()
X_res, y_res = random_undersampler.fit_sample(X_train, y_train)
# Shuffle the data
perms = np.random.permutation(X_res.shape[0])
X_res = X_res[perms]
y_res = y_res[perms]
# Define base learners
xgb_clf = xgb.XGBClassifier(objective="binary:logistic",
learning_rate=0.03,
n_estimators=500,
max_depth=1,
subsample=0.4,
random_state=123)
svm_clf = SVC(gamma=0.1,
C=0.01,
kernel="poly",
degree=3,
coef0=10.0,
probability=True)
rf_clf = RandomForestClassifier(n_estimators=300,
max_features="sqrt",
criterion="gini",
min_samples_leaf=5,
class_weight="balanced")
# Define meta-learner
logreg_clf = LogisticRegression(penalty="l2", C=100, fit_intercept=True)
# Fitting voting clf --> average ensemble
voting_clf = VotingClassifier([("xgb", xgb_clf),
("svm", svm_clf),
("rf", rf_clf)],
voting="soft",
flatten_transform=True)
voting_clf.fit(X_res, y_res)
xgb_model, svm_model, rf_model = voting_clf.estimators_
models = {"xgb": xgb_model,
"svm": svm_model,
"rf": rf_model,
"avg_ensemble": voting_clf}
# Build first stack of base learners
first_stack = make_pipeline(voting_clf,
FunctionTransformer(lambda X: X[:, 1::2]))
# Use CV to generate meta-features
meta_features = cross_val_predict(first_stack, X_res, y_res, cv=10, method="transform")
# Refit the first stack on the full training set
first_stack.fit(X_res, y_res)
# Fit the meta learner
second_stack = logreg_clf.fit(meta_features, y_res)
# Plot ROC and PR curves using all models and test data
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
for name, model in models.items():
model_probs = model.predict_proba(X_test)[:, 1:]
model_auc_score = roc_auc_score(y_test, model_probs)
fpr, tpr, _ = roc_curve(y_test, model_probs)
precision, recall, _ = precision_recall_curve(y_test, model_probs)
axes[0].plot(fpr, tpr, label=f"{name}, auc = {model_auc_score:.3f}")
axes[1].plot(recall, precision, label=f"{name}")
stacked_probs = second_stack.predict_proba(first_stack.transform(X_test))[:, 1:]
stacked_auc_score = roc_auc_score(y_test, stacked_probs)
fpr, tpr, _ = roc_curve(y_test, stacked_probs)
precision, recall, _ = precision_recall_curve(y_test, stacked_probs)
axes[0].plot(fpr, tpr, label=f"stacked_ensemble, auc = {stacked_auc_score:.3f}")
axes[1].plot(recall, precision, label="stacked_ensembe")
axes[0].legend(loc="lower right")
axes[0].set_xlabel("FPR")
axes[0].set_ylabel("TPR")
axes[0].set_title("ROC curve")
axes[1].legend()
axes[1].set_xlabel("recall")
axes[1].set_ylabel("precision")
axes[1].set_title("PR curve")
plt.tight_layout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment