<jupyter_start><jupyter_text>XGBoost Without SMOTE<jupyter_code>#calculating the weights of class 0 to 1
class_0 = y.loc[y['cancelled'] == 0].value_counts().values[0]
class_1 = y.loc[y['cancelled'] == 1].value_counts().values[0]
weight = class_0/class_1
print('Ratio of Class 0 to Class 1:',weight)
X_train, X_test, y_train, y_test = train_test_split(X, y,shuffle=True,test_size=0.30, random_state=123)
y_pred = model_weight.predict(X_test.values)
print('roc_auc score for weighted model:',roc_auc_score(y_test,y_pred))
print('accuracy score: ',accuracy_score(y_test,y_pred))
print('f1 score: ',f1_score(y_test,y_pred))
c = confusion_matrix(y_test,y_pred)
#plotting the ROC_AUC
probs = model_weight.predict_proba(X_test)
preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y_test, y_pred)
roc_auc = metrics.auc(fpr, tpr)
plt.figure(figsize=(10,8))
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
ns_probs = [0 for _ in range(len(y_test))]
y_probs = model_weight.predict_proba(X_test)
y_probs = y_probs[:, 1]
ns_auc = roc_auc_score(y_test, ns_probs)
y_auc = roc_auc_score(y_test, y_probs)
# calculate roc curves
ns_fpr, ns_tpr, _ = roc_curve(y_test, ns_probs)
y_fpr, y_tpr, _ = roc_curve(y_test, y_probs)
# plot the roc curve for the model
plt.figure(figsize=(10,8))
plt.title('ROC for different thresholds')
plt.plot(ns_fpr, ns_tpr, linestyle='--', label='No Skill')
plt.plot(y_fpr, y_tpr, marker='.', label='XGBoost')
# axis labels
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
# show the legend
plt.legend()
# show the plot
plt.show()
#Normalized Confusion Matrix
normed_c = (c.T / c.astype(np.float).sum(axis=1)).T
plt.figure(figsize=(10,8))
df_cm = normed_c
sn.set(font_scale=1.4) # for label size
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16}) # font size
plt.title('Normalized Confusion Matrix')
plt.show()<jupyter_output><empty_output><jupyter_text>XGBoost With SMOTE<jupyter_code>oversample = SMOTE(random_state=0)
X_resampled, y_resampled = oversample.fit_sample(X, y)
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled,shuffle=True,\
                                                    stratify=y_resampled, test_size=0.30, random_state=123)
y_pred_smoote = model_weight.predict(X_test.values)
#printing the scores
print('roc_auc score for SMOTE:', roc_auc_score(y_test,y_pred_smoote))
print('f1 score for SMOTE:', f1_score(y_test,y_pred_smoote))
print('accuracy score: ',accuracy_score(y_test,y_pred_smoote))
probs = model_smoote.predict_proba(X_test)
preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y_test, y_pred_weight)
roc_auc = metrics.auc(fpr, tpr)
plt.figure(figsize=(10,8))
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
c = confusion_matrix(y_test,y_pred_weight)
normed_c = (c.T / c.astype(np.float).sum(axis=1)).T
plt.figure(figsize=(10,8))
df_cm = normed_c
# plt.figure(figsize=(10,7))
sn.set(font_scale=1.4) # for label size
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16}) # font size
plt.title('Normalized Confusion Matrix')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16005665722379603, 'fraction_numerical': 0.02294617563739377, 'mean_word_length': 6.5610278372591, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '566225', 'license': 'MIT', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1454, 'n_words': 276, 'path': 'XGB_binary_Presentation.ipynb', 'repo_name': 'bnati5/midterm-project', 'size': '3530'}