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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/krishnaaxo/Thyroid-AI/blob/main/ThyroidFinalFinal.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="hRRMD6uBc6VZ"
import numpy as np
import pandas as pd
import scipy as sc
import seaborn as sns
import warnings
import matplotlib.pyplot as plt
from scipy import stats
# %matplotlib inline

from sklearn import metrics
from sklearn import preprocessing


warnings.filterwarnings('ignore')

# + colab={"base_uri": "https://localhost:8080/", "height": 214} id="8fZYsF7Gdx9_" outputId="f7684f4b-beac-40ff-b1c2-7fdca20d1146"
dataset = pd.read_csv('full_dataset.csv')
dataset.head()

# + id="MMij-_pfdyFE"

X = dataset.iloc[:, 0:-1].values
y = dataset.iloc[:, -1].values

# + colab={"base_uri": "https://localhost:8080/"} id="A2LBPUVudyIy" outputId="5cccac26-508d-4c16-9b60-01464fb5e512"
dataset.info()

# + colab={"base_uri": "https://localhost:8080/", "height": 302} id="oBi9gu5pdyMS" outputId="f0a79ba3-6c6b-42ab-d61a-f9fd6cd412f2"
dataset.describe()

# + colab={"base_uri": "https://localhost:8080/"} id="r43dq7dZepii" outputId="8ae62829-fdbb-4415-ddde-09af1c15d0d0"
dataset.isnull().sum()

# + colab={"base_uri": "https://localhost:8080/", "height": 501} id="W5LZYGxtigjz" outputId="53b5cec3-5442-49df-bb93-82e88cfae9d5"
plt.figure(figsize=(12, 6))
sns.heatmap(dataset.isnull())
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="8No4aOgujCm3" outputId="bfbe7406-d78f-4777-c3b5-960e484d6595"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
print(X_train)
print(y_train)
print(X_test)
print(y_test)

# + colab={"base_uri": "https://localhost:8080/"} id="b7bE3j42jI_3" outputId="34b04512-0a76-4257-c41b-daebf2aa0a0b"
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="GmkxTBTujMYm" outputId="1c76e588-460a-41c1-e517-0a9d1e0abbda"
from sklearn.linear_model import LogisticRegression
classifier1 = LogisticRegression(random_state = 0)
classifier1.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="_rS1a3RIjX52" outputId="f1134b56-8204-496a-ef4a-810fa302bcfa"
y_pred1 = classifier1.predict(X_test)
print(np.concatenate((y_pred1.reshape(len(y_pred1),1), y_test.reshape(len(y_test),1)),1))

# + colab={"base_uri": "https://localhost:8080/"} id="1RId4mQujbRO" outputId="f1a3a334-156d-4603-a442-5ed6c81c3978"
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred1)
print(cm)
a=accuracy_score(y_test, y_pred1)
a


# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="mEnlsNg1kHrq" outputId="099a76af-774d-4702-e13d-17a2093b43e5"
sns.heatmap(cm, annot=True)

# + colab={"base_uri": "https://localhost:8080/"} id="ajk8ZFaKlSCJ" outputId="59feec2a-4c22-4882-9ec8-2278ad509572"
from sklearn.neighbors import KNeighborsClassifier
classifier2 = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
classifier2.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="w-xQgK7RlR-j" outputId="41d617df-f75a-41c4-9a00-ed2361fbbbf8"
y_pred2 = classifier2.predict(X_test)
print(np.concatenate((y_pred2.reshape(len(y_pred2),1), y_test.reshape(len(y_test),1)),1))

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="7LiKJyJ4mU4g" outputId="4d232615-53a5-4313-a9a2-dac53c8c7731"
from sklearn.metrics import confusion_matrix, accuracy_score
cm2 = confusion_matrix(y_test, y_pred2)
sns.heatmap(cm2, annot=True)
b=accuracy_score(y_test, y_pred2)
b

# + colab={"base_uri": "https://localhost:8080/"} id="cLQZoxwGmiky" outputId="c5ea1c55-bdc2-4b3f-fe6a-f311f28d70e5"
from sklearn.svm import SVC
classifier3 = SVC(kernel = 'rbf', random_state = 0)
classifier3.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="02Chg8wYmiZG" outputId="c43e27a2-f18c-473b-831f-2b752121e6b4"
y_pred3 = classifier3.predict(X_test)
print(np.concatenate((y_pred3.reshape(len(y_pred3),1), y_test.reshape(len(y_test),1)),1))

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="zhdvt-kum8ZA" outputId="c05637f9-74a8-4a87-c5d3-e22a4c4cb9da"
from sklearn.metrics import confusion_matrix, accuracy_score
cm3 = confusion_matrix(y_test, y_pred3)
sns.heatmap(cm3, annot=True)
c=accuracy_score(y_test, y_pred3)
c

