# ---
# 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
# ---

# + id="K4wbhulGsGww"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import zipfile

# + id="flSBIZl2sm7k"
zip = zipfile.ZipFile('archive.zip')
zip.extractall()

# + colab={"base_uri": "https://localhost:8080/", "height": 473} id="U3TMw-qbspFR" outputId="b42d3952-dbad-4cdd-a7ab-1eebcfdfe07f"
df=pd.read_csv("Life Expectancy Data.csv")
df['country']=df['Country']
df['GDP']=round(df['GDP'])
df['Alcohol']=round(df['Alcohol'])
df[' BMI ']=round(df[' BMI '])
df[' HIV/AIDS']=round(df[' HIV/AIDS'])
df['Schooling']=round(df['Schooling'])
df[' thinness  1-19 years']=round(df[' thinness  1-19 years'])
df[' thinness 5-9 years']=round(df[' thinness 5-9 years'])
df=df.drop(columns=['Year','Country','Income composition of resources'])
df=df.dropna(how='any')
df

# + colab={"base_uri": "https://localhost:8080/"} id="_wETuFO0tCqa" outputId="257353d8-e156-4e4c-baf2-10b04b7aa366"
df['country'].unique()

# + colab={"base_uri": "https://localhost:8080/", "height": 473} id="oqsIuQiYtFvK" outputId="32bbcc49-9dcd-49b0-c033-39eeada1b717"
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
le1=LabelEncoder()
df['Status']=le.fit_transform(df['Status'])
df['country']=le1.fit_transform(df['country'])
df

# + id="a3mc9MdktQ0O"
X=df.iloc[:,:-1].values
y=df.iloc[:,-1].values

# + colab={"base_uri": "https://localhost:8080/"} id="ohGiLM_dtUyt" outputId="7ca23181-a1fc-431c-fbb0-f129f5357e66"
X[:,15:16]

# + id="wcsq6l-xtW5v"
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2 ,random_state = 1)

# + id="gFmmZQcbtY4T"
from sklearn.preprocessing import StandardScaler
norm = StandardScaler()
X_train[:,15:16] = norm.fit_transform(X_train[:, 15:16])
X_test[:, 15:16] = norm.fit_transform(X_test[:, 15:16])

# + colab={"base_uri": "https://localhost:8080/"} id="yNEUNmR4ta_h" outputId="4263bba2-4f6a-4741-ba41-347f94690b99"
X_train[:,15:16]

# + id="82IOQWrfumLC"
y_train=tf.keras.utils.to_categorical(y_train)
y_test=tf.keras.utils.to_categorical(y_test)

# + id="c7eai0B4tfHv"
import tensorflow as tf
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=500, activation='relu'))
ann.add(tf.keras.layers.Dense(units=500, activation='relu'))
ann.add(tf.keras.layers.Dense(units=500, activation='relu'))
ann.add(tf.keras.layers.Dense(units=500, activation='relu'))
ann.add(tf.keras.layers.Dense(units=500, activation='relu'))
ann.add(tf.keras.layers.Dense(units=133,activation='softmax'))

# + colab={"base_uri": "https://localhost:8080/"} id="EO7WZBNBtjEi" outputId="05028490-72a7-45dc-ab9c-59f8707dec35"
ann.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = ann.fit(X_train, y_train, batch_size=32, epochs=1000)
ann.save("LEC.h5")

# + colab={"base_uri": "https://localhost:8080/", "height": 573} id="h0vAyF-1tkcC" outputId="54299462-e260-4a8f-fca7-d40cff5c74b4"
plt.figure(0)
plt.plot(history.history['accuracy'], label='training accuracy')
plt.title('Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.title('Results for ANN training-1')
plt.savefig('Accuracy.png')

plt.figure(1)
plt.plot(history.history['loss'], label='training loss')
plt.title('Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.title('Results for ANN training-1')
plt.savefig('Loss.png')

# + colab={"base_uri": "https://localhost:8080/"} id="wV3WntsouXDn" outputId="a73b3060-db67-455c-d461-6b7d69a230a8"
model = tf.keras.models.load_model('LEC.h5')
print("Loaded model from disk")

# + colab={"base_uri": "https://localhost:8080/"} id="HmFrplWvubrX" outputId="efa09709-322f-41ac-8b73-f1e51db86992"
y_pred= model.predict(X_test)
y_pred=np.round(y_pred)
np.set_printoptions(precision=2)
print(y_pred)

# + colab={"base_uri": "https://localhost:8080/"} id="_61QkjfIuiY2" outputId="8aa0ebf6-96a1-4952-9d23-2fd6cef06d3d"
from sklearn.metrics import accuracy_score
print("Accuracy Score for the algorithm=>{}%".format(round(accuracy_score(y_test,y_pred)*100),2))

StarcoderdataJupyterScriptsDedupFiltered