<jupyter_start><jupyter_text>Processing training dataset<jupyter_code>X = data.drop(columns=['diagnosis'])
y = data['diagnosis'].map({'malignant': 1, 'benign': 0}.get).values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=11)

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)<jupyter_output>(455, 30)
(455,)
(114, 30)
(114,)<jupyter_text>Modelling<jupyter_code>meta_gbc = GradientBoostingClassifier()

param_dist = {
    'n_estimators': list(range(10, 210, 10)),
    'criterion': ['mae', 'mse'],
    'max_features': ['sqrt', 'log2', 0.25, 0.3, 0.5, 0.8, None],
    'max_depth': list(range(1, 10)),
    'min_samples_leaf': list(range(1, 10)),
}
rand_search_params = {
    'param_distributions': param_dist,
    'scoring': 'accuracy',
    'n_iter': 100,
    'cv': 5,
    'return_train_score': True,
    'n_jobs': -1,
    'random_state': 11
}
random_search = RandomizedSearchCV(meta_gbc, **rand_search_params)
random_search.fit(X_train, y_train)
idx = np.argmax(random_search.cv_results_['mean_test_score'])
final_params = random_search.cv_results_['params'][idx]
final_params
train_X, val_X, train_y, val_y = train_test_split(X_train, y_train, test_size=0.15, random_state=11)
train_X.shape, train_y.shape, val_X.shape, val_y.shape
gbc = GradientBoostingClassifier(**final_params)
gbc.fit(train_X, train_y)

preds_train = gbc.predict(train_X)
preds_val = gbc.predict(val_X)
pred_probs_val = np.array([each[1] for each in gbc.predict_proba(val_X)])
print('train accuracy_score = {}'.format(accuracy_score(y_true=train_y, y_pred=preds_train)))
print('validation accuracy_score = {}'.format(accuracy_score(y_true=val_y, y_pred=preds_val)))

print('confusion_matrix: \n{}'.format(confusion_matrix(y_true=val_y, y_pred=preds_val)))
print('precision_score = {}'.format(precision_score(y_true=val_y, y_pred=preds_val)))
print('recall_score = {}'.format(recall_score(y_true=val_y, y_pred=preds_val)))
plt.figure(figsize=(10,7)) 
 
precision, recall, thresholds = precision_recall_curve(val_y, pred_probs_val) 
plt.plot(recall, precision) 
plt.xlabel('Recall') 
plt.ylabel('Precision') 
plt.show()
# Plot the variation in precision and recall with increasing threshold values. 
PR_variation_df = pd.DataFrame({'precision': precision, 'recall': recall}, index=list(thresholds)+[1]) 
 
PR_variation_df.plot(figsize=(10,7)) 
plt.xlabel('Threshold') 
plt.ylabel('P/R values') 
plt.show() 
final_threshold = 0.05<jupyter_output><empty_output><jupyter_text>Predicting on test dataset<jupyter_code>pred_probs_test = np.array([each[1] for each in gbc.predict_proba(X_test)])
preds_test = (pred_probs_test > final_threshold).astype(int)
preds_test<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1572004397215097, 'fraction_numerical': 0.025283986808354707, 'mean_word_length': 7.806451612903226, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '362557', 'license': 'MIT', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1078, 'n_words': 164, 'path': 'Chapter07/Activity7.01/Activity7.01.ipynb', 'repo_name': 'nijinjose/The-Supervised-Learning-Workshop', 'size': '2729'}