File size: 32,274 Bytes
ca055b7 859cf09 ca055b7 94cc85e ca055b7 94cc85e 24617e8 94cc85e 4ee561a 94cc85e 8a3fed5 94cc85e 0063153 01de340 0063153 4ee561a 0063153 94cc85e 8a3fed5 94cc85e 8a3fed5 0063153 4ee561a 94cc85e 8a3fed5 5fc6c25 94cc85e c10f08f 4863220 c10f08f 904478a dbf472e 8a3fed5 94cc85e c10f08f 739e288 42321c9 739e288 94cc85e dbf472e d00b29c 3bf0541 291eb58 232c59f b81200d aaa0909 b81200d 3bf0541 94cc85e a5dc826 94cc85e a5dc826 94cc85e a5dc826 94cc85e a5dc826 94cc85e a5dc826 94cc85e a5dc826 94cc85e a5dc826 94cc85e 56f8561 bb93eb8 7b920f5 091462b 291eb58 ef1891d 291eb58 3a4b99d 291eb58 030bd1c 9efed1e ef1891d 9efed1e 0063153 94cc85e 3bf0541 64bb241 7e1e2b2 64bb241 73bee56 64bb241 7e1e2b2 64bb241 3bf0541 94cc85e 20ed088 94cc85e b198972 136efd8 b198972 05151a0 20ed088 739e288 b198972 6964386 739e288 6964386 523740d 6964386 739e288 20ed088 8449392 5a1dec4 8449392 ef1891d 8449392 ef1891d 8449392 ef1891d 8449392 ef1891d 8449392 20ed088 b7885e0 ef1891d b7885e0 8449392 b664056 f8132aa b664056 a2fe634 b664056 0d4c535 b664056 f8132aa b664056 a2fe634 b664056 0d4c535 b664056 f8132aa b664056 a5dc826 b664056 0d4c535 68963be b664056 f8132aa b664056 a2fe634 a5dc826 0d4c535 b664056 b198972 20ed088 a5dc826 eae0c35 8fa17eb 5665d45 5a370ea 5665d45 eae0c35 282de42 eae0c35 5a370ea 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 5665d45 eae0c35 282de42 5665d45 eae0c35 5665d45 eae0c35 282de42 5665d45 8fa17eb 5665d45 8fa17eb 5665d45 8fa17eb 5665d45 8fa17eb 5665d45 282de42 5665d45 8fa17eb 5665d45 282de42 5665d45 eae0c35 5665d45 eae0c35 ba963a9 282de42 5665d45 ab9ee28 eae0c35 5665d45 eae0c35 5665d45 eae0c35 e0b73ec eae0c35 5665d45 eae0c35 5665d45 282de42 eae0c35 5665d45 282de42 eae0c35 913a5f1 eae0c35 20ed088 282de42 a7be810 818ea9b a7be810 818ea9b d043d2f 818ea9b d043d2f 818ea9b d043d2f 818ea9b d043d2f 818ea9b 4c47890 d043d2f 818ea9b d043d2f 818ea9b 4c47890 d043d2f b7885e0 d043d2f 4c47890 d043d2f 818ea9b a7be810 20ed088 818ea9b 5665d45 94cc85e c10f08f 9efed1e 94cc85e 904478a 94cc85e 904478a c10f08f 904478a 94cc85e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 |
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("""
<style>
.stApp {
background-color: #E3F2FD;
}
.title {
text-align: center;
font-size: 28px;
font-weight: bold;
color: #2C3E50;
}
.subtitle {
text-align: center;
font-size: 30px;
font-weight: bold;
color: #003366;
margin-top: 10px;
}
.stButton > button {
width: 100%;
background-color: #1E88E5;
color: white;
font-size: 16px;
font-weight: bold;
border-radius: 6px;
padding: 8px;
transition: 0.3s;
}
.stButton > button:hover {
background-color: #1565C0;
}
.result-box {
text-align: center;
font-size: 22px;
font-weight: bold;
color: white;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
background-color: #388E3C;
}
</style>
""", unsafe_allow_html=True)
# Navigation State
if "current_page" not in st.session_state:
st.session_state.current_page = "Model Pipeline"
def switch_page(page):
st.session_state.current_page = page
# Sidebar Navigation
st.sidebar.title("Navigation")
if st.sidebar.button("Model Pipeline"):
switch_page("Model Pipeline")
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 Pipeline":
st.markdown("<h1 class='title'>Model Pipeline</h1>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
#st.markdown("<h4 class='subtitle'>Explore different stages of the Life Expectancy project</h4>", unsafe_allow_html=True)
st.image("images/Life_Expectancy.webp",
caption="Life Expectancy Prediction Overview",
use_container_width=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")
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown(
"""
<div style="text-align: center;">
<a href="https://github.com/Yashvj22/Life_Expectancy_Model" target="_blank" style="
background-color: #007bff;
color: white;
padding: 12px 25px;
text-decoration: none;
font-size: 16px;
font-weight: bold;
border-radius: 8px;
display: inline-block;
transition: 0.3s;">
π See Whole Code on GitHub
</a>
</div>
""",
unsafe_allow_html=True
)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown('''
<h2 style="text-align:center;"> About Author</h2>
<div style="background-color:#f5f5f5; border-radius:10px; padding:20px; margin-top:20px;">
<p style="font-size:16px; text-align:center; font-family:Georgia; line-height:1.6; color:#000;">
Hello! Iβm <b>Yash Jadhav</b>, a passionate <span style="color:#FF6347;">Data Scientist</span>
and <span style="color:#4682B4;">Data Analyst</span>.
I specialize in transforming raw data into actionable insights and helping others master the art of Machine Learning.
</p>
<div style="text-align:center; margin-top:20px;">
<a href="https://www.linkedin.com/in/yash-jadhav-454b0a237/" target="_blank" style="
background-color:#0073b1; color:white; padding:10px 20px; border-radius:5px;
text-decoration:none; margin-right:10px;">LinkedIn</a>
<a href="https://github.com/Yashvj22" target="_blank" style="
background-color:black; color:white; padding:10px 20px; border-radius:5px;
text-decoration:none; margin-right:10px;">GitHub</a>
<a href="https://medium.com/@yashvj2222" target="_blank" style="
background-color:grey; color:white; padding:10px 20px; border-radius:5px;
text-decoration:none;">Medium</a>
</div>
</div>
''', unsafe_allow_html=True)
# Individual Sections
elif st.session_state.current_page == "Problem Statement":
st.markdown("<h1 class='title'>Problem Statement</h1>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center; margin-top: 20px;">
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.
</h5>
""", unsafe_allow_html=True)
st.markdown("<br>", 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 Pipeline"):
switch_page("Model Pipeline")
elif st.session_state.current_page == "Data Collection":
st.markdown("<h1 class='title'>Data Collection</h1>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center; margin-top: 20px;">
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.
</h5>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center; margin-top: 10px;">
π <a href="https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who" target="_blank" style="font-weight: bold; color: #007BFF; text-decoration: none;">
Click here to access the dataset on Kaggle</a>
</h5>
""", unsafe_allow_html=True)
st.markdown("<h2 class='subtitle' style='text-align: center; margin-top: 20px;'>Dataset Overview</h2>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center; margin-top: 15px; margin-bottom: 20px;">
The dataset consists of <b>2938 rows</b> and <b>22 columns</b>, capturing crucial indicators such as life expectancy,
mortality rates, GDP, schooling, immunization rates, and more. Below is a summary of the dataset features:
</h5>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
data_info = """
<div style= "font-size: 16px; background-color: #F5F5F5; padding: 15px; border-radius: 10px;">
β’ <b>Country:</b> Name of the country (Categorical)<br>
β’ <b>Year:</b> Year of observation (Numerical)<br>
β’ <b>Status:</b> Developing or Developed country (Categorical)<br>
β’ <b>Life Expectancy:</b> Average age a person is expected to live (Numerical)<br>
β’ <b>Adult Mortality:</b> Probability of dying between 15-60 years per 1000 population (Numerical)<br>
β’ <b>Infant Deaths:</b> Number of infant deaths per 1000 live births (Numerical)<br>
β’ <b>Alcohol:</b> Alcohol consumption per capita (Numerical)<br>
β’ <b>Percentage Expenditure:</b> Government expenditure on health as a percentage of GDP (Numerical)<br>
β’ <b>Hepatitis B:</b> Immunization coverage for Hepatitis B (Numerical)<br>
β’ <b>Measles:</b> Number of reported measles cases per year (Numerical)<br>
β’ <b>BMI:</b> Average Body Mass Index of the population (Numerical)<br>
β’ <b>Under-five Deaths:</b> Number of deaths under the age of five per 1000 live births (Numerical)<br>
β’ <b>Polio:</b> Immunization coverage for Polio (Numerical)<br>
β’ <b>Total Expenditure:</b> Total health expenditure as a percentage of GDP (Numerical)<br>
β’ <b>Diphtheria:</b> Immunization coverage for Diphtheria (Numerical)<br>
β’ <b>HIV/AIDS:</b> Death rate due to HIV/AIDS per 100,000 people (Numerical)<br>
β’ <b>GDP:</b> Gross Domestic Product per capita (Numerical)<br>
β’ <b>Population:</b> Total population of the country (Numerical)<br>
β’ <b>Thinness 1-19 Years:</b> Percentage of thin individuals aged 1-19 years (Numerical)<br>
β’ <b>Thinness 5-9 Years:</b> Percentage of thin individuals aged 5-9 years (Numerical)<br>
β’ <b>Income Composition:</b> Human development index based on income composition (Numerical)<br>
β’ <b>Schooling:</b> Average number of years of schooling (Numerical)<br>
</div>
"""
st.markdown(data_info, unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
elif st.session_state.current_page == "Simple EDA":
st.markdown("<h1 class='title'>Simple Exploratory Data Analysis</h1>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center; margin-top: 20px;">
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.
</h5>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
# Display dataset sample
st.markdown("<h3 class='subtitle' style='text-align: center;'>Sample Dataset</h3>", unsafe_allow_html=True)
st.dataframe(data.head())
st.markdown("<br>", unsafe_allow_html=True)
# Display missing values count
st.markdown("<h3 class='subtitle' style='text-align: center;'>Missing Values Summary</h3>", 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("<br>", unsafe_allow_html=True)
# Display missing values count
st.markdown("<h3 class='subtitle' style='text-align: center;'>Data Description</h3>", unsafe_allow_html=True)
st.dataframe(data.describe())
st.markdown("<br>", unsafe_allow_html=True)
# Add Boxplot Visualizations
st.markdown("<h3 class='subtitle' style='text-align: center;'>Boxplots for Data Distribution</h3>", 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("<br>", unsafe_allow_html=True)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
elif st.session_state.current_page == "Data Pre-processing":
st.markdown("<h1 class='title'>Data Preprocessing</h1>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("<h2 class='subtitle' style='text-align: center;'>Handling Missing Values</h2>", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center;">
<b>Using "Median" Imputation to Fill Highly Skewed Data</b>
</h5>
""", unsafe_allow_html=True)
st.markdown("""
<div style="
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
background-color: #f9f9f9;
text-align: justify;">
Median imputation is used for columns where data distribution is highly skewed.
This approach ensures that extreme values do not overly influence the dataset.
Examples include GDP, Population, and Adult Mortality.
</div>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center;">
<b>Mean Imputation for Columns with Small Missing Values and Normally Distributed Data</b>
</h5>
""", unsafe_allow_html=True)
st.markdown("""
<div style="
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
background-color: #f9f9f9;
text-align: justify;">
Mean imputation is applied when missing values are small and the data is normally distributed.
This helps maintain the overall dataset structure without being affected by extreme values.
Suitable columns include BMI, Polio, and Schooling.
</div>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("""
<h5 style="text-align: center;">
<b>Applying One-Hot Encoding on "Status" Column</b>
</h5>
""", unsafe_allow_html=True)
st.markdown("""
<div style="
border: 1px solid #ddd;
border-radius: 8px;
padding: 15px;
background-color: #f9f9f9;
text-align: justify;">
The "Status" column categorizes countries as either Developed or Developing.
One-Hot Encoding is used to convert this categorical variable into a numerical format
suitable for machine learning models. The "drop='first'" parameter is applied to prevent
multicollinearity.
</div>
""", unsafe_allow_html=True)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
elif st.session_state.current_page == "EDA":
st.markdown("<h1 class='title'>Exploratory Data Analysis (EDA)</h1>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# Target Column Distribution
st.markdown("<h2 class='subtitle' style='text-align: center;'>Target Column Distribution</h2>", unsafe_allow_html=True)
st.image("images/target_column_distribution.png", caption="Life Expectancy Distribution", use_container_width=True)
st.markdown("""
<h5 style="text-align: center;">
Insight: Mostly Life Expectancy is in <b>range of 50-80</b>.
</h5>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# Correlation Heatmap
st.markdown("<h2 class='subtitle' style='text-align: center;'>Correlation Heatmap</h2>", unsafe_allow_html=True)
st.image("images/Correlation_Heatmap.png", caption="Correlation Heatmap", use_container_width=True)
st.markdown("""
<h5 style="text-align: center;">
Insight: Our target column <b>Life Expectancy</b> is mostly linearly dependent on
<b>Schooling, Income Composition of Resources, GDP, Diphtheria, Polio, BMI, and Percentage Expenditure</b>.
</h5>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# How Specific Columns Affect Life Expectancy
st.markdown("<h2 class='subtitle' style='text-align: center;'>How Specific Columns Affect Life Expectancy</h2>", unsafe_allow_html=True)
# GDP vs. Life Expectancy
st.image("images/specific_col_affecting_target.png", caption="Features vs. Life Expectancy", use_container_width=True)
st.markdown("""
<h5>
Insights:
1οΈβ£ **GDP vs. Life Expectancy**
- Positive correlation: As GDP increases, Life Expectancy also increases.
- Some countries with low GDP still have high Life Expectancy due to good healthcare policies.
2οΈβ£ **Schooling vs. Life Expectancy**
- Strong positive correlation: More years of schooling β longer life.
- Educated populations follow better hygiene, diet, and medical care, increasing Life Expectancy.
3οΈβ£ **Income Composition vs. Life Expectancy**
- Higher economic stability leads to better healthcare systems and lifestyles, improving Life Expectancy.
4οΈβ£ **Diphtheria & Polio vs. Life Expectancy**
- Higher vaccination rates (80%-100%) correspond to Life Expectancy above 70 years.
- Lower vaccination rates (<40%) lead to lower Life Expectancy (~40-60 years), indicating weak healthcare infrastructure.
5οΈβ£ **BMI vs. Life Expectancy**
- No clear linear trend due to high variance in data points.
- BMI < 18 (malnutrition) and BMI > 30 (obesity) reduce Life Expectancy.
- Advanced healthcare and better nutrition in some countries help maintain high Life Expectancy despite malnutrition/obesity.
</h5>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# Life Expectancy vs Developed / Undeveloped Countries
st.markdown("<h2 class='subtitle' style='text-align: center;'>Life Expectancy vs Developed / Undeveloped Countries</h2>", unsafe_allow_html=True)
st.image("images/target_col vs countries.png", caption="Life Expectancy vs Developed / Undeveloped Countries", use_container_width=True)
st.markdown("""
<h5 style="text-align: center;">
Insight: Life Expectancy is <b>higher in Developed Countries</b> due to Advanced Healthcare, Better Nutrition, Medical Interventions.
</h5>""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
# Model Building
elif st.session_state.current_page == "Model Building":
st.markdown("""
<h2 style='text-align: center; color: #333;'>Model Building</h2>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("""
<h2>Introduction</h2>
<p>In this section, we explore different <b>Ensemble Learning</b> techniques to improve model performance.</p>
<p>We implemented three ensemble models:
<span style='font-size:16px;'>π₯ <b>Voting Regressor</b> - π― <b>Bagging Regressor</b> - π² <b>Random Forest Regressor</b></span></p>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("""
<h5 style='color: #1363DF;'>1οΈβ£ Voting Regressor</h5>
<ul>
<li><b>Concept:</b> Combines multiple models (<b>KNN & Decision Tree</b>) and takes the <b>average prediction</b>.</li>
<li><b>Why Voting Regressor?</b> β
Works well when models have different strengths. β
Reduces variance while maintaining interpretability.</li>
</ul>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px dashed #bbb;'>", unsafe_allow_html=True)
st.markdown("""
<h5 style='color: #FF6D28;'>2οΈβ£ Bagging Regressor</h5>
<ul>
<li><b>Concept:</b> Uses <b>bootstrap sampling</b> to train multiple models on different subsets of data.</li>
<li><b>Why Bagging Regressor?</b> β
Reduces overfitting by averaging multiple models. β
Works best with <b>high-variance models</b> like Decision Tree.</li>
</ul>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px dashed #bbb;'>", unsafe_allow_html=True)
st.markdown("""
<h5 style='color: #2EB086;'>3οΈβ£ Random Forest Regressor</h5>
<ul>
<li><b>Concept:</b> Uses <b>multiple Decision Trees</b>, trained on different feature subsets.</li>
<li><b>Why Random Forest?</b> β
Handles <b>non-linearity</b> well. β
Less prone to overfitting compared to a single Decision Tree.</li>
</ul>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("""
<h3>Combining High & Low Variance Models</h3>
<p>A crucial step to improve ensemble performance is <b>choosing models with different variance levels</b>:</p>
<ul>
<li><b>Voting Regressor:</b> Uses a combination of <b>high-variance</b> (Decision Tree, KNN with small K) and <b>low-variance</b> (KNN with large K, Decision Tree with depth constraint) models.</li>
<li><b>Bagging & Random Forest:</b> Use <b>only high-variance models</b> (Decision Trees with deep splits) to maximize variance reduction.</li>
</ul>
<p><b>This technique helps create a <span style='color: green;'>balanced ensemble</span>, preventing excessive overfitting or underfitting! β
</b></p>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# Hyperparameter Tuning
st.markdown("""
<h3>Hyperparameter Tuning using Optuna β‘</h3>
<p>We optimized hyperparameters for <b>KNN, Decision Tree, Bagging Regressor, and Random Forest</b> using <b>Optuna</b>.</p>
<p>Below are the <b>optimized parameters</b> for each model:</p>
<h5>πΉ K-Nearest Neighbors (KNN)</h5>
<ul>
<li><code>n_neighbors</code></li>
<li><code>p</code></li>
<li><code>weights</code></li>
<li><code>algorithm</code></li>
</ul>
<h5>πΉ Decision Tree</h5>
<ul>
<li><code>max_depth</code></li>
<li><code>min_samples_split</code></li>
<li><code>min_samples_leaf</code></li>
<li><code>max_features</code></li>
<li><code>min_impurity_decrease</code></li>
</ul>
<h5>πΉ Bagging Regressor</h5>
<ul>
<li><code>n_estimators</code></li>
<li><code>max_samples</code></li>
</ul>
<h5>πΉ Random Forest</h5>
<ul>
<li><code>n_estimators</code></li>
<li><code>max_samples</code></li>
</ul>
""", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
# Model Performance Insights
st.markdown("""
<h3>Model Performance Insights π</h3>
<p>Hereβs how our ensemble models performed on training and test datasets:</p>
""", unsafe_allow_html=True)
st.markdown("""
<style>
table {
width: 100%;
border-collapse: collapse;
text-align: center;
font-size: 16px;
}
th, td {
padding: 10px;
border-bottom: 1px solid #ddd;
}
th {
background-color: #F3F4F6;
}
</style>
""", unsafe_allow_html=True)
st.markdown("""
<table>
<tr>
<th>Ensemble</th>
<th>Training Score</th>
<th>Test Score</th>
<th>Generalized Score</th>
</tr>
<tr>
<td>Voting Ensemble</td>
<td>95.80%</td>
<td>92.13%</td>
<td>92.89%</td>
</tr>
<tr>
<td>Bagging Ensemble</td>
<td>98.68%</td>
<td>95.04%</td>
<td><b>95.45%</b></td>
</tr>
<tr>
<td>Random Forest</td>
<td>97.92%</td>
<td>94.71%</td>
<td>94.71%</td>
</tr>
</table>
""", unsafe_allow_html=True)
st.markdown("<br>", unsafe_allow_html=True)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
# Fianl Model
elif st.session_state.current_page == "Final Model":
st.markdown(
"""
<style>
.title {
text-align: center;
font-size: 36px;
font-weight: bold;
color: #1E3A8A;
}
.subtitle {
text-align: center;
font-size: 20px;
color: #475569;
margin-bottom: 20px;
}
.image-container {
display: flex;
justify-content: center;
}
.caption {
text-align: center;
font-size: 16px;
font-style: italic;
color: #6B7280;
}
.box {
background-color: #F8FAFC;
padding: 15px;
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
</style>
""",
unsafe_allow_html=True,
)
# Title
st.markdown("<h1 class='title'>Final Model</h1>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown(
"<div class='box'>"
"<p><strong>After experimenting with multiple trials using Optuna, we selected the best-fit model "
"by analyzing the training and test scores of different trials. "
"The following scatter plots provide insights into this selection process.</strong></p>"
"</div>",
unsafe_allow_html=True,
)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Training vs Test Score (All Trials)</h3>", unsafe_allow_html=True)
st.markdown(
"<p class='subtitle'>This scatter plot visualizes the training and test scores of all trials. "
"The goal was to identify a model where both scores are closely aligned, ensuring minimal overfitting or underfitting.</p>",
unsafe_allow_html=True,
)
st.image("images/bagging_trails.png",
caption="All Trails",
use_container_width=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Training vs Test Score (First 50 Trials)</h3>", unsafe_allow_html=True)
st.markdown(
"<p class='subtitle'>By filtering the first 50 trials, we focused on models that demonstrated balanced performance. "
"The best-fit model was selected by ensuring that the training and test scores are close to each other.</p>",
unsafe_allow_html=True,
)
st.image("images/bagging_50trails.png",
caption="50 Trails",
use_container_width=True)
st.markdown(
"<p style='text-align: center; font-weight: bold; font-size: 16px;'>"
"From the above trials, we selected the <b>9th trial</b> as its train score and test score have minimal difference."
"</p>",
unsafe_allow_html=True
)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("<h3 style='text-align: center;'>Selected Best-Fit Model</h3>", unsafe_allow_html=True)
st.markdown(
"<div class='box'>"
"<ul>"
"<li><b>Base Model:</b> DecisionTreeRegressor</li>"
"<li><b>Hyperparameters:</b>"
"<ul>"
"<li>min_samples_leaf = 2</li>"
"<li>min_samples_split = 3</li>"
"</ul></li>"
"<li><b>Ensemble Method:</b> BaggingRegressor</li>"
"<li><b>Bagging Hyperparameters:</b>"
"<ul>"
"<li>n_estimators = 40</li>"
"<li>max_samples = 0.838404</li>"
"</ul></li>"
"</ul>"
"<p>This model was selected as it demonstrated a balance between generalization and performance.</p>"
"</div>",
unsafe_allow_html=True,
)
if st.button("π Go Back to Model Pipeline"):
switch_page("Model Pipeline")
# Hands-on Model Page
elif st.session_state.current_page == "Hands-on Model":
st.markdown("<h1 class='title'>Hands-on Model</h1>", unsafe_allow_html=True)
st.markdown("<hr style='border:1px solid #ddd;'>", unsafe_allow_html=True)
st.markdown("<h4 class='subtitle' style='text-align: center;'>Provide inputs to predict Life Expectancy</h4>", unsafe_allow_html=True)
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"""
<div class="result-box">
Predicted Life Expectancy: <b>{prediction:.2f} years</b>
</div>
""",
unsafe_allow_html=True,
)
|