tututz commited on
Commit
0342a0f
·
verified ·
1 Parent(s): 428e4ad

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +48 -3
main.py CHANGED
@@ -211,14 +211,59 @@ def predict_risk(student_data: StudentFeatures):
211
  input_encoded = input_encoded.reindex(columns=MODEL_FEATURES, fill_value=False)
212
 
213
  try:
214
- prediction = ml_model.predict(input_encoded)
215
  prediction_proba = ml_model.predict_proba(input_encoded)
216
  classes = ml_model.classes_
217
  probabilities = dict(zip(classes, prediction_proba[0]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
  return {
220
- "prediction": prediction[0],
221
- "probabilities": probabilities
 
222
  }
223
  except Exception as e:
224
  raise HTTPException(status_code=500, detail=f"Terjadi kesalahan saat prediksi: {e}")
 
211
  input_encoded = input_encoded.reindex(columns=MODEL_FEATURES, fill_value=False)
212
 
213
  try:
214
+ prediction_val = ml_model.predict(input_encoded)[0]
215
  prediction_proba = ml_model.predict_proba(input_encoded)
216
  classes = ml_model.classes_
217
  probabilities = dict(zip(classes, prediction_proba[0]))
218
+ explanation_str = ""
219
+
220
+ if hasattr(ml_model, 'tree_'):
221
+ try:
222
+ tree = ml_model.tree_
223
+ feature_names = MODEL_FEATURES
224
+
225
+ # Dapatkan path yang diambil sampel
226
+ path = ml_model.decision_path(input_encoded)
227
+ # Dapatkan node indices untuk sampel pertama
228
+ node_indices = path.indices[path.indptr[0]:path.indptr[1]]
229
+
230
+ rules = []
231
+ # Iterasi semua node di path, *kecuali* node terakhir (leaf)
232
+ for node_id in node_indices[:-1]:
233
+ # Dapatkan info split dari tree_ object
234
+ feature_index = tree.feature[node_id]
235
+ feature_name = feature_names[feature_index]
236
+ threshold = tree.threshold[node_id]
237
+
238
+ # Ambil nilai aktual sampel untuk fitur ini
239
+ sample_value = input_encoded.iloc[0, feature_index]
240
+
241
+ # Cek ke arah mana sampel pergi
242
+ if sample_value <= threshold:
243
+ # Jika True, sampel pergi ke kiri (<=)
244
+ rule = f"{feature_name} <= {threshold:.2f}"
245
+ else:
246
+ # Jika False, sampel pergi ke kanan (>)
247
+ rule = f"{feature_name} > {threshold:.2f}"
248
+
249
+ rules.append(rule)
250
+
251
+ if rules:
252
+ # Gabungkan semua aturan menjadi satu string
253
+ factors_str = ", ".join(rules)
254
+ explanation_str = f"Terdapat beberapa faktor yang mempengaruhi kategori '{prediction_val}', beberapa di antaranya adalah: {factors_str}."
255
+ else:
256
+ explanation_str = f"Prediksi '{prediction_val}' dibuat tanpa ada split (kemungkinan node root)."
257
+
258
+ except Exception as e:
259
+ explanation_str = f"Gagal membuat penjelasan: {str(e)}"
260
+ else:
261
+ explanation_str = "Penjelasan (decision path) tidak tersedia untuk tipe model ini."
262
 
263
  return {
264
+ "prediction": prediction_val,
265
+ "probabilities": probabilities,
266
+ "explanation": explanation_str
267
  }
268
  except Exception as e:
269
  raise HTTPException(status_code=500, detail=f"Terjadi kesalahan saat prediksi: {e}")