-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.py
175 lines (143 loc) · 7.48 KB
/
home.py
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
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.impute import SimpleImputer
import matplotlib.pyplot as plt
import seaborn as sns
# Theme settings
st.set_page_config(page_title='Life Expectancy Estimator Tool', layout='wide')
# Title and description
st.title('LIFE EXPECTANCY ESTIMATOR TOOL')
st.markdown('---')
st.write('''
This app uses a Linear Regression model to estimate life expectancy based on features and data obtained the World Health Organization (WHO) on Kaggle
Please fill in the attributes below and adjust the model's parameters to the desired values.
Once ready, please hit the 'ESTIMATE LIFE EXPECTANCY' button to get the prediction and the model's performance.
''')
st.markdown('---')
# Input Attributes
st.header('Input Attributes')
att_adult_mortality = st.slider('Adult Mortality', min_value=0, max_value=24, value=10, step=1)
att_infant_deaths = st.slider('Infant Deaths', min_value=0, max_value=55, value=10, step=1)
att_alcohol = st.slider('Alcohol Consumption', min_value=0.0, max_value=4.3, value=0.01, step=0.1)
att_hepatitis_b = st.slider('Hepatitis B Unimmunized Rates(%)', min_value=0.0, max_value=5.0, value=3.1, step=0.1)
att_measles = st.slider('Measles Unimmunized Rates', min_value=0.0, max_value=11.7, value=5.0, step=0.1)
att_bmi = st.slider('BMI', min_value=0.0, max_value=5.0, value=1.0, step=0.1)
att_polio = st.slider('Polio Unimmunized Rates(%)', min_value=0.0, max_value=5.0, value=1.0, step=0.1)
att_total_expenditure = st.slider('Total Expenditure (% of GDP)', min_value=0.0, max_value=13.0, value=8.16, step=0.1)
att_diphtheria = st.slider('Diphtheria Unimmunized Rates(%)', min_value=0.0, max_value=5.0, value=3.2, step=0.01)
att_hiv_aids = st.slider('HIV/AIDS deaths (per 1000 lives)', min_value=0.0, max_value=3.0, value=0.1, step=0.1)
att_gdp = st.slider('GDP (10000)', min_value=0.0, max_value=12.0, value=2.1, step=0.1)
att_thinness_1_19_years = st.slider('Thinness 1-19 years (%)', min_value=0.0, max_value=3.0, value=1.1, step=0.1)
att_schooling = st.slider('Schooling (years)', min_value=0, max_value=18, value=10, step=1)
word = "Developed Countries"
on = st.toggle("Developing Countries", "Developed Countries")
if on:
word = "Developing Countries"
else:
word = "Developed Countries"
user_input = np.array([att_adult_mortality, att_infant_deaths, att_alcohol, att_hepatitis_b,
att_measles, att_bmi, att_polio, att_total_expenditure,
att_diphtheria, att_hiv_aids, att_gdp, att_thinness_1_19_years,
att_schooling])
# Sidebar - Set Parameters
with st.sidebar.header('Set Parameters'):
split_size = st.sidebar.slider('Data split ratio (percentage for Training Set)', min_value=10, max_value=90, value=20, step=10)
# Load dataset
def get_dataset():
data = pd.read_csv("Life Expectancy Data.csv")
return data
life_df = get_dataset()
df = life_df.copy()
selected_cols = ["Alcohol", "Adult Mortality", "Hepatitis B", "Measles ", " BMI ",
"Polio", "Total expenditure", "Diphtheria ", " HIV/AIDS",
"GDP", " thinness 1-19 years", "Schooling", "infant deaths", "Life expectancy ", "Status"]
selected_data = df[selected_cols]
selected_data.columns = ["Alcohol", "Adult Mortality", "Hepatitis B", "Measles", "BMI",
"Polio", "Total Expenditure", "Diphtheria", "HIV/AIDS",
"GDP", "Thinness 1-19 Years", "Schooling", "Infant Deaths", "Life Expectancy", "Status"]
# Transform selected columns
transformed_data = selected_data.apply({"Adult Mortality": np.sqrt,
"Life Expectancy": lambda x: x,
"Alcohol": np.sqrt,
"Hepatitis B": lambda x: np.log(100 - x),
"Measles": lambda x: np.log(x + 0.1),
"BMI": lambda x: np.log(100 - x),
"Polio": lambda x: np.log(100 - x),
"Total Expenditure": lambda x: x,
"Diphtheria": lambda x: np.log(100 - x),
"HIV/AIDS": np.log,
"GDP": np.log,
"Thinness 1-19 Years": np.log,
"Schooling": lambda x: x,
"Status": lambda x: x,
"Infant Deaths": lambda x: x})
# Remove rows with outliers
def find_outliers(data):
q1 = np.nanpercentile(data, 25)
q3 = np.nanpercentile(data, 75)
iqr = q3 - q1
min_threshold = q1 - 1.5*iqr
max_threshold = q3 + 1.5*iqr
return list( np.where((data < min_threshold) | (data > max_threshold))[0] )
def get_filtered_data(remove_status):
outliers = []
for i, col in transformed_data.items():
if i != "Status":
outliers += find_outliers(col)
else:
outliers += list( np.where(df["Status"] == remove_status)[0] )
data_no_outliers = transformed_data.drop(index=outliers).reset_index(drop=True).drop('Status', axis=1)
# Replace missing values with mean
imputer = SimpleImputer(strategy="mean")
new_data = pd.DataFrame(imputer.fit_transform(data_no_outliers), columns=data_no_outliers.columns)
train_data, test_data = train_test_split(new_data, train_size=0.8, random_state=1)
train_data, val_data = train_test_split(train_data, train_size=0.8, random_state=1)
return train_data, val_data, test_data
train_data, val_data, test_data = get_filtered_data(remove_status=word)
# Features
vars = ["Alcohol", "Adult Mortality", "Hepatitis B", "Measles", "BMI",
"Polio", "Total Expenditure", "Diphtheria", "HIV/AIDS",
"GDP", "Thinness 1-19 Years", "Schooling", "Infant Deaths"]
X_test = test_data[vars]
y_test = test_data["Life Expectancy"]
# Prepare training and validation sets
X_train = train_data[vars]
X_val = val_data[vars]
y_train = train_data["Life Expectancy"]
y_val = val_data["Life Expectancy"]
if st.button('Estimate Life Expectancy'):
# Model training
model = LinearRegression()
model.fit(X_train, y_train)
# Align user_input with X_train columns
user_input_df = pd.DataFrame([user_input], columns=[
'Adult Mortality', 'Infant Deaths', 'Alcohol', 'Hepatitis B',
'Measles', 'BMI', 'Polio', 'Total Expenditure',
'Diphtheria', 'HIV/AIDS', 'GDP', 'Thinness 1-19 Years',
'Schooling'
])
# Add missing dummy columns for 'Status'
for col in X_train.columns:
if col not in user_input_df.columns:
user_input_df[col] = 0
# Reorder user_input_df columns to match X_train
user_input_df = user_input_df[X_train.columns]
# Make predictions
predictions = model.predict(user_input_df)
model_score = model.score(X_test, y_test)
mse = mean_squared_error(y_test, model.predict(X_test))
rmse = np.sqrt(mse)
mse_train = mean_squared_error(y_train, model.predict(X_train))
rmse_train = np.sqrt(mse_train)
# Display results
st.markdown('**Result - Prediction!**')
st.write('Based on the user input, the estimated Life Expectancy is:')
st.info(predictions[0])
st.write('Model Performance:')
st.write('Error (MSE) for testing:')
st.info(mse)
st.markdown('---')