UMESH06/Life_Expectancy
0
1import streamlit as st
2import pandas as pd
3import pickle
4
5st.set_page_config(page_title="Life Expectancy App", layout="wide")
6
7# Sidebar Navigation using radio buttons
8st.sidebar.title("๐ Life Expectancy App")
9selected = st.sidebar.radio("Go to", [
10 "๐ Introduction",
11 "๐ Problem Statement",
12 "๐ Data Understanding",
13 "๐ EDA",
14 "๐ ๏ธ Feature Engineering",
15 "โ๏ธ Model Building",
16 "๐ Predict"
17])
18
19# Page: Introduction
20if selected == "๐ Introduction":
21 st.title("๐ฅ Life Expectancy Prediction App")
22 st.markdown("""
23 ### Welcome to the Life Expectancy Prediction Project!
24
25 #### ๐ Project Highlights:
26 - Built using **Python**, **Pandas**, **Scikit-learn**, and **Streamlit**
27 - Machine Learning Model: **K-Nearest Neighbors (KNN) Regressor**
28 - Dataset sourced from **World Health Organization (WHO)** (2000โ2015)
29
30 #### ๐ฏ Objectives:
31 - Predict life expectancy based on socio-economic and health indicators
32 - Enable data-driven decision making in public health and governance
33
34 #### ๐จโ๐ป Created by:
35 **Bandi Umesh Chandra**, UG Student
36 Gokaraju Rangaraju Institute of Engineering and Technology
37 """)
38
39# Page: Problem Statement
40elif selected == "๐ Problem Statement":
41 st.title("๐ Problem Statement")
42 st.markdown("""
43 Life expectancy is a key indicator of public health and socio-economic development.
44
45 #### โ ๏ธ Key Challenges:
46 - Influenced by multiple complex factors like income, education, health expenditure, and disease prevalence
47 - Need to handle missing data, noisy trends, and temporal changes
48
49 #### ๐ง Project Goal:
50 - Build a predictive model using historical health data
51 - Help health policymakers focus on impactful factors to improve lifespan
52 """)
53
54# Page: Data Understanding
55elif selected == "๐ Data Understanding":
56 st.title("๐ Data Understanding")
57 st.markdown("""
58 - **Source**: WHO via Kaggle
59 - **Time Frame**: 2000โ2015
60 - **Size**: ~3,000 records across 193 countries
61
62 #### ๐ Key Variables:
63 - **Country**: Nation-wise grouping
64 - **Status**: Developed vs. Developing
65 - **Health Metrics**: Adult mortality, infant deaths, immunization rates
66 - **Economic Factors**: GDP, income composition, total health expenditure
67 - **Demographics**: Population, schooling, thinness indicators
68
69 These variables provide a comprehensive snapshot of public health and infrastructure.
70 """)
71
72# Page: EDA
73elif selected == "๐ EDA":
74 st.title("๐ Exploratory Data Analysis")
75 st.markdown("""
76 Exploratory Data Analysis helps uncover patterns, correlations, and outliers in the dataset.
77
78 #### ๐ Observations:
79 - **Positive Correlations**:
80 - Schooling, Income Composition, and GDP positively affect life expectancy
81 - **Negative Correlations**:
82 - HIV/AIDS, adult mortality, and infant deaths lower life expectancy
83 - **Trend**:
84 - Developed countries consistently show higher life expectancy due to better healthcare and education systems
85
86 #### ๐ Data Cleaning Insights:
87 - Handled null values via mean/median imputation
88 - Outliers examined using boxplots
89 - Categorical variables analyzed by group-wise aggregations
90 """)
91
92# Page: Feature Engineering
93elif selected == "๐ ๏ธ Feature Engineering":
94 st.title("๐ ๏ธ Feature Engineering")
95 st.markdown("""
96 Feature engineering is critical to transform raw data into informative inputs for ML models.
97
98 #### โ๏ธ Techniques Used:
99 - **Encoding**:
100 - One-Hot Encoding for 'Country' and 'Status'
101 - **Scaling**:
102 - Applied StandardScaler to normalize feature ranges
103 - **Feature Selection**:
104 - Based on correlation heatmaps and model importance ranking
105
106 #### ๐ฏ Final Features:
107 - Adult Mortality, BMI, GDP, Schooling, Alcohol, HIV/AIDS, and Immunization rates
108 """)
109
110# Page: Model Building
111elif selected == "โ๏ธ Model Building":
112 st.title("โ๏ธ Model Building")
113 st.markdown("""
114 Machine Learning helps predict outcomes using historical data trends.
115
116 #### ๐ค Model: K-Nearest Neighbors (KNN) Regressor
117 - **Why KNN?** Simple, intuitive, works well for small datasets
118 - **Hyperparameter Tuning**:
119 - Used GridSearchCV to find optimal number of neighbors (k)
120 - **Evaluation Metrics**:
121 - **RMSE** (Root Mean Squared Error): Measures prediction error
122 - **Rยฒ Score**: Indicates how well the model explains variation in data
123
124 #### ๐งพ Final Model:
125 - Saved as: `knn_life_expectancy_model.pkl`
126 """)
127
128# Page: Prediction
129elif selected == "๐ Predict":
130 st.title("๐ Predict Life Expectancy")
131
132 st.markdown("""
133 #### ๐งช Enter input values below to predict life expectancy using the trained model.
134 - The values should be realistic and lie within the historical range (2000โ2015)
135 - The prediction is based on patterns learned from past WHO data
136 """)
137
138 # Input Widgets
139 Country = st.selectbox("๐ Country", ["Afghanistan", "India", "Brazil", "United States", "Nigeria", "Canada", "France"])
140 Status = st.radio("๐ท๏ธ Development Status", ["Developed", "Developing"])
141 Year = st.slider("๐
Year", 2000, 2015)
142 Adult_Mortality = st.slider("โฐ๏ธ Adult Mortality (per 1000)", 0, 1000)
143 infant_deaths = st.slider("๐ถ Infant Deaths", 0, 500)
144 Alcohol = st.slider("๐บ Alcohol (litres per capita)", 0.0, 20.0)
145 percentage_expenditure = st.slider("๐ฐ Health Expenditure (% of GDP)", 0.0, 10000.0)
146 Hepatitis_B = st.slider("๐ Hepatitis B Immunization (%)", 0, 100)
147 Measles = st.slider("๐ค Measles Cases", 0, 10000)
148 BMI = st.slider("โ๏ธ BMI", 10.0, 50.0)
149 under_five_deaths = st.slider("๐ธ Under-5 Deaths", 0, 500)
150 Polio = st.slider("๐ Polio Immunization (%)", 0, 100)
151 Total_expenditure = st.slider("๐ฅ Total Health Expenditure (% of GDP)", 0.0, 20.0)
152 Diphtheria = st.slider("๐ Diphtheria Immunization (%)", 0, 100)
153 HIV_AIDS = st.slider("๐ฆ HIV/AIDS Impact", 0.0, 5.0)
154 GDP = st.slider("๐ต GDP per Capita (US$)", 0.0, 100000.0)
155 Population = st.slider("๐ฅ Population", 100000.0, 1500000000.0)
156 thinness_1_19 = st.slider("๐ฝ๏ธ Thinness 1-19 (%)", 0.0, 50.0)
157 thinness_5_9 = st.slider("๐ฝ๏ธ Thinness 5-9 (%)", 0.0, 50.0)
158 Income_composition = st.slider("๐ Income Composition (0-1)", 0.0, 1.0)
159 Schooling = st.slider("๐ Schooling (Years)", 0.0, 20.0)
160
161 # Prediction
162 if st.button("๐ Predict Life Expectancy"):
163 with open("knn_life_expectancy_model.pkl", "rb") as f:
164 model = pickle.load(f)
165
166 input_df = pd.DataFrame([[Country, Status, Year, Adult_Mortality, infant_deaths, Alcohol,
167 percentage_expenditure, Hepatitis_B, Measles, BMI, under_five_deaths,
168 Polio, Total_expenditure, Diphtheria, HIV_AIDS, GDP, Population,
169 thinness_1_19, thinness_5_9, Income_composition, Schooling]],
170 columns=['Country', 'Status', 'Year', 'Adult Mortality', 'infant deaths', 'Alcohol',
171 'percentage expenditure', 'Hepatitis B', 'Measles', 'BMI', 'under-five deaths',
172 'Polio', 'Total expenditure', 'Diphtheria', 'HIV/AIDS', 'GDP', 'Population',
173 'thinness 1-19 years', 'thinness 5-9 years',
174 'Income composition of resources', 'Schooling'])
175
176 prediction = model.predict(input_df)[0]
177 st.success(f"๐ Predicted Life Expectancy: **{prediction:.2f} years**")
178 st.caption("Based on WHO dataset trends (2000โ2015)")
179 