# + colab={"base_uri": "https://localhost:8080/"} id="iI39TKOUnO1k" outputId="e6129c95-14dc-4f65-e773-a6beb0096039"
from sklearn.ensemble import RandomForestClassifier
classifier4 = RandomForestClassifier(n_estimators = 2, criterion = 'entropy', random_state = 0)
classifier4.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/"} id="MiTT2p5MnOvU" outputId="6af2aaae-c04a-43ac-8420-655f05fcf237"
y_pred4 = classifier4.predict(X_test)
print(np.concatenate((y_pred4.reshape(len(y_pred4),1), y_test.reshape(len(y_test),1)),1))

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="cvkG8vIJnOmi" outputId="d9a6a9d5-15e7-4f29-d1c2-bb06019bd976"
from sklearn.metrics import confusion_matrix, accuracy_score
cm4 = confusion_matrix(y_test, y_pred4)
sns.heatmap(cm4, annot=True)
d=accuracy_score(y_test, y_pred4)
print(d)

# + colab={"base_uri": "https://localhost:8080/"} id="BnRJhiSWn5yn" outputId="e60914b9-a8d0-408a-baa8-616d51a268b9"
from sklearn.naive_bayes import GaussianNB
classifier5 = GaussianNB()
classifier5.fit(X_train, y_train)

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="lS7Cq0ZkpXqb" outputId="50340b4c-207d-42ba-ae07-15f114268135"
from sklearn.metrics import confusion_matrix, accuracy_score
y_pred5 = classifier5.predict(X_test)
cm5 = confusion_matrix(y_test, y_pred5)
sns.heatmap(cm5,annot=True)
e=accuracy_score(y_test, y_pred5)
e

# + colab={"base_uri": "https://localhost:8080/", "height": 194} id="eQr9zuzZpyO7" outputId="7a7d7973-6668-4436-ec89-6a019b212975"
data = [a,b,c,d,e]
index = ['Logistic Regression', 'KNeighborsClassifier', 'Kernel SVM', 'RandomForestClassifier', 'Naive Bayes']
pd.DataFrame(data, index=index, columns=['Scores']).sort_values(ascending = False, by=['Scores'])

# + id="yuYcGiAhBRs8"


# + id="LhmkJq1FAF4X"
from imblearn.over_sampling import RandomOverSampler

# + id="fKGbbKzQAFuq"
os=RandomOverSampler(0.75)
X_train_ns,y_train_ns=os.fit_sample(X_train,y_train)


# + colab={"base_uri": "https://localhost:8080/"} id="zizHizCoxfEU" outputId="e0744653-c9c4-47b4-c69b-c1666bf49c9f"
pip install -U keras-tuner

# + id="2d66Oy74sHgm"
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch


# + id="9TJ0GzZjsj_c"
def build_model(hp):
    model = keras.Sequential()
    for i in range(hp.Int('num_layers', 2, 20)):
        model.add(layers.Dense(units=hp.Int('units_' + str(i),
                                            min_value=32,
                                            max_value=512,
                                            step=32),
                               activation='relu'))
    model.add(layers.Dense(1, activation='sigmoid'))
    model.compile(
        optimizer=keras.optimizers.Adam(
            hp.Choice('learning_rate', [1e-2, 1e-3, 1e-4])),
        loss='binary_crossentropy',
        metrics=['accuracy'])
    return model


# + colab={"base_uri": "https://localhost:8080/"} id="mNj6O3WFsjs1" outputId="07ee3c62-dbe9-4c8d-f24c-dfac956c64df"
tuner = RandomSearch(
    build_model,
    objective='val_accuracy',
    max_trials=5,
    executions_per_trial=2,
    directory='project',
    project_name='Thyroid')

# + colab={"base_uri": "https://localhost:8080/"} id="n5M3pqI_xhSr" outputId="ebc93808-9a65-42e3-beb1-a9da10da82d5"
tuner.search_space_summary()

