jialitan23 commited on
Commit
cf2392a
·
verified ·
1 Parent(s): 1c5e834

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -18,36 +18,38 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
18
  # Initialize all features to 0
19
  data = {feat: 0 for feat in feature_names}
20
 
21
- # One-hot encode categoricals
22
  data[f'Movement Activity_{movement_activity}'] = 1
23
  data[f'Location_{location}'] = 1
24
  data[f'day_of_week_{day_of_week}'] = 1
25
 
26
- # Set numeric features
27
  data['hour_of_day'] = hour_of_day
28
  data['minute_of_day'] = minute_of_day
29
  data['time_since_last_event'] = time_since_last_event
30
 
31
- # Build DataFrame with columns in correct order
32
  input_df = pd.DataFrame([data], columns=feature_names)
33
 
34
- # Scale all features (must match scaler's expected input columns)
35
- scaled_array = scaler.transform(input_df)
36
- input_df.loc[:, feature_names] = scaled_array
37
 
38
- # Predict probability of fall
39
- prob_fall = model.predict_proba(input_df)[:, 1][0]
40
 
 
 
41
  threshold = 0.4
42
- prediction = "Fall Detected" if prob_fall >= threshold else "No Fall"
43
 
44
- return f"Prediction: {prediction}\nFall Probability: {prob_fall:.2f}"
45
 
46
  except Exception as e:
47
  import traceback
48
  traceback.print_exc()
49
  return f"Error: {str(e)}. Check logs for details."
50
 
 
51
  # Gradio interface setup
52
  iface = gr.Interface(
53
  fn=predict_fall,
 
18
  # Initialize all features to 0
19
  data = {feat: 0 for feat in feature_names}
20
 
21
+ # Set one-hot encoded categorical variables
22
  data[f'Movement Activity_{movement_activity}'] = 1
23
  data[f'Location_{location}'] = 1
24
  data[f'day_of_week_{day_of_week}'] = 1
25
 
26
+ # Set numerical features
27
  data['hour_of_day'] = hour_of_day
28
  data['minute_of_day'] = minute_of_day
29
  data['time_since_last_event'] = time_since_last_event
30
 
31
+ # Build DataFrame in the exact order of feature_names (very important)
32
  input_df = pd.DataFrame([data], columns=feature_names)
33
 
34
+ # Pass columns in correct order to scaler
35
+ scaled_features = scaler.transform(input_df[feature_names])
 
36
 
37
+ # Replace numerical values with scaled values (or all if scaler scales all)
38
+ input_df.loc[:, feature_names] = scaled_features
39
 
40
+ # Predict probability
41
+ pred_proba = model.predict_proba(input_df)[0, 1]
42
  threshold = 0.4
43
+ pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
44
 
45
+ return f"Prediction: {pred_label}\nFall Probability: {pred_proba:.2f}"
46
 
47
  except Exception as e:
48
  import traceback
49
  traceback.print_exc()
50
  return f"Error: {str(e)}. Check logs for details."
51
 
52
+
53
  # Gradio interface setup
54
  iface = gr.Interface(
55
  fn=predict_fall,