CoolFace
Apppublic

UMESH06/Life_Expectancy

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py179 linesDownload Raw Back to root
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