<jupyter_start><jupyter_text>Predicting diabetes using machine learning<jupyter_code># Import all the tools we need 

# Regular EDA and plotting libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline 

# Models from Scikit-learn 
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier


#Model Evaluations
from sklearn.model_selection import train_test_split, cross_val_score,RandomizedSearchCV, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report, precision_score, recall_score, f1_score, plot_roc_curve<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>df = pd.read_csv("diabetes.csv")
df
df.shape<jupyter_output><empty_output><jupyter_text>Examining the data further.<jupyter_code>df.head()
df.tail()
df["Outcome"].value_counts()
df["Outcome"].value_counts().plot(kind="bar", color = ["salmon", "lightblue"])
df.info()
df.isna().sum()
df.describe()<jupyter_output><empty_output><jupyter_text>age and Body mass index for diabetes<jupyter_code>#Create another figure
plt.figure(figsize = (10,6))

#Scatter with positive examples
plt.scatter(df.Age[df.Outcome == 1], df.BMI[df.Outcome==1], c="salmon")

#Scatter with negative examples
plt.scatter(df.Age[df.Outcome==0], df.BMI[df.Outcome==0], c = "lightblue")

# Add some helpful info
plt.title("Heart Disease in function of Age and BMI")
plt.xlabel("Age")
plt.ylabel("BMI")
plt.legend(["Disease", "No Disease"])
df.Age.plot.hist()<jupyter_output><empty_output><jupyter_text>Disease frequency per blood pressure<jupyter_code>df.corr()
corr_matrix = df.corr()
fig, ax = plt.subplots(figsize = (15,10))
ax = sns.heatmap(corr_matrix, annot = True, linewidths = 0.5, fmt = ".2f", cmap = "YlGnBu")<jupyter_output><empty_output><jupyter_text>Modelling the data<jupyter_code>df.head()
X = df.drop("Outcome", axis=1)
y = df["Outcome"]
X
y
#Split the data into train and test sets
np.random.seed(42)

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2)
X_train
y_train, len(y_train)<jupyter_output><empty_output><jupyter_text>time to build a machine learning model and see which one best fits. We'll use 6 models, 1. LogisticRegression2. K_NearestNeighborsClassifier3. RandomForestClassifier4. SVM5. Naive Bayes6. Decision Tree<jupyter_code>#put models in a dict
models = {"LogisticRegression": LogisticRegression(), "LinearSVC": LinearSVC(), 
          "Naive Bayes": GaussianNB(), "KNN": KNeighborsClassifier(), "RandomForest": RandomForestClassifier(),
           "Decision Tree": DecisionTreeClassifier()}

#Create a function to fit and score models
def fit_and_score(models, X_train, X_test, y_train, y_test):
    np.random.seed(42)
    model_scores = {}
    
    #loop through models
    for name, model in models.items():
        model.fit(X_train, y_train)
        model_scores[name] = model.score(X_test, y_test)
    return model_scores
model_scores = fit_and_score(models=models, X_train = X_train, X_test = X_test,y_train = y_train, y_test = y_test)
model_scores
model_compare = pd.DataFrame(model_scores, index=["accuracy"])
model_compare.T.plot.bar()<jupyter_output><empty_output><jupyter_text>Hypertune Logistic Regression parameters<jupyter_code>#Create a hyperparameter grid for LogisticRegression
log_reg_grid = {"C": np.logspace(-4, 4, 20),
               "solver": ["liblinear"]}

#Create a hyperparameter grid for RandomForestClassifier
rf_grid = {"n_estimators": np.arange(10,1000,50), "max_depth": [None, 3,5,10], "min_samples_split": np.arange(2,20,2), 
          "min_samples_leaf": np.arange(1,20,2)}
np.logspace(-4, 4, 20)
# Tune LogisticRegressionModel 

np.random.seed(42)

# Setup random hyperparatmer search for LogisticRegression
rs_log_reg = RandomizedSearchCV(LogisticRegression(), param_distributions=log_reg_grid, cv=5,n_iter=20,verbose=True)

#Fit random hyperparameter search model for LogisticRegression
rs_log_reg.fit(X_train, y_train)
rs_log_reg.best_params_
rs_log_reg.score(X_test,y_test)
# Different hyperparanmeters for our LogisticRegression model
log_reg_grid = {"C": np.logspace(-4,4,30), "solver": ["liblinear"]}

#Setup grid hyperparameter search for LogisticRegression
gs_log_reg = GridSearchCV(LogisticRegression(), param_grid=log_reg_grid, cv=5, verbose=True)

