lkp72 commited on
Commit
4e27a52
·
verified ·
1 Parent(s): 0355362

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -94
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
- target_col = 'Life_expectancy'
17
- X = data[feature_cols]
18
- y = data[target_col]
19
-
20
- # Split the data
21
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
22
-
23
- # Train the model
24
- rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
25
- rf_model.fit(X_train, y_train)
26
-
27
- #-----------------------------------------------------------------------------------
28
-
29
- # Define the feature columns - must match the columns used in the training script
30
- feature_cols = [
31
- 'Infant_deaths', 'Under_five_deaths', 'Adult_mortality', 'Alcohol_consumption',
32
- 'Hepatitis_B', 'Measles', 'BMI', 'Polio', 'Diphtheria', 'Incidents_HIV', 'GDP_per_capita',
33
- 'Schooling', 'Economy_status_Developed', 'Economy_status_Developing'
34
- ]
35
-
36
- # Load the trained model
37
- #rf_model = joblib.load('rf_model.pkl')
38
-
39
- # Define the prediction function
40
- def predict_life_expectancy(
41
- Infant_deaths, Under_five_deaths, Adult_mortality, Alcohol_consumption,
42
- Hepatitis_B, Measles, BMI, Polio, Diphtheria, Incidents_HIV, GDP_per_capita,
43
- Schooling, Economy_status
44
- ):
45
- # Initialize Economy_status features
46
- Economy_status_Developed = 0
47
- Economy_status_Developing = 0
48
-
49
- # Set the appropriate status to 1 based on the dropdown selection
50
- if Economy_status == "Developed":
51
- Economy_status_Developed = 1
52
- else: # economy_status == "Developing"
53
- Economy_status_Developing = 1
54
-
55
- # Convert the inputs to a DataFrame
56
- input_data = pd.DataFrame([[
57
- Infant_deaths, Under_five_deaths, Adult_mortality, Alcohol_consumption,
58
- Hepatitis_B, Measles, BMI, Polio, Diphtheria, Incidents_HIV, GDP_per_capita,
59
- Schooling, Economy_status_Developed, Economy_status_Developing
60
- ]], columns=feature_cols)
61
-
62
- # Make the prediction
63
- prediction = rf_model.predict(input_data)[0]
64
- return prediction
65
-
66
- # Create Gradio inputs for each feature
67
- inputs = [
68
- #gr.Number(label=col) for col in feature_cols
69
- gr.Slider(0, 1000, value=0, label="Infant_deaths",step=0.1),
70
- gr.Slider(0, 1000, value=0, label="Under_five_deaths",step=0.1),
71
- gr.Slider(0, 1000, value=0, label="Adult_mortality",step=0.1),
72
- gr.Slider(0, 100, value=0, label="Alcohol_consumption",step=0.1),
73
- gr.Slider(0, 100, value=99, label="Hepatitis_B", step=0.1),
74
- gr.Slider(0, 100, value=99, label="Measles", step=0.1),
75
- gr.Slider(0, 50, value=25, label="BMI", step=0.1),
76
- gr.Slider(0, 100, value=99, label="Polio", step=0.1),
77
- gr.Slider(0, 100, value=99, label="Diphtheria", step=0.1),
78
- gr.Slider(0, 100, value=0, label="Incidents_HIV", step=0.1),
79
- gr.Slider(0, 199999, value=0, label="GDP_per_capita", step=1),
80
- gr.Slider(0, 100, value=0, label="Schooling", step=0.1),
81
- gr.Dropdown(value=0, label="Economy Status", choices=["Developed", "Developing"]) # Dropdown for economy status
82
- ]
83
-
84
- # Create the Gradio interface
85
- app = gr.Interface(
86
- fn=predict_life_expectancy, # Function to use for predictions
87
- inputs=inputs, # The inputs for the model
88
- outputs=gr.Textbox(label="Life Expectancy Prediction:"), # The output is a single number (life expectancy)
89
- title="Life Expectancy Prediction",
90
- description="Enter values for the features to predict life expectancy.",
91
- theme=gr.themes.Base()
92
- )
93
-
94
- # Launch the Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()