"""
## Thyroid Disease 

The most common thyroid disorder is hypothyroidism. Hypo- means deficient or under(active), so hypothyroidism is a condition in which the thyroid gland is underperforming or producing too little thyroid hormone.. Recognizing the symptoms of hypothyroidism is extremely important.





<img src="https://i.imgur.com/5cW3lGZ.gif?noredirect" width="800px">


### Data Set Information:



#### From Garavan Institute
#### Documentation: as given by Ross Quinlan
#### 6 databases from the Garavan Institute in Sydney, Australia
#### Approximately the following for each database:

* 2800 training (data) instances and 972 test instances
* Plenty of missing data
* 29 or so attributes, either Boolean or continuously-valued

#### 2 additional databases, also from Ross Quinlan, are also here

* Hypothyroid.data and sick-euthyroid.data
* Quinlan believes that these databases have been corrupted
* Their format is highly similar to the other databases

#### 1 more database of 9172 instances that cover 20 classes, and a related domain theory
#### Another thyroid database from Stefan Aeberhard

* 3 classes, 215 instances, 5 attributes
* No missing values

#### Dataset link:


##### [Here](https://www.kaggle.com/yasserhessein/thyroid-disease-data-set)
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
import seaborn as sns

%matplotlib inline
sns.set(rc={'figure.figsize': [20, 20]}, font_scale=1.4)
df = pd.read_csv('../input/thyroid-disease-data-set/hypothyroid.csv')
df
df.head()
df.describe().T
df.info()
df
df["binaryClass"].value_counts()
df["binaryClass"]=df["binaryClass"].map({"P":0,"N":1})
df["pregnant"].value_counts()
df=df.replace({"t":1,"f":0})
df
#df['target'].isnull().sum()
df['sex'].isnull().sum()
df["TBG"].value_counts()
del df["TBG"]
df=df.replace({"?":np.NAN})
df.isnull().sum()
df["sex"].value_counts()
df=df.replace({"F":1,"M":0})
df["referral source"].value_counts()
del df["referral source"]
df.info()
df["T3 measured"].value_counts()
df["TT4 measured"].value_counts()
df["FTI measured"].value_counts()
df["TBG measured"].value_counts()
df["binaryClass"].value_counts()
df.dtypes
cols = df.columns[df.dtypes.eq('object')]
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
df.dtypes
df.isnull().sum()
df['T4U measured'].mean()
df['T4U measured'].fillna(df['T4U measured'].mean(), inplace=True)
df['sex'].fillna(df['sex'].mean(), inplace=True)
df['age'].fillna(df['age'].mean(), inplace=True)
from sklearn.impute import SimpleImputer

imputer = SimpleImputer(strategy='mean')
df['TSH'] = imputer.fit_transform(df[['TSH']])
df['T3'] = imputer.fit_transform(df[['T3']])
df['TT4'] = imputer.fit_transform(df[['TT4']])
df['T4U'] = imputer.fit_transform(df[['T4U']])
df['FTI'] = imputer.fit_transform(df[['FTI']])
df.isnull().sum()
df
df.columns
import seaborn as sns

%matplotlib inline
sns.set(rc={'figure.figsize': [8, 8]}, font_scale=1.2)
sns.distplot(df['age'])
sns.distplot(df['sex'])
sns.distplot(df['T3'])
sns.distplot(df['TT4'])
sns.distplot(df['T4U'])
sns.distplot(df['FTI'])
sns.distplot(df['TBG measured'])
sns.jointplot(x='age', y='TT4', data=df, kind='scatter', height=8, color='m')
sns.jointplot(x='age', y='TT4', data=df, kind='reg', height=8, color='m')
sns.distplot(df['age'], kde=False, bins=30, color='m')
sns.countplot(x='binaryClass', data=df, palette='rocket')
sns.countplot(x='binaryClass', data=df, hue='sex', palette='BuPu')
sns.stripplot(x="binaryClass", y="age", data=df, palette="viridis")
sns.boxplot(x='binaryClass', y='age', data=df)
sns.jointplot(x='FTI', y='binaryClass', data=df, kind='scatter', height=8, color='m')
df_corr = df.corr()
df_corr
sns.heatmap(df_corr, cmap='viridis', linecolor='k', linewidths=2, annot=True)
#df.dropna(axis = 0, inplace=True)
x = df.drop('binaryClass', axis=1)
y = df['binaryClass']
x
y
x = sm.add_constant(x)
results = sm.OLS(y,x).fit()
results.summary()
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=42)
x.shape
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
sc.fit(x_train)
x_train = sc.transform(x_train)
x_test = sc.transform(x_test)
"""
<img src="https://lh3.googleusercontent.com/proxy/dmZ-7K8n0tfjlAlmjL316IQymLkEUejTyRJe_PvnpoIvDzoDixx_4L1JmNBYg0xhdyywrCkkjD9F5Snp7PJU8N6RFZ0ke4IO9wMe5c2xbJvC" width="800px">
"""
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping
x.shape
x.shape[1]
model = Sequential()
model.add(Dense(256, input_shape=[x.shape[1]], activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(63, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(1, activation='sigmoid'))
model.summary()
from tensorflow.keras.utils import plot_model
from IPython.display import Image
plot_model(model, to_file='convnet.png', show_shapes=True,show_layer_names=True)
Image(filename='convnet.png') 
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
lrd = ReduceLROnPlateau(monitor = 'val_loss',
                         patience = 20,
                         verbose = 1,
                         factor = 0.75,
                         min_lr = 1e-10)

mcp = ModelCheckpoint('model.h5')

es = EarlyStopping(verbose=1, patience=20)
%%time
history = model.fit(x=x_train, y=y_train, epochs=100, callbacks=[lrd, mcp, es], batch_size=64, validation_split=0.1)

model.evaluate(x_test, y_test)
y_pred = model.predict(x_test)
y_pred
y_pred[1]
y_test
%%time
model.evaluate(x_test, y_test)
model.predict(x_test)[50]
model.predict_classes(x_test)[50]
y_test.iloc[50]
model.predict(x_test)[70]
model.predict_classes(x_test)[70]
y_test.iloc[70]
model.predict(sc.transform([[42.1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.00,132,1.00,1.00,109.0,1.0,0.88,.100,110.00,0.00,0,1]]))
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')

plt.legend(['train', 'test'], loc='upper left')
plt.show()

# # summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')
plt.show()
model.save('model.h5')
import joblib
joblib.dump(sc, 'scaler.pkl')
sc = joblib.load('scaler.pkl')
%%time
from tensorflow.keras.models import load_model
model = load_model('model.h5')
model.predict(sc.transform([[42.1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.00,132,1.00,1.00,109.0,1.0,0.88,.100,110.00,0.00,0,1]]))