# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import io
import requests
url="https://raw.githubusercontent.com/fbarth/ds-saint-paul/master/data/base_train.csv"
s=requests.get(url).content
X_train = pd.read_csv(io.StringIO(s.decode('utf-8')), sep=",")
X_train = X_train.drop(columns=['Unnamed: 0'])

url="https://raw.githubusercontent.com/fbarth/ds-saint-paul/master/data/base_train_answer.csv"
s=requests.get(url).content
df_diagnosis = pd.read_csv(io.StringIO(s.decode('utf-8')), sep=",")
df_diagnosis = df_diagnosis.drop(columns=['Unnamed: 0'])
y_train = df_diagnosis['diagnosis'].ravel()

# joining info and diagnosis into one df
df_full = pd.concat([df_diagnosis, X_train], axis=1)
df_full.head()
# -

print(X_train.shape)
print(y_train.shape)

# +
import seaborn as sns
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import recall_score, make_scorer
from sklearn.model_selection import cross_val_score, cross_val_predict

# loop to find best number of estimators
min_estimators = 100
max_estimators = 1000
step = 50
result = []
best_score = 0
best_estimator = 0

for i in range(min_estimators, max_estimators+step, step):
    clf = GradientBoostingClassifier(
        n_estimators=i, 
        random_state=0)
    
    s = make_scorer(recall_score, pos_label='M')
    scores = cross_val_score(clf, X_train, y_train, cv=5, scoring=s)
    if (scores.mean() > best_score):
        best_estimator = i
        best_score = scores.mean()
    result.append((i, scores.mean()))
    
# converting result into dataframe
estimators = np.array(result)[:,0]
score = np.array(result)[:,1]
d = {'estimators': estimators, 'score': score}
df_scores = pd.DataFrame(d)
print(f'Best estimator: {best_estimator}')
print(df_scores)

# plotting results
sns.set_theme(style="dark")
sns.set_palette("colorblind")
sns.lineplot(
        data=df_scores,
        x="estimators", 
        y="score"
    )

# best and smallest number of estimators using above loop was 200
# -

clf = GradientBoostingClassifier(
        n_estimators=200,
        random_state=0)
s = make_scorer(recall_score, pos_label='M')
scores = cross_val_score(clf, X_train, y_train, cv=5, scoring=s)
y_pred = cross_val_predict(clf, X_train, y_train, cv=5)
print("recall_score: %0.5f (+/- %0.5f)" % (scores.mean(), scores.std()))

# +
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report, plot_confusion_matrix

print(confusion_matrix(y_train, y_pred))
print(classification_report(y_train, y_pred))
# -



StarcoderdataJupyterScriptsDedupFiltered