saifsunny commited on
Commit
fe85280
1 Parent(s): 8271c87

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -108
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
- import matplotlib.pyplot as plt
5
- import joblib
6
 
7
  from sklearn.ensemble import RandomForestClassifier, VotingClassifier
8
  from sklearn.tree import DecisionTreeClassifier
@@ -12,12 +10,9 @@ from sklearn.svm import SVC
12
  from sklearn.naive_bayes import GaussianNB
13
  from sklearn.neural_network import MLPClassifier
14
  from sklearn.ensemble import GradientBoostingClassifier
15
- from xgboost import XGBClassifier
16
- from lightgbm import LGBMClassifier
17
  from sklearn.model_selection import train_test_split
18
  from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
19
-
20
-
21
  st.title('Disease Prediction Application')
22
  st.write('''
23
  Please fill in the attributes below, then hit the Predict button
@@ -32,7 +27,6 @@ st.write(''' ''')
32
  bmi = st.slider('BMI', min_value=0.0, max_value=50.0, value=25.0, step=0.1)
33
  st.write(''' ''')
34
 
35
-
36
  st.header('Blood Test Information')
37
  bs = st.radio("Is Your Fasting Blood Sugar > 120 mg/dl?", ('Yes', 'No'))
38
  st.write(''' ''')
@@ -62,11 +56,14 @@ max_heart = st.slider('Maximum Heart Rate', min_value=0.0, max_value=300.0, valu
62
  st.write(''' ''')
63
  resting_bp = st.slider('Resting Blood Pressure (In mm Hg)', min_value=0.0, max_value=200.0, value=100.0, step=1.0)
64
  st.write(''' ''')
65
- re = st.radio("Resting Electrocardiogram Results", ('Normal', 'ST-T Wave Abnormality (T Wave Inversions and/or ST Elevation or Depression of > 0.05 mV)', 'Showing Probable or Definite Left Ventricular Hypertrophy by Estes Criteria'))
 
 
66
  st.write(''' ''')
67
  ex = st.radio("Exercise Induced Angina", ('Yes', 'No'))
68
  st.write(''' ''')
69
- oldpeak = st.slider('ST Depression Induced by Exercise Relative to Rest', min_value=-5.0, max_value=5.0, value=0.0, step=0.01)
 
70
  st.write(''' ''')
71
  sp = st.radio("The Slope of the Peak Exercise ST Segment", ('Upsloping', 'Flat', 'Downsloping'))
72
  st.write(''' ''')
@@ -121,7 +118,8 @@ user_input = np.array([age, gender, chest, blood_sugar, resting_bp, electro, max
121
 
122
  # import dataset
123
  def get_dataset():
124
- data = pd.read_csv('Fianl Dataset.csv')
 
125
 
126
  # Calculate the correlation matrix
127
  # corr_matrix = data.corr()
@@ -138,115 +136,83 @@ def get_dataset():
138
  # st.pyplot()
139
 
140
  return data
141
-
142
 
143
  if st.button('Submit'):
144
  # Load your dataset for prediction
145
  df = get_dataset()
146
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  # Split the dataset into features and targets for Heart and Diabetes prediction
148
  X = df.drop(['Diabetes', 'Heart'], axis=1)
149
  y_heart = df['Heart']
150
  y_diabetes = df['Diabetes']
151
 
152
- # Create an ensemble model for Heart prediction
153
- random_forest_model_heart = RandomForestClassifier(random_state=42)
154
- naive_bayes_model_heart = GaussianNB()
155
- gradient_boosting_model_heart = GradientBoostingClassifier(random_state=42)
156
 
157
- # Create a voting ensemble with soft voting for Heart prediction
158
- ensemble_model_heart = VotingClassifier(estimators=[
159
- ('Random Forest', random_forest_model_heart),
160
- ('Naive Bayes', naive_bayes_model_heart),
161
- ('Gradient Boosting', gradient_boosting_model_heart)
162
- ], voting='soft')
163
-
164
- # Create an ensemble model for Diabetes prediction
165
- random_forest_model_diabetes = RandomForestClassifier(random_state=42)
166
- naive_bayes_model_diabetes = GaussianNB()
167
- gradient_boosting_model_diabetes = GradientBoostingClassifier(random_state=42)
168
-
169
- # Create a voting ensemble with soft voting for Diabetes prediction
170
- ensemble_model_diabetes = VotingClassifier(estimators=[
171
- ('Random Forest', random_forest_model_diabetes),
172
- ('Naive Bayes', naive_bayes_model_diabetes),
173
- ('Gradient Boosting', gradient_boosting_model_diabetes)
174
- ], voting='soft')
175
 
176
- # Split the data into training and testing sets for both targets
177
- X_train, X_test, y_heart_train, y_heart_test, y_diabetes_train, y_diabetes_test = train_test_split(
178
- X, y_heart, y_diabetes, test_size=0.2, random_state=42
179
- )
180
-
181
- # Ensure the user input has the correct number of features for Heart prediction
182
- if user_input.shape[1] == X_train.shape[1]:
183
- # Fit the ensemble model for Heart prediction on the training data
184
- ensemble_model_heart.fit(X_train, y_heart_train)
185
-
186
- # Make predictions on user input for Heart
187
- prediction_heart = ensemble_model_heart.predict(user_input)
188
- prediction_proba_heart = ensemble_model_heart.predict_proba(user_input)
189
-
190
- # Check if the dimensions of prediction_heart match y_heart_test
191
- if prediction_heart.shape[0] == y_heart_test.shape[0]:
192
- # You can add a threshold and provide a prediction based on class 1 for Heart
193
- threshold_heart = 0.5
194
- if prediction_proba_heart[0][1] >= threshold_heart:
195
- st.header("Predicted Heart Disease: You might have Heart Disease")
196
- st.write("Predicted Probability of Having Heart Disease:", prediction_proba_heart[0][1] * 100)
197
- else:
198
- st.header("Predicted Heart Disease: You do not have Heart Disease")
199
- st.write("Predicted Probability of Having Heart Disease:", prediction_proba_heart[0][1] * 100)
200
-
201
- # Calculate and print Heart prediction metrics
202
- accuracy_heart = accuracy_score(y_heart_test, prediction_heart)
203
- precision_heart = precision_score(y_heart_test, prediction_heart)
204
- recall_heart = recall_score(y_heart_test, prediction_heart)
205
- f1_heart = f1_score(y_heart_test, prediction_heart)
206
-
207
- st.write("Heart Prediction Metrics:")
208
- st.write("Accuracy:", accuracy_heart)
209
- st.write("Precision:", precision_heart)
210
- st.write("Recall:", recall_heart)
211
- st.write("F1-score:", f1_heart)
212
- st.write("____________________________________________________________________________________________")
213
- else:
214
- st.write("Error: Inconsistent dimensions in Heart prediction. Please check your data.")
215
  else:
216
- st.write("Error: Input features do not match the dataset. Please provide valid input.")
217
-
218
- # Ensure the user input has the correct number of features for Diabetes prediction
219
- if user_input.shape[1] == X_train.shape[1]:
220
- # Fit the ensemble model for Diabetes prediction on the training data
221
- ensemble_model_diabetes.fit(X_train, y_diabetes_train)
222
-
223
- # Make predictions on user input for Diabetes
224
- pred_diabetes = ensemble_model_diabetes.predict(user_input)
225
- pred_diabetes_proba = ensemble_model_diabetes.predict_proba(user_input)
226
-
227
- # Check if the dimensions of pred_diabetes match y_diabetes_test
228
- if pred_diabetes.shape[0] == y_diabetes_test.shape[0]:
229
- # You can add a threshold and provide a prediction based on class 1 for Diabetes
230
- threshold_diabetes = 0.5
231
- if pred_diabetes_proba[0][1] >= threshold_diabetes:
232
- st.header("Predicted Diabetes: You might have Diabetes")
233
- st.write("Predicted Probability of Having Diabetes:", pred_diabetes_proba[0][1] * 100)
234
- else:
235
- st.header("Predicted Diabetes: You do not have Diabetes")
236
- st.write("Predicted Probability of Having Diabetes:", pred_diabetes_proba[0][1] * 100)
237
-
238
- # Calculate and print Diabetes prediction metrics
239
- accuracy_diabetes = accuracy_score(y_diabetes_test, pred_diabetes)
240
- precision_diabetes = precision_score(y_diabetes_test, pred_diabetes)
241
- recall_diabetes = recall_score(y_diabetes_test, pred_diabetes)
242
- f1_diabetes = f1_score(y_diabetes_test, pred_diabetes)
243
-
244
- st.write("Diabetes Prediction Metrics:")
245
- st.write("Accuracy:", accuracy_diabetes)
246
- st.write("Precision:", precision_diabetes)
247
- st.write("Recall:", recall_diabetes)
248
- st.write("F1-score:", f1_diabetes)
249
- else:
250
- st.write("Error: Inconsistent dimensions in Diabetes prediction. Please check your data.")
251
  else:
252
- st.write("Error: Input features do not match the dataset. Please provide valid input.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import numpy as np
 
 
4
 
5
  from sklearn.ensemble import RandomForestClassifier, VotingClassifier
6
  from sklearn.tree import DecisionTreeClassifier
 
10
  from sklearn.naive_bayes import GaussianNB
11
  from sklearn.neural_network import MLPClassifier
12
  from sklearn.ensemble import GradientBoostingClassifier
 
 
13
  from sklearn.model_selection import train_test_split
14
  from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
15
+ from sklearn.multioutput import MultiOutputClassifier
 
16
  st.title('Disease Prediction Application')
17
  st.write('''
18
  Please fill in the attributes below, then hit the Predict button
 
27
  bmi = st.slider('BMI', min_value=0.0, max_value=50.0, value=25.0, step=0.1)
28
  st.write(''' ''')
29
 
 
30
  st.header('Blood Test Information')
31
  bs = st.radio("Is Your Fasting Blood Sugar > 120 mg/dl?", ('Yes', 'No'))
32
  st.write(''' ''')
 
56
  st.write(''' ''')
57
  resting_bp = st.slider('Resting Blood Pressure (In mm Hg)', min_value=0.0, max_value=200.0, value=100.0, step=1.0)
58
  st.write(''' ''')
59
+ re = st.radio("Resting Electrocardiogram Results", (
60
+ 'Normal', 'ST-T Wave Abnormality (T Wave Inversions and/or ST Elevation or Depression of > 0.05 mV)',
61
+ 'Showing Probable or Definite Left Ventricular Hypertrophy by Estes Criteria'))
62
  st.write(''' ''')
63
  ex = st.radio("Exercise Induced Angina", ('Yes', 'No'))
64
  st.write(''' ''')
65
+ oldpeak = st.slider('ST Depression Induced by Exercise Relative to Rest', min_value=-5.0, max_value=5.0, value=0.0,
66
+ step=0.01)
67
  st.write(''' ''')
68
  sp = st.radio("The Slope of the Peak Exercise ST Segment", ('Upsloping', 'Flat', 'Downsloping'))
69
  st.write(''' ''')
 
118
 
119
  # import dataset
120
  def get_dataset():
121
+ data = pd.read_csv('Final Dataset.csv')
122
+
123
 
124
  # Calculate the correlation matrix
125
  # corr_matrix = data.corr()
 
136
  # st.pyplot()
137
 
138
  return data
139
+
140
 
141
  if st.button('Submit'):
142
  # Load your dataset for prediction
143
  df = get_dataset()
144
 
145
+ # Create an ensemble model with 'Random Forest', 'Naive Bayes', and 'Gradient Boosting'
146
+ random_forest_model = RandomForestClassifier(random_state=101)
147
+ naive_bayes_model = GaussianNB()
148
+ gradient_boosting_model = GradientBoostingClassifier(random_state=42)
149
+
150
+ # Create a voting ensemble with soft voting
151
+ ensemble_model = VotingClassifier(estimators=[
152
+ ('Random Forest', random_forest_model),
153
+ ('Naive Bayes', naive_bayes_model),
154
+ ('Gradient Boosting', gradient_boosting_model)
155
+ ], voting='soft')
156
+
157
  # Split the dataset into features and targets for Heart and Diabetes prediction
158
  X = df.drop(['Diabetes', 'Heart'], axis=1)
159
  y_heart = df['Heart']
160
  y_diabetes = df['Diabetes']
161
 
162
+ # Fit the ensemble model on the entire dataset for Heart Disease prediction
163
+ ensemble_model.fit(X, y_heart)
 
 
164
 
165
+ # Make predictions on user input for Heart Disease
166
+ prediction_heart = ensemble_model.predict(user_input)
167
+ prediction_proba_heart = ensemble_model.predict_proba(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
+ # You can add a threshold and provide a prediction based on class 1 for Heart Disease
170
+ threshold_heart = 0.5
171
+ if prediction_proba_heart[0][1] >= threshold_heart:
172
+ st.header("Predicted Heart Disease: You might have Heart Disease")
173
+ st.write("Predicted Probability of Having Heart Disease:", prediction_proba_heart[0][1] * 100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  else:
175
+ st.header("Predicted Heart Disease: You do not have Heart Disease")
176
+ st.write("Predicted Probability of Having Heart Disease:", prediction_proba_heart[0][1] * 100)
177
+
178
+ # Calculate and display evaluation metrics for Heart Disease prediction
179
+ accuracy_heart = accuracy_score(y_heart, ensemble_model.predict(X))
180
+ precision_heart = precision_score(y_heart, ensemble_model.predict(X))
181
+ recall_heart = recall_score(y_heart, ensemble_model.predict(X))
182
+ f1_heart = f1_score(y_heart, ensemble_model.predict(X))
183
+
184
+ st.header("Heart Disease Prediction Metrics:")
185
+ st.write("Accuracy:", accuracy_heart)
186
+ st.write("Precision:", precision_heart)
187
+ st.write("Recall:", recall_heart)
188
+ st.write("F1-score:", f1_heart)
189
+
190
+ # Fit the ensemble model on the entire dataset for Diabetes prediction
191
+ ensemble_model.fit(X, y_diabetes)
192
+
193
+ # Make predictions on user input for Diabetes
194
+ pred_diabetes = ensemble_model.predict(user_input)
195
+ pred_diabetes_proba = ensemble_model.predict_proba(user_input)
196
+
197
+ # You can add a threshold and provide a prediction based on class 1 for Diabetes
198
+ threshold_diabetes = 0.5
199
+ if pred_diabetes_proba[0][1] >= threshold_diabetes:
200
+ st.header("Predicted Diabetes: You might have Diabetes")
201
+ st.write("Predicted Probability of Having Diabetes:", pred_diabetes_proba[0][1] * 100)
 
 
 
 
 
 
 
 
202
  else:
203
+ st.header("Predicted Diabetes: You do not have Diabetes")
204
+ st.write("Predicted Probability of Having Diabetes:", pred_diabetes_proba[0][1] * 100)
205
+
206
+ # Calculate and display evaluation metrics for Diabetes prediction
207
+ accuracy_diabetes = accuracy_score(y_diabetes, ensemble_model.predict(X))
208
+ precision_diabetes = precision_score(y_diabetes, ensemble_model.predict(X))
209
+ recall_diabetes = recall_score(y_diabetes, ensemble_model.predict(X))
210
+ f1_diabetes = f1_score(y_diabetes, ensemble_model.predict(X))
211
+
212
+ st.header("Diabetes Prediction Metrics:")
213
+ st.write("Accuracy:", accuracy_diabetes)
214
+ st.write("Precision:", precision_diabetes)
215
+ st.write("Recall:", recall_diabetes)
216
+ st.write("F1-score:", f1_diabetes)
217
+
218
+