-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
346 lines (232 loc) · 12.4 KB
/
main.py
File metadata and controls
346 lines (232 loc) · 12.4 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
!pip install imbalanced-learn
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from imblearn.over_sampling import SMOTE
df = pd.read_csv('MLDATA.csv')
df.columns = df.columns.str.strip().str.lower().str.replace('[^a-z0-9]', '_', regex=True)
df['gender'] = df['gender'].astype(str).str.strip().str.lower().map({'male': 0, 'female': 1})
binary_cols = [
'panicattackhistory_yes_no_',
'hypertensionhistory_yes_no_',
'medicationuse_yes_no_',
'smoking_alcohol_etc_yes_no_'
]
for col in binary_cols:
df[col] = df[col].astype(str).str.strip().str.upper().map({'YES': 1, 'NO': 0})
df['socialinteraction_low_med_high_'] = df['socialinteraction_low_med_high_'].astype(str).str.strip().str.upper().map({'LOW': 0, 'MED': 1, 'HIGH': 2})
if 'duration' in df.columns:
df['duration'] = df['duration'].astype(str).str.extract(r'(\d+)').astype(float)
df['qol_score'] = (
100
- df['painseverity_1_10_'] * 3
- df['anxietyscore'] * 2
- df['depressionscore'] * 2
- df['panicattackhistory_yes_no_'] * 10
- df['hypertensionhistory_yes_no_'] * 5
- df['medicationuse_yes_no_'] * 5
- df['smoking_alcohol_etc_yes_no_'] * 5
+ df['sleephours'] * 2
+ df['physicalactivity_hoursperweek_'] * 2
+ df['socialinteraction_low_med_high_'] * 5
- df['age'] * 0.1
)
def classify_qol(score):
if score < 40:
return 'Low'
elif score < 70:
return 'Medium'
else:
return 'High'
df['qol_class'] = df['qol_score'].apply(classify_qol)
cat_cols = ['location', 'trigger', 'occupation']
cat_cols = [col for col in cat_cols if col in df.columns]
df = pd.get_dummies(df, columns=cat_cols, drop_first=True)
df.to_csv('MLDATA_with_QoL.csv', index=False)
X = df.drop(['qol_score', 'qol_class'], axis=1)
y_cls = df['qol_class']
y_reg = df['qol_score']
# REGRESSION
X_train_r, X_test_r, y_train_r, y_test_r = train_test_split(X, y_reg, test_size=0.15, random_state=42)
reg = RandomForestRegressor(n_estimators=100, random_state=42)
reg.fit(X_train_r, y_train_r)
y_pred_r = reg.predict(X_test_r)
print("\n📈 Regression Results:")
print("R2 Score:", round(r2_score(y_test_r, y_pred_r), 3))
print("RMSE:", round(np.sqrt(mean_squared_error(y_test_r, y_pred_r)), 2))
# CLASSIFICATION
X_train_c, X_test_c, y_train_c, y_test_c = train_test_split(X, y_cls, test_size=0.15, random_state=42, stratify=y_cls)
sm = SMOTE(random_state=42)
X_train_c_sm, y_train_c_sm = sm.fit_resample(X_train_c, y_train_c)
clf = RandomForestClassifier(n_estimators=100, class_weight='balanced', random_state=42)
clf.fit(X_train_c_sm, y_train_c_sm)
y_pred_c = clf.predict(X_test_c)
print("\n Classification Results:")
print("Accuracy:", round(accuracy_score(y_test_c, y_pred_c), 3))
print(classification_report(y_test_c, y_pred_c))
conf_mat = confusion_matrix(y_test_c, y_pred_c)
sns.heatmap(conf_mat, annot=True, fmt='d', cmap='Blues', xticklabels=clf.classes_, yticklabels=clf.classes_)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()
# FEATURE IMPORTANCE
importances = clf.feature_importances_
indices = np.argsort(importances)[::-1]
features = X.columns[indices]
plt.figure(figsize=(10, 12))
sns.barplot(x=importances[indices], y=features)
plt.title("Feature Importance - Classification Model")
plt.tight_layout()
plt.show()
# Take input and predict
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
def get_user_input():
user_input = {}
user_input['gender'] = input("Enter gender (Male/Female): ").strip().lower()
user_input['weight'] = float(input("Enter weight (in kg): "))
user_input['age'] = int(input("Enter age: "))
user_input['painseverity_1_10_'] = int(input("Pain severity (1-10): "))
user_input['duration'] = input("How long does the pain last (e.g., '5 minutes'): ")
user_input['location'] = input("Where is the pain located? (e.g., 'Lower Back'): ").strip()
user_input['trigger'] = input("Trigger of the pain? (e.g., 'Prolonged Sitting'): ").strip()
user_input['occupation'] = input("Occupation: ").strip()
user_input['anxietyscore'] = int(input("Anxiety score (0-10): "))
user_input['depressionscore'] = int(input("Depression score (0-10): "))
user_input['panicattackhistory_yes_no_'] = input("Panic attack history (Yes/No): ").strip().upper()
user_input['hypertensionhistory_yes_no_'] = input("Hypertension history (Yes/No): ").strip().upper()
user_input['medicationuse_yes_no_'] = input("Medication use (Yes/No): ").strip().upper()
user_input['smoking_alcohol_etc_yes_no_'] = input("Smoking/Alcohol etc. (Yes/No): ").strip().upper()
user_input['sleephours'] = float(input("Sleep hours per day: "))
user_input['physicalactivity_hoursperweek_'] = float(input("Physical activity hours/week: "))
user_input['socialinteraction_low_med_high_'] = input("Social interaction (Low/Med/High): ").strip().upper()
return user_input
def predict_qol(user_input_dict, model, columns_template):
input_df = pd.DataFrame([user_input_dict])
input_df['gender'] = input_df['gender'].map({'male': 0, 'female': 1})
input_df['panicattackhistory_yes_no_'] = input_df['panicattackhistory_yes_no_'].map({'YES': 1, 'NO': 0})
input_df['hypertensionhistory_yes_no_'] = input_df['hypertensionhistory_yes_no_'].map({'YES': 1, 'NO': 0})
input_df['medicationuse_yes_no_'] = input_df['medicationuse_yes_no_'].map({'YES': 1, 'NO': 0})
input_df['smoking_alcohol_etc_yes_no_'] = input_df['smoking_alcohol_etc_yes_no_'].map({'YES': 1, 'NO': 0})
input_df['socialinteraction_low_med_high_'] = input_df['socialinteraction_low_med_high_'].map({'LOW': 0, 'MED': 1, 'HIGH': 2})
input_df['duration'] = input_df['duration'].str.extract(r'(\d+)').astype(float)
input_df = pd.get_dummies(input_df)
input_df = input_df.reindex(columns=columns_template, fill_value=0)
prediction = model.predict(input_df)[0]
return prediction
df = pd.read_csv("MLDATA_with_QoL.csv")
X = df.drop(['qol_score', 'qol_class'], axis=1)
y = df['qol_class']
model = RandomForestClassifier()
model.fit(X, y)
user_input = get_user_input()
prediction = predict_qol(user_input, model, X.columns)
print("\n🔮 Predicted QoL Class:", prediction)
if prediction == 'Low':
print("""⚠️ High Risk of Musculoskeletal Chest Pain (MSCP)
Please consult a healthcare provider immediately.
Your current health data suggests a high likelihood of musculoskeletal chest pain, which may be significantly affecting your quality of life and mental well-being. MSCP, while non-cardiac in origin, can mimic serious conditions and may be worsened by psychological factors such as anxiety, depression, panic attacks, and hypertension.
📋 What You Should Know:
MSCP is not caused by heart problems, but it can still be debilitating.
It may stem from muscle tension, poor posture, or joint dysfunction in the chest or upper back.
Mental health plays a major role—stress and anxiety can amplify the perception of pain.
Ignoring symptoms can lead to chronic pain and reduced daily function.
🩺 What You Should Do:
✅ 1. Seek Immediate Medical Evaluation
Consult a primary care physician, physiotherapist, or pain specialist.
Ensure that any serious cardiac causes are ruled out first.
✅ 2. Begin a Pain Management Plan
A doctor may recommend pain relief, muscle relaxants, or targeted physical therapy.
Avoid self-medicating without professional advice.
✅ 3. Address Mental Health
Speak to a licensed therapist about managing stress, panic, or depressive symptoms.
Consider Cognitive Behavioral Therapy (CBT) or mindfulness techniques to reduce pain-related anxiety.
✅ 4. Monitor Your Vital Signs
Track blood pressure, heart rate, and pain episodes using a wearable or monitoring device.
Keep a daily log of symptoms, mood, sleep, and activity levels.
✅ 5. Make Lifestyle Changes
Improve posture and ergonomic habits.
Reduce intake of caffeine, processed foods, and salt.
Prioritize regular sleep, hydration, and gentle exercise like stretching or walking.
📞 When to Get Emergency Help:
If you experience any of the following, seek emergency medical care immediately:
Severe or sudden chest pain
Pain radiating to the arm, jaw, or neck
Shortness of breath, dizziness, or fainting
Sweating, nausea, or irregular heartbeat
🧠 Reminder:
Even though MSCP is non-cardiac, it is a real and painful condition. Getting early treatment and support can prevent it from becoming chronic and help restore your physical and emotional well-being.""")
elif prediction == 'Medium':
print("""
⚠️ Moderate QoL Detected
🩺 Your current health status indicates a moderate risk level. Taking preventive action now can help improve your quality of life and avoid future complications.
✅ Recommended Preventive Measures
🧘 Mental & Emotional Well-being
🧠 Try Cognitive Behavioral Therapy (CBT)
Manage stress and anxiety that may be worsening your symptoms.
🕯️ Practice Mindfulness or Guided Meditation
Even 10–15 minutes daily can reduce panic and boost emotional balance.
📅 Schedule Regular Mental Health Check-ins
Speak with a counselor or therapist at least once a month.
🏃 Physical Health & Pain Management
🧍♂️ Start Light Physiotherapy
Target chest and upper back muscles to relieve tension and pain.
🪑 Improve Posture & Ergonomics
Adjust your workspace and sitting habits to reduce physical strain.
🧘♀️ Incorporate Daily Stretching & Strength Exercises
Just 10 minutes daily can prevent chronic MSCP.
🍎 Lifestyle Habits
🥗 Eat a Heart-Healthy, Anti-Inflammatory Diet
Include fruits, veggies, whole grains, and omega-3s. Cut back on salt, caffeine, and processed foods.
😴 Maintain a Consistent Sleep Schedule
Aim for 7–8 hours of quality sleep per night.
🚭 Avoid Smoking and Limit Alcohol
These can worsen anxiety, panic, and blood pressure.
📱 Self-Monitoring & Tools
📊 Track Your Mood and Pain Daily
Use a mobile app or journal to identify patterns and triggers.
🩺 Monitor Blood Pressure and Heart Rate
Use a home device or smartwatch to track trends over time.
⌚ Use Wearables for Sleep and Activity Monitoring
Stay informed about your stress levels and sleep quality.
👨⚕️ Medical Support & Education
🏥 Attend Regular Doctor Visits
Review symptoms, blood pressure, and mental health every 4–6 weeks.
🧾 Stay Informed About Your Condition
MSCP is not life-threatening but needs proper care. Learn more to reduce unnecessary worry.
💊 Take Medications as Prescribed
Adhere to your treatment plan for pain, anxiety, and hypertension.
""")
else:
print("""✅ Good Quality of Life
👍 Low Risk of Musculoskeletal Chest Pain (MSCP)
Your current health data indicates a low risk of musculoskeletal chest pain, and your overall quality of life appears stable and positive. Great job! Maintaining both physical and mental well-being is key to preventing future discomfort.
🌟 What This Means:
You are functioning well physically and emotionally.
There are no major signs of MSCP or related psychological distress.
Your lifestyle choices and habits are likely helping protect your health.
🛡️ Keep Up the Good Work With These Tips:
🧘 Stay Mentally Balanced
Continue healthy routines like mindfulness, relaxation techniques, or journaling.
Maintain a positive support system—talk to friends, family, or a counselor when needed.
🏃♂️ Stay Physically Active
Include light strength training or cardio 3–5 times per week.
Stretch regularly to keep muscles and joints flexible.
🍽️ Eat Smart
Stick to a balanced, anti-inflammatory diet.
Stay hydrated and avoid excessive caffeine, sugar, or salty foods.
😴 Sleep Well
Prioritize 7–8 hours of sleep per night.
Maintain a consistent sleep schedule for optimal recovery and mood.
🔍 Continue Monitoring
Track your mood, activity, and vitals using a health app or wearable device.
Stay proactive—early detection is always better than reactive care.
✅ Final Note:
You're on the right path. Continue these healthy habits, and remember: Prevention is better than cure. Even with low risk, staying consistent with your health practices ensures long-term wellness and resilience against stress or future discomfort.
""")