import streamlit as st
import numpy as np
import pickle
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Load Model
model = pickle.load(open("life_expectancy_model.pkl", "rb"))
# Set Page Config
st.set_page_config(
page_title="Life Expectancy Prediction",
page_icon="📊",
layout="centered",
)
# Styling
st.markdown("""
""", unsafe_allow_html=True)
# Navigation State
if "current_page" not in st.session_state:
st.session_state.current_page = "Model Report"
# Function to switch pages
def switch_page(page):
st.session_state.current_page = page
# Sidebar Navigation
st.sidebar.title("Navigation")
if st.sidebar.button("Model Report"):
switch_page("Model Report")
if st.sidebar.button("Hands-on Model"):
switch_page("Hands-on Model")
## Importing Data
data = pd.read_csv("Life Expectancy Data.csv")
data.columns = data.columns.str.strip()
# Model Report Page
if st.session_state.current_page == "Model Report":
st.markdown("
Model Report
", unsafe_allow_html=True)
st.image("images/Life_Expectanccy.webp",
caption="Life Expectancy Prediction Overview",
use_container_width=True)
st.markdown("Explore different stages of the Life Expectancy project
", unsafe_allow_html=True)
if st.button("â–¶ **Problem Statement**"):
switch_page("Problem Statement")
if st.button("â–¶ **Data Collection**"):
switch_page("Data Collection")
if st.button("â–¶ **Simple EDA**"):
switch_page("Simple EDA")
if st.button("â–¶ **Data Pre-processing**"):
switch_page("Data Pre-processing")
if st.button("â–¶ **Exploratory Data Analysis**"):
switch_page("EDA")
if st.button("â–¶ **Model Building**"):
switch_page("Model Building")
if st.button("â–¶ **Final Model**"):
switch_page("Final Model")
# Individual Sections
elif st.session_state.current_page == "Problem Statement":
st.markdown("Problem Statement
", unsafe_allow_html=True)
st.markdown("""
The Goal of this project is to build a predictive model that estimates the Life Expectancy of a country
based on multiple influencing factors such as health indicators, economic conditions, and social parameters.
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
st.image("images/problem_statement.png",
caption="Life Expectancy Prediction Overview",
use_container_width=True)
if st.button("🔙 Go Back to Model Report"):
switch_page("Model Report")
elif st.session_state.current_page == "Data Collection":
st.markdown("Data Collection
", unsafe_allow_html=True)
st.markdown("""
The dataset used in this project is sourced from Kaggle, containing information on life expectancy across
different countries along with various health, economic, and demographic factors.
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
st.markdown("""
""", unsafe_allow_html=True)
st.markdown("Dataset Overview
", unsafe_allow_html=True)
st.markdown("""
The dataset consists of 2938 rows and 22 columns, capturing crucial indicators such as life expectancy,
mortality rates, GDP, schooling, immunization rates, and more. Below is a summary of the dataset features:
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
data_info = """
• Country: Name of the country (Categorical)
• Year: Year of observation (Numerical)
• Status: Developing or Developed country (Categorical)
• Life Expectancy: Average age a person is expected to live (Numerical)
• Adult Mortality: Probability of dying between 15-60 years per 1000 population (Numerical)
• Infant Deaths: Number of infant deaths per 1000 live births (Numerical)
• Alcohol: Alcohol consumption per capita (Numerical)
• Percentage Expenditure: Government expenditure on health as a percentage of GDP (Numerical)
• Hepatitis B: Immunization coverage for Hepatitis B (Numerical)
• Measles: Number of reported measles cases per year (Numerical)
• BMI: Average Body Mass Index of the population (Numerical)
• Under-five Deaths: Number of deaths under the age of five per 1000 live births (Numerical)
• Polio: Immunization coverage for Polio (Numerical)
• Total Expenditure: Total health expenditure as a percentage of GDP (Numerical)
• Diphtheria: Immunization coverage for Diphtheria (Numerical)
• HIV/AIDS: Death rate due to HIV/AIDS per 100,000 people (Numerical)
• GDP: Gross Domestic Product per capita (Numerical)
• Population: Total population of the country (Numerical)
• Thinness 1-19 Years: Percentage of thin individuals aged 1-19 years (Numerical)
• Thinness 5-9 Years: Percentage of thin individuals aged 5-9 years (Numerical)
• Income Composition: Human development index based on income composition (Numerical)
• Schooling: Average number of years of schooling (Numerical)
"""
st.markdown(data_info, unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
if st.button("🔙 Go Back to Model Report"):
switch_page("Model Report")
elif st.session_state.current_page == "Simple EDA":
st.markdown("Simple Exploratory Data Analysis
", unsafe_allow_html=True)
st.markdown("""
Exploratory Data Analysis (EDA) helps in understanding the structure, patterns, and missing values in the dataset.
Below is an initial preview of the data, followed by a missing values summary.
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
# Display dataset sample
st.markdown("Sample Dataset
", unsafe_allow_html=True)
st.dataframe(data.head())
st.markdown("
", unsafe_allow_html=True)
# Display missing values count
st.markdown("Missing Values Summary
", unsafe_allow_html=True)
missing_values = data.isna().sum().reset_index()
missing_values.columns = ["Column Name", "Missing Values"]
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
st.dataframe(missing_values)
st.markdown("
", unsafe_allow_html=True)
# Display missing values count
st.markdown("Data Description
", unsafe_allow_html=True)
st.dataframe(data.describe())
st.markdown("
", unsafe_allow_html=True)
# Add Boxplot Visualizations
st.markdown("Boxplots for Data Distribution
", unsafe_allow_html=True)
# Define columns for visualization
columns = ['Life expectancy', 'Adult Mortality',
'infant deaths', 'Alcohol', 'percentage expenditure', 'Hepatitis B',
'Measles', 'BMI', 'under-five deaths', 'Polio', 'Total expenditure',
'Diphtheria', 'HIV/AIDS', 'GDP', 'Population', 'thinness 1-19 years',
'thinness 5-9 years', 'Income composition of resources', 'Schooling']
# Matplotlib figure setup
fig, axes = plt.subplots(nrows=10, ncols=2, figsize=(12, 30)) # Adjust grid size
axes = axes.flatten()
for i, col in enumerate(columns):
sns.boxplot(x=data[col], ax=axes[i], color="skyblue") # Create boxplots
axes[i].set_title(f'Boxplot of {col}', fontsize=12)
axes[i].set_xlabel("")
plt.tight_layout()
st.pyplot(fig)
st.markdown("
", unsafe_allow_html=True)
if st.button("🔙 Go Back to Model Report"):
switch_page("Model Report")
# Hands-on Model Page
elif st.session_state.current_page == "Hands-on Model":
st.title("Hands-on Model")
st.write("Provide inputs to predict Life Expectancy.")
col1, col2 = st.columns(2)
with col1:
year = st.slider("Year", 2000, 2015, 2008)
status = st.radio("Status", ["Developing", "Developed"], horizontal=True)
status = 1 if status == "Developed" else 0
adult_mortality = st.slider("Adult Mortality Rate", 1, 723, 144)
infant_deaths = st.slider("Infant Deaths", 0, 1800, 3)
alcohol = st.slider("Alcohol Consumption", 0.01, 17.87, 4.55)
percentage_expenditure = st.slider("Percentage Expenditure", 0.0, 19479.91, 738.25)
hepatitis_b = st.slider("Hepatitis B Immunization (%)", 1, 99, 83)
measles = st.slider("Measles Cases", 0, 212183, 2419)
bmi = st.slider("BMI", 1.0, 87.3, 38.3)
polio = st.slider("Polio Immunization (%)", 3, 99, 82)
with col2:
under_five_deaths = st.slider("Under-Five Deaths", 0, 2500, 4)
total_expenditure = st.slider("Total Healthcare Expenditure (%)", 0.37, 17.6, 5.92)
diphtheria = st.slider("Diphtheria Immunization (%)", 2, 99, 82)
hiv_aids = st.slider("HIV/AIDS Prevalence Rate", 0.1, 50.6, 1.74)
gdp = st.slider("GDP per Capita", 1.68, 119172.7, 6611.52)
population = st.slider("Population", 34, 1293859000, 10230850)
thinness_1_19 = st.slider("Thinness 1-19 years (%)", 0.1, 27.7, 4.83)
thinness_5_9 = st.slider("Thinness 5-9 years (%)", 0.1, 28.6, 4.86)
income_composition = st.slider("Income Composition of Resources", 0.0, 0.948, 0.63)
schooling = st.slider("Schooling (Years)", 0.0, 20.7, 11.99)
if st.button("Predict Life Expectancy"):
features = np.array([[year, status, adult_mortality, infant_deaths, alcohol, percentage_expenditure,
hepatitis_b, measles, bmi, under_five_deaths, polio, total_expenditure,
diphtheria, hiv_aids, gdp, population, thinness_1_19, thinness_5_9,
income_composition, schooling]])
prediction = model.predict(features)[0]
st.markdown(
f"""
Predicted Life Expectancy: {prediction:.2f} years
""",
unsafe_allow_html=True,
)
if st.button("⬅ **Back to Model Report**"):
switch_page("Model Report")