Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,113 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import pandas as pd
|
3 |
-
from sklearn.model_selection import train_test_split
|
4 |
-
from sklearn.ensemble import RandomForestRegressor
|
5 |
-
import gradio as gr
|
6 |
-
|
7 |
-
# Load dataset
|
8 |
-
data = pd.read_csv('Life-Expectancy-Data-Updated.csv')
|
9 |
-
|
10 |
-
# Define the feature columns and target column - 'Country' and 'Region' removed
|
11 |
-
feature_cols = [
|
12 |
-
'Infant_deaths', 'Under_five_deaths', 'Adult_mortality', 'Alcohol_consumption',
|
13 |
-
'Hepatitis_B', 'Measles', 'BMI', 'Polio', 'Diphtheria', 'Incidents_HIV', 'GDP_per_capita',
|
14 |
-
'Schooling', 'Economy_status_Developed', 'Economy_status_Developing'
|
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 |
-
# Create
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
)
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
app.launch()
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.ensemble import RandomForestRegressor
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Load dataset
|
8 |
+
data = pd.read_csv('Life-Expectancy-Data-Updated.csv')
|
9 |
+
|
10 |
+
# Define the feature columns and target column - 'Country' and 'Region' removed
|
11 |
+
#feature_cols = [
|
12 |
+
# 'Infant_deaths', 'Under_five_deaths', 'Adult_mortality', 'Alcohol_consumption',
|
13 |
+
# 'Hepatitis_B', 'Measles', 'BMI', 'Polio', 'Diphtheria', 'Incidents_HIV', 'GDP_per_capita',
|
14 |
+
# 'Schooling', 'Economy_status_Developed', 'Economy_status_Developing'
|
15 |
+
#]
|
16 |
+
feature_cols = [ # Only 6 selected features used
|
17 |
+
'Infant_deaths', 'Under_five_deaths', 'Adult_mortality',
|
18 |
+
'BMI', 'Polio', 'Schooling'
|
19 |
+
]
|
20 |
+
target_col = 'Life_expectancy'
|
21 |
+
X = data[feature_cols]
|
22 |
+
y = data[target_col]
|
23 |
+
|
24 |
+
# Split the data
|
25 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
26 |
+
|
27 |
+
# Train the model
|
28 |
+
#rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
|
29 |
+
# Based on CV tuning
|
30 |
+
rf_model = RandomForestRegressor(max_depth=10, max_features='sqrt', min_samples_leaf=1, min_samples_split=2, n_estimators=200, random_state=42)
|
31 |
+
rf_model.fit(X_train, y_train)
|
32 |
+
|
33 |
+
#-----------------------------------------------------------------------------------
|
34 |
+
|
35 |
+
# Define the feature columns - must match the columns used in the training script
|
36 |
+
#feature_cols = [
|
37 |
+
# 'Infant_deaths', 'Under_five_deaths', 'Adult_mortality', 'Alcohol_consumption',
|
38 |
+
# 'Hepatitis_B', 'Measles', 'BMI', 'Polio', 'Diphtheria', 'Incidents_HIV', 'GDP_per_capita',
|
39 |
+
# 'Schooling', 'Economy_status_Developed', 'Economy_status_Developing'
|
40 |
+
#]
|
41 |
+
feature_cols = [ # Only 6 selected features used
|
42 |
+
'Infant_deaths', 'Under_five_deaths', 'Adult_mortality',
|
43 |
+
'BMI', 'Polio', 'Schooling'
|
44 |
+
]
|
45 |
+
|
46 |
+
# Load the trained model
|
47 |
+
#rf_model = joblib.load('rf_model.pkl')
|
48 |
+
|
49 |
+
# Define the prediction function
|
50 |
+
#def predict_life_expectancy(
|
51 |
+
# Infant_deaths, Under_five_deaths, Adult_mortality, Alcohol_consumption,
|
52 |
+
# Hepatitis_B, Measles, BMI, Polio, Diphtheria, Incidents_HIV, GDP_per_capita,
|
53 |
+
# Schooling, Economy_status
|
54 |
+
#):
|
55 |
+
def predict_life_expectancy(
|
56 |
+
Infant_deaths, Under_five_deaths, Adult_mortality,
|
57 |
+
BMI, Polio, Schooling
|
58 |
+
):
|
59 |
+
# Initialize Economy_status features
|
60 |
+
#Economy_status_Developed = 0
|
61 |
+
#Economy_status_Developing = 0
|
62 |
+
|
63 |
+
# Set the appropriate status to 1 based on the dropdown selection
|
64 |
+
#if Economy_status == "Developed":
|
65 |
+
# Economy_status_Developed = 1
|
66 |
+
#else: # economy_status == "Developing"
|
67 |
+
# Economy_status_Developing = 1
|
68 |
+
|
69 |
+
# Convert the inputs to a DataFrame
|
70 |
+
#input_data = pd.DataFrame([[
|
71 |
+
# Infant_deaths, Under_five_deaths, Adult_mortality, Alcohol_consumption,
|
72 |
+
# Hepatitis_B, Measles, BMI, Polio, Diphtheria, Incidents_HIV, GDP_per_capita,
|
73 |
+
# Schooling, Economy_status_Developed, Economy_status_Developing
|
74 |
+
#]], columns=feature_cols)
|
75 |
+
input_data = pd.DataFrame([[
|
76 |
+
Infant_deaths, Under_five_deaths, Adult_mortality,
|
77 |
+
BMI, Polio, Schooling
|
78 |
+
]], columns=feature_cols)
|
79 |
+
|
80 |
+
# Make the prediction
|
81 |
+
prediction = rf_model.predict(input_data)[0]
|
82 |
+
return prediction
|
83 |
+
|
84 |
+
# Create Gradio inputs for each feature
|
85 |
+
inputs = [
|
86 |
+
#gr.Number(label=col) for col in feature_cols
|
87 |
+
gr.Slider(0, 1000, value=0, label="Infant_deaths",step=0.1),
|
88 |
+
gr.Slider(0, 1000, value=0, label="Under_five_deaths",step=0.1),
|
89 |
+
gr.Slider(0, 1000, value=0, label="Adult_mortality",step=0.1),
|
90 |
+
#gr.Slider(0, 100, value=0, label="Alcohol_consumption",step=0.1),
|
91 |
+
#gr.Slider(0, 100, value=99, label="Hepatitis_B", step=0.1),
|
92 |
+
#gr.Slider(0, 100, value=99, label="Measles", step=0.1),
|
93 |
+
gr.Slider(0, 50, value=25, label="BMI", step=0.1),
|
94 |
+
gr.Slider(0, 100, value=99, label="Polio", step=0.1),
|
95 |
+
#gr.Slider(0, 100, value=99, label="Diphtheria", step=0.1),
|
96 |
+
#gr.Slider(0, 100, value=0, label="Incidents_HIV", step=0.1),
|
97 |
+
#gr.Slider(0, 199999, value=0, label="GDP_per_capita", step=1),
|
98 |
+
gr.Slider(0, 100, value=0, label="Schooling", step=0.1),
|
99 |
+
#gr.Dropdown(value=0, label="Economy Status", choices=["Developed", "Developing"]) # Dropdown for economy status
|
100 |
+
]
|
101 |
+
|
102 |
+
# Create the Gradio interface
|
103 |
+
app = gr.Interface(
|
104 |
+
fn=predict_life_expectancy, # Function to use for predictions
|
105 |
+
inputs=inputs, # The inputs for the model
|
106 |
+
outputs=gr.Textbox(label="Life Expectancy Prediction:"), # The output is a single number (life expectancy)
|
107 |
+
title="Life Expectancy Prediction",
|
108 |
+
description="Enter values for the features to predict life expectancy.",
|
109 |
+
theme=gr.themes.Base()
|
110 |
+
)
|
111 |
+
|
112 |
+
# Launch the Gradio app
|
113 |
app.launch()
|