# + colab={"base_uri": "https://localhost:8080/"} id="Fc9o5zBjyErp" outputId="228b33e4-2c38-4769-f763-9d9fa45715a3"
tuner.search(X_train_ns, y_train_ns,
             epochs=2,
             validation_data=(X_test, y_test))

# + colab={"base_uri": "https://localhost:8080/"} id="-mOItkVwyHE9" outputId="0568587b-347d-43f9-c0a5-7752ca59c4ef"
tuner.results_summary()

# + id="FTAjZe6VMfvv"
import numpy as np
import pandas as pd
import scipy as sc
import seaborn as sns
import warnings
import matplotlib.pyplot as plt
from scipy import stats
# %matplotlib inline
import tensorflow as tf

from sklearn import metrics
from sklearn import preprocessing


warnings.filterwarnings('ignore')

# + id="269_F2uvMfmT" colab={"base_uri": "https://localhost:8080/", "height": 214} outputId="e984412a-8153-430e-f0ee-d8d901de41cc"
dataset = pd.read_csv('full_dataset.csv')
dataset.head()

# + id="EAbz55Xitx3Z"
X = dataset.iloc[:, 0:-1].values
y = dataset.iloc[:, -1].values

# + id="jCeZK4-Tt2A_"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# + id="XZ6ML4cpt5V4"
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# + colab={"base_uri": "https://localhost:8080/"} id="gGUpaYVB1sWy" outputId="58fc5c48-5830-406f-bfae-b996cfcd8961"
from collections import Counter
from imblearn.over_sampling import RandomOverSampler
os=RandomOverSampler(1)
X_train_ns,y_train_ns=os.fit_sample(X_train,y_train)
print("The number of classes before fit {}".format(Counter(y_train)))
print("The number of classes after fit {}".format(Counter(y_train_ns)))

# + id="Z1vdjghPuBgK"
ann = tf.keras.models.Sequential()

# + id="SShtZChUuGFA"
ann.add(tf.keras.layers.Dense(units=32, kernel_initializer = 'he_uniform',activation='relu'))

# + id="gR4V9Al2uF47"
ann.add(tf.keras.layers.Dense(units=400, kernel_initializer = 'he_uniform',activation='relu'))

# + id="3C21oYw_ue_9"
ann.add(tf.keras.layers.Dense(units=500, kernel_initializer = 'he_uniform',activation='relu'))

# + id="IgjAb4SD-ElX"
ann.add(tf.keras.layers.Dense(units=400, kernel_initializer = 'he_uniform',activation='relu'))

# + id="YtAuIzyF2HKI"
ann.add(tf.keras.layers.Dense(units=400, kernel_initializer = 'he_uniform',activation='relu'))

# + id="10vk8r0dulII"
ann.add(tf.keras.layers.Dense(units=400, kernel_initializer = 'he_uniform',activation='relu'))

# + id="jCnNGxczutlV"
ann.add(tf.keras.layers.Dense(units=1,kernel_initializer = 'glorot_uniform', activation='sigmoid'))

# + id="HBrXBLqIuw5D"
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="9nkRSJO3wI8V" outputId="de1fffde-2958-48c7-ada6-10f4c7becc82"
model_history=ann.fit(X_train_ns, y_train_ns, batch_size = 32, epochs = 300)

# + id="RJsvFrIRvp4j"
y_pred7 = ann.predict_classes(X_test)

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="dneCVJRUvswZ" outputId="6ace1bb9-bdbe-4705-8702-d95c7848f289"
from sklearn.metrics import confusion_matrix, accuracy_score
cm6 = confusion_matrix(y_test, y_pred7)
sns.heatmap(cm6,annot=True)
acc9=accuracy_score(y_test, y_pred7)
acc9

# + colab={"base_uri": "https://localhost:8080/", "height": 223} id="jW-KM0eH3JkX" outputId="616c05d0-b631-4ce1-b843-a24b96332700"
data = [a,b,c,d,e,acc9]
index = ['Logistic Regression', 'KNeighborsClassifier', 'Kernel SVM', 'RandomForestClassifier', 'Naive Bayes','ANN']
pd.DataFrame(data, index=index, columns=['Scores']).sort_values(ascending = False, by=['Scores'])

# + colab={"base_uri": "https://localhost:8080/", "height": 295} id="vWdHAQ35DzgO" outputId="51e7ec7c-09b1-4509-b823-06b38aa860d8"
plt.plot(model_history.history['accuracy'])

plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

StarcoderdataJupyterScriptsDedupFiltered