#Fit grid hyperparameter search model
gs_log_reg.fit(X_train, y_train)
gs_log_reg.best_params_
gs_log_reg.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Tuning DecisionTree hyperparameters<jupyter_code>#Create a hyperparameter grid for LogisticRegression
param_dist = {"max_depth": [5, None]}
np.logspace(-4, 4, 20)
# Tune Decision Tree

np.random.seed(42)

# Setup random hyperparatmer search for LogisticRegression
rs_log_reg1 = RandomizedSearchCV(DecisionTreeClassifier(), param_dist, cv=5)

#Fit random hyperparameter search model for LogisticRegression
rs_log_reg1.fit(X_train, y_train)
rs_log_reg1.best_params_
rs_log_reg1.score(X_test,y_test)<jupyter_output><empty_output><jupyter_text>Tuning Naive Bayes hyperparameters<jupyter_code>nb_classifier = GaussianNB()

params_NB = {'var_smoothing': np.logspace(0,-9, num=100)}
gs_NB = GridSearchCV(estimator=nb_classifier, 
                 param_grid=params_NB, 
                    # use any cross validation technique 
                 verbose=1, 
                 scoring='accuracy') 
gs_NB.fit(X_train, y_train)

gs_NB.best_params_
gs_NB.score(X_test, y_test)
best_model.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Evaluate our tuned classifier model<jupyter_code>y_preds = rs_log_reg1.predict(X_test)
y_preds
y_test
plot_roc_curve(rs_log_reg1, X_test, y_test)
print(confusion_matrix(y_test, y_preds))
sns.set(font_scale=1.5)

def plot_conf_mat(y_test, y_preds):
    """

    """
    
    fig,ax = plt.subplots(figsize=(3,3))
    ax = sns.heatmap(confusion_matrix(y_test, y_preds),annot=True, cbar=False)
    plt.xlabel("True label")
    plt.ylabel("Predicted label")
    
plot_conf_mat(y_test, y_preds)
print(classification_report(y_test, y_preds))<jupyter_output>precision    recall  f1-score   support

           0       0.81      0.88      0.84        99
           1       0.74      0.64      0.69        55

    accuracy                           0.79       154
   macro avg       0.78      0.76      0.77       154
weighted avg       0.79      0.79      0.79       154<jupyter_text>Doing this same stuff now for LogisticRegression<jupyter_code>y_preds = gs_log_reg.predict(X_test)
y_preds
y_test
plot_roc_curve(gs_log_reg, X_test, y_test)
print(confusion_matrix(y_test, y_preds))
sns.set(font_scale=1.5)

def plot_conf_mat(y_test, y_preds):
    """
    """
    
    fig,ax = plt.subplots(figsize=(3,3))
    ax = sns.heatmap(confusion_matrix(y_test, y_preds),annot=True, cbar=False)
    plt.xlabel("True label")
    plt.ylabel("Predicted label")
    
plot_conf_mat(y_test, y_preds)
print(classification_report(y_test, y_preds))
gs_log_reg.best_params_
clf = LogisticRegression(C=2.592943797404667, solver="liblinear")
cv_acc = cross_val_score(clf, X, y, cv=5, scoring="accuracy")
cv_acc
cv_precision = cross_val_score(clf, X, y, cv=5, scoring="precision")

cv_precision = np.mean(cv_precision)
cv_precision
cv_recall = cross_val_score(clf, X, y, cv=5, scoring="recall")

cv_recall = np.mean(cv_recall)
cv_recall
cv_f1 = cross_val_score(clf, X, y, cv=5, scoring="f1")

cv_f1 = np.mean(cv_f1)
cv_f1<jupyter_output><empty_output><jupyter_text>Feature importance after data modeling<jupyter_code>clf = LogisticRegression(C=2.592943797404667, solver = "liblinear")
clf.fit(X_train, y_train)
df.head()
clf.coef_
feature_dict = dict(zip(df.columns, list(clf.coef_[0])))
feature_dict
feature_df = pd.DataFrame(feature_dict, index = [0])
feature_df.T.plot.bar(title = "Feature Importance", legend=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11417910447761194, 'fraction_numerical': 0.02400497512437811, 'mean_word_length': 5.629018961253092, 'pattern_counts': {'":': 15, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '10043', 'license': 'MIT', 'n_tokens_mistral': 3121, 'n_tokens_neox': 2950, 'n_words': 579, 'path': 'diabetesProject/DiabetesProjectMachineLearning.ipynb', 'repo_name': 'NeelApte/Diabetes', 'size': '8040'}