iDrops commited on
Commit
4b9b315
·
verified ·
1 Parent(s): 434cceb

Upload 5 files

Browse files
Cardio_Vascular_Disease_by_Gut_Microbiota.csv ADDED
The diff for this file is too large to render. See raw diff
 
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
- title: Hypertension CVD Detection
3
- emoji: 📈
4
- colorFrom: purple
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.12.0
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Cardio Vascular Disease Prediction
3
+ emoji: 🐢
4
+ colorFrom: red
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sklearn.model_selection import train_test_split
3
+ import numpy as np
4
+ from sklearn.metrics import accuracy_score
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+
8
+ # Check for missing values
9
+ #Loading Data
10
+ data = pd.read_csv('Cardio_Vascular_Disease_by_Gut_Microbiota.csv')
11
+ print(data.head())
12
+
13
+ from sklearn.ensemble import RandomForestClassifier
14
+
15
+ # Define features and target
16
+ X = data.drop(columns=['patient_id', 'CVD_Status'])
17
+ y = data['CVD_Status']
18
+
19
+ # Train a RandomForest model
20
+ rf = RandomForestClassifier(random_state=42)
21
+ rf.fit(X, y)
22
+
23
+ # Feature importances
24
+ importances = rf.feature_importances_
25
+
26
+ # Plot feature importances
27
+ feature_importance_df = pd.DataFrame({'Feature': X.columns, 'Importance': importances})
28
+ feature_importance_df = feature_importance_df.sort_values('Importance', ascending=False)
29
+
30
+ plt.figure(figsize=(10,6))
31
+ sns.barplot(x='Importance', y='Feature', data=feature_importance_df)
32
+ plt.title('Feature Importance from Random Forest')
33
+ plt.show()
34
+
35
+ from sklearn.ensemble import GradientBoostingClassifier
36
+ from xgboost import XGBClassifier
37
+ from lightgbm import LGBMClassifier
38
+ from sklearn.metrics import accuracy_score, confusion_matrix
39
+ from sklearn.metrics import accuracy_score, confusion_matrix, r2_score, mean_squared_error, mean_absolute_error
40
+ from math import sqrt
41
+
42
+ # Initialize the models
43
+ gradient_boosting = GradientBoostingClassifier(random_state=42)
44
+
45
+ # Split into training and testing sets (80% train, 20% test)
46
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
47
+
48
+ # Train and evaluate Gradient Boosting
49
+ gradient_boosting.fit(X_train, y_train)
50
+ y_pred_gb = gradient_boosting.predict(X_test)
51
+ accuracy_gb = accuracy_score(y_test, y_pred_gb)
52
+ conf_matrix_gb = confusion_matrix(y_test, y_pred_gb)
53
+
54
+ # Print results
55
+ print(f"Gradient Boosting Accuracy: {accuracy_gb * 100:.2f}%")
56
+ print(f"Confusion Matrix:\n {conf_matrix_gb}\n")
57
+
58
+ # Predict probabilities
59
+ y_pred_prob_gb = gradient_boosting.predict_proba(X_test)[:, 1]
60
+
61
+ # Predict class labels
62
+ y_pred_gb = gradient_boosting.predict(X_test)
63
+
64
+ # Calculate R² Score, RMSE, MSE, and MAE for Gradient Boosting
65
+ r2_gb = r2_score(y_test, y_pred_prob_gb)
66
+ rmse_gb = sqrt(mean_squared_error(y_test, y_pred_prob_gb))
67
+ mse_gb = mean_squared_error(y_test, y_pred_prob_gb)
68
+ mae_gb = mean_absolute_error(y_test, y_pred_prob_gb)
69
+
70
+ # Print Accuracy, R², RMSE, MSE, and MAE for Gradient Boosting
71
+ print(f"Gradient Boosting Accuracy: {accuracy_gb * 100:.2f}%")
72
+ print(f"R² Score: {r2_gb:.4f}, RMSE: {rmse_gb:.4f}, MSE: {mse_gb:.4f}, MAE: {mae_gb:.4f}")
73
+ print(f"Confusion Matrix:\n {conf_matrix_gb}\n")
74
+
75
+ xgboost = XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)
76
+
77
+
78
+ # Train and evaluate XGBoost
79
+ xgboost.fit(X_train, y_train)
80
+ y_pred_xgb = xgboost.predict(X_test)
81
+ accuracy_xgb = accuracy_score(y_test, y_pred_xgb)
82
+ conf_matrix_xgb = confusion_matrix(y_test, y_pred_xgb)
83
+
84
+ print(f"XGBoost Accuracy: {accuracy_xgb * 100:.2f}%")
85
+ print(f"Confusion Matrix:\n {conf_matrix_xgb}\n")
86
+
87
+ y_pred_prob_xgb = xgboost.predict_proba(X_test)[:, 1]
88
+
89
+ y_pred_xgb = xgboost.predict(X_test)
90
+
91
+ # Calculate R² Score, RMSE, MSE, and MAE for XGBoost
92
+ r2_xgb = r2_score(y_test, y_pred_prob_xgb)
93
+ rmse_xgb = sqrt(mean_squared_error(y_test, y_pred_prob_xgb))
94
+ mse_xgb = mean_squared_error(y_test, y_pred_prob_xgb)
95
+ mae_xgb = mean_absolute_error(y_test, y_pred_prob_xgb)
96
+
97
+ # Print Accuracy, R², RMSE, MSE, and MAE for XGBoost
98
+ print(f"XGBoost Accuracy: {accuracy_xgb * 100:.2f}%")
99
+ print(f"R² Score: {r2_xgb:.4f}, RMSE: {rmse_xgb:.4f}, MSE: {mse_xgb:.4f}, MAE: {mae_xgb:.4f}")
100
+ print(f"Confusion Matrix:\n {conf_matrix_xgb}\n")
101
+
102
+ lightgbm = LGBMClassifier(random_state=42)
103
+
104
+ # Train and evaluate LightGBM
105
+ lightgbm.fit(X_train, y_train)
106
+ y_pred_lgbm = lightgbm.predict(X_test)
107
+ accuracy_lgbm = accuracy_score(y_test, y_pred_lgbm)
108
+ conf_matrix_lgbm = confusion_matrix(y_test, y_pred_lgbm)
109
+
110
+
111
+ print(f"LightGBM Accuracy: {accuracy_lgbm * 100:.2f}%")
112
+ print(f"Confusion Matrix:\n {conf_matrix_lgbm}\n")
113
+
114
+ y_pred_prob_lgbm = lightgbm.predict_proba(X_test)[:, 1]
115
+
116
+ y_pred_lgbm = lightgbm.predict(X_test)
117
+
118
+ # Calculate R² Score, RMSE, MSE, and MAE for LightGBM
119
+ r2_lgbm = r2_score(y_test, y_pred_prob_lgbm)
120
+ rmse_lgbm = sqrt(mean_squared_error(y_test, y_pred_prob_lgbm))
121
+ mse_lgbm = mean_squared_error(y_test, y_pred_prob_lgbm)
122
+ mae_lgbm = mean_absolute_error(y_test, y_pred_prob_lgbm)
123
+
124
+
125
+ # Print Accuracy, R², RMSE, MSE, and MAE for LightGBM
126
+ print(f"LightGBM Accuracy: {accuracy_lgbm * 100:.2f}%")
127
+ print(f"R² Score: {r2_lgbm:.4f}, RMSE: {rmse_lgbm:.4f}, MSE: {mse_lgbm:.4f}, MAE: {mae_lgbm:.4f}")
128
+ print(f"Confusion Matrix:\n {conf_matrix_lgbm}\n")
129
+
130
+ import joblib
131
+
132
+ # Assuming you have already trained the model (e.g., GradientBoostingClassifier, XGBoost, etc.)
133
+ # Example with a Gradient Boosting model (replace with your trained model)
134
+ from sklearn.ensemble import GradientBoostingClassifier
135
+
136
+ # Assuming you have trained a model
137
+ model = GradientBoostingClassifier(random_state=42)
138
+ model.fit(X_train, y_train) # Replace this with your actual training code
139
+
140
+ # Save the trained model as a .pkl file
141
+ joblib.dump(model, 'trained_model.pkl')
142
+
143
+ print("Model saved successfully as trained_model.pkl")
144
+
145
+
146
+ def predict_cvd(Age, Gender, BMI, Blood_pressure, cholesterol, Bacteroides_fragilis, Faecalibacterium_prausnitzii,
147
+ Akkermansia_muciniphila, Ruminococcus_bromii, Microbiome_Diversity):
148
+
149
+ # Convert Gender to numerical (assuming Male: 0, Female: 1)
150
+ Gender = 1 if Gender.lower() == 'female' else 0
151
+
152
+ # Prepare the input data as a dataframe
153
+ input_data = pd.DataFrame({
154
+ 'Age': [Age],
155
+ 'Gender': [Gender],
156
+ 'BMI': [BMI],
157
+ 'Blood_pressure': [Blood_pressure],
158
+ 'cholesterol': [cholesterol],
159
+ 'Bacteroides_fragilis': [Bacteroides_fragilis],
160
+ 'Faecalibacterium_prausnitzii': [Faecalibacterium_prausnitzii],
161
+ 'Akkermansia_muciniphila': [Akkermansia_muciniphila],
162
+ 'Ruminococcus_bromii': [Ruminococcus_bromii],
163
+ 'Microbiome_Diversity': [Microbiome_Diversity]
164
+ })
165
+
166
+ print(input_data) # Print the input to debug
167
+
168
+ # Predict CVD status (0 or 1)
169
+ prediction = model.predict(input_data)
170
+
171
+ # Return the result
172
+ return "Cardiovascular Disease Detected" if prediction[0] == 1 else "No Cardiovascular Disease Detected"
173
+
174
+ import gradio as gr
175
+ import pandas as pd
176
+ import joblib
177
+
178
+ # Load the pre-trained model
179
+ model = joblib.load('trained_model.pkl')
180
+
181
+ # Define the prediction function
182
+ def predict_cvd(Age, Gender, BMI, Blood_pressure, Cholesterol, Bacteroides_fragilis, Faecalibacterium_prausnitzii,
183
+ Akkermansia_muciniphila, Ruminococcus_bromii, Microbiome_Diversity):
184
+
185
+ try:
186
+ # Convert Gender to numerical (assuming Male: 0, Female: 1)
187
+ Gender = 1 if Gender.lower() == 'female' else 0
188
+
189
+ # Prepare the input data as a dataframe with correctly capitalized feature names
190
+ input_data = pd.DataFrame({
191
+ 'Age': [Age],
192
+ 'Gender': [Gender],
193
+ 'BMI': [BMI],
194
+ 'Blood_pressure': [Blood_pressure],
195
+ 'Cholesterol': [Cholesterol], # Note the capital "C"
196
+ 'Bacteroides_fragilis': [Bacteroides_fragilis],
197
+ 'Faecalibacterium_prausnitzii': [Faecalibacterium_prausnitzii],
198
+ 'Akkermansia_muciniphila': [Akkermansia_muciniphila],
199
+ 'Ruminococcus_bromii': [Ruminococcus_bromii],
200
+ 'Microbiome_Diversity': [Microbiome_Diversity]
201
+ })
202
+
203
+ # Make prediction
204
+ prediction = model.predict(input_data)
205
+
206
+ # Return result based on prediction
207
+ return "Cardiovascular Disease Detected" if prediction[0] == 1 else "No Cardiovascular Disease Detected"
208
+
209
+ except Exception as e:
210
+ return f"An error occurred: {str(e)}"
211
+
212
+ # Define Gradio inputs with proper ranges and selections
213
+ inputs = [
214
+ gr.Slider(18, 100, step=1, value=50, label="Age"),
215
+ gr.Dropdown(['Male', 'Female'], label="Gender"),
216
+ gr.Slider(10.0, 50.0, step=0.1, value=25.0, label="BMI"),
217
+ gr.Slider(90, 200, step=1, value=120, label="Blood Pressure"),
218
+ gr.Slider(100, 300, step=1, value=180, label="Cholesterol"), # Corrected capitalization
219
+ gr.Slider(0.0, 10.0, step=0.1, value=5.0, label="Bacteroides Fragilis Level"),
220
+ gr.Slider(0.0, 10.0, step=0.1, value=5.0, label="Faecalibacterium Prausnitzii Level"),
221
+ gr.Slider(0.0, 10.0, step=0.1, value=5.0, label="Akkermansia Muciniphila Level"),
222
+ gr.Slider(0.0, 10.0, step=0.1, value=5.0, label="Ruminococcus Bromii Level"),
223
+ gr.Slider(0.0, 10.0, step=0.1, value=5.0, label="Microbiome Diversity"),
224
+ ]
225
+
226
+ # Define Gradio interface
227
+ iface = gr.Interface(fn=predict_cvd, inputs=inputs, outputs="text", title="Cardiovascular Disease Prediction")
228
+
229
+ # Launch the interface
230
+ iface.launch()
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ scikit-learn
2
+ seaborn
3
+ xgboost
4
+ lightgbm