plutoa386 commited on
Commit
bbc1725
·
1 Parent(s): 0b96efe

switched from numpy to dmatrix (audio prediction)

Browse files
Files changed (1) hide show
  1. crew.py +24 -6
crew.py CHANGED
@@ -10,6 +10,7 @@ from typing import Union
10
  import shap
11
  import xgboost as xgb
12
  from xgboost import DMatrix
 
13
 
14
  # Load audio model & metadata
15
  audio_model = xgb.Booster()
@@ -73,10 +74,28 @@ def calculate_dementia_risk_audio(docID: str, return_json: bool = False) -> Unio
73
  return {"status": "error", "message": error_msg}
74
  return error_msg
75
 
 
 
 
 
76
  # Predict
77
- prediction = audio_model.predict(combined_features.reshape(1, -1))[0]
78
- probability = audio_model.predict_proba(combined_features.reshape(1, -1))[0]
79
- confidence = float(max(probability))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  risk_level = "High" if prediction == 1 else "Low"
81
  gender_str = "Male" if gender_val == 1 else "Female"
82
 
@@ -225,7 +244,7 @@ def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) ->
225
 
226
  # Predict
227
  prediction = lifestyle_model.predict(dmat)[0]
228
- # For probability, use predict_proba if available, or fallback
229
  try:
230
  # If model supports predict_proba (e.g., scikit-learn wrapper), use it
231
  proba = lifestyle_model.predict_proba(dmat)[0]
@@ -234,7 +253,6 @@ def calculate_dementia_risk_lifestyle(docID: str, return_json: bool = False) ->
234
  # Fallback: use raw prediction scores (if binary classification)
235
  raw_score = lifestyle_model.predict(dmat, output_margin=True)[0]
236
  # Convert to probability via sigmoid (for binary classification)
237
- from math import exp
238
  proba_pos = 1 / (1 + exp(-raw_score))
239
  confidence = proba_pos if prediction == 1 else (1 - proba_pos)
240
 
@@ -314,7 +332,7 @@ def explain_dementia_risk_lifestyle(docID: str) -> str:
314
 
315
  top_indices = np.argsort(-shap_vals)[:5]
316
 
317
- explanation_lines = ["🔍 Top factors increasing dementia risk (lifestyle analysis):"]
318
  for i in top_indices:
319
  feat = lifestyle_feature_names[i]
320
  val = features[i]
 
10
  import shap
11
  import xgboost as xgb
12
  from xgboost import DMatrix
13
+ from math import exp
14
 
15
  # Load audio model & metadata
16
  audio_model = xgb.Booster()
 
74
  return {"status": "error", "message": error_msg}
75
  return error_msg
76
 
77
+ # Convert to DMatrix
78
+ audio_array = np.array(combined_features).reshape(1, -1)
79
+ dmat = DMatrix(audio_array)
80
+
81
  # Predict
82
+ prediction = lifestyle_model.predict(dmat)[0]
83
+ # For confidence
84
+ try:
85
+ # If model supports predict_proba (e.g., scikit-learn wrapper), use it
86
+ proba = audio_model.predict_proba(dmat)[0]
87
+ confidence = float(max(proba))
88
+ except AttributeError:
89
+ # Fallback: use raw prediction scores (if binary classification)
90
+ raw_score = audio_model.predict(dmat, output_margin=True)[0]
91
+ # Convert to probability via sigmoid (for binary classification)
92
+ proba_pos = 1 / (1 + exp(-raw_score))
93
+ confidence = proba_pos if prediction == 1 else (1 - proba_pos)
94
+
95
+
96
+ # prediction = audio_model.predict(combined_features.reshape(1, -1))[0]
97
+ # probability = audio_model.predict_proba(combined_features.reshape(1, -1))[0]
98
+ # confidence = float(max(probability))
99
  risk_level = "High" if prediction == 1 else "Low"
100
  gender_str = "Male" if gender_val == 1 else "Female"
101
 
 
244
 
245
  # Predict
246
  prediction = lifestyle_model.predict(dmat)[0]
247
+ # For confidence
248
  try:
249
  # If model supports predict_proba (e.g., scikit-learn wrapper), use it
250
  proba = lifestyle_model.predict_proba(dmat)[0]
 
253
  # Fallback: use raw prediction scores (if binary classification)
254
  raw_score = lifestyle_model.predict(dmat, output_margin=True)[0]
255
  # Convert to probability via sigmoid (for binary classification)
 
256
  proba_pos = 1 / (1 + exp(-raw_score))
257
  confidence = proba_pos if prediction == 1 else (1 - proba_pos)
258
 
 
332
 
333
  top_indices = np.argsort(-shap_vals)[:5]
334
 
335
+ explanation_lines = ["Top factors increasing dementia risk (lifestyle analysis):"]
336
  for i in top_indices:
337
  feat = lifestyle_feature_names[i]
338
  val = features[i]