jialitan23 commited on
Commit
94679fc
·
verified ·
1 Parent(s): e57b334

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -15,36 +15,36 @@ day_choices = [col.replace('day_of_week_', '') for col in feature_names if col.s
15
 
16
  def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
17
  try:
18
- # Initialize all features to 0 based on model's feature_names (which must be a list)
19
- data = {feat: 0 for feat in feature_names}
20
 
21
- # Set categorical one-hot encoded features
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
- # Create input DataFrame in the order of feature_names
32
  input_df = pd.DataFrame([data], columns=feature_names)
33
 
34
- print("Scaler expects:", scaler.feature_names_in_)
35
- print("Input columns:", input_df.columns.tolist())
 
36
 
37
- # Select columns in scaler's expected order
38
- input_for_scaler = input_df[scaler.feature_names_in_]
39
 
40
- # Transform
41
- scaled_features = scaler.transform(input_for_scaler)
 
42
 
43
- # Replace original columns with scaled data in scaler's order
44
- input_df.loc[:, scaler.feature_names_in_] = scaled_features
45
-
46
- # Predict probability
47
  pred_proba = model.predict_proba(input_df)[0, 1]
 
48
  threshold = 0.4
49
  pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
50
 
@@ -53,7 +53,7 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
53
  except Exception as e:
54
  import traceback
55
  traceback.print_exc()
56
- return f"Error: {str(e)}. Check logs."
57
 
58
 
59
  # Gradio interface setup
 
15
 
16
  def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_of_day, time_since_last_event):
17
  try:
18
+ # 1. Initialize all features to zero using model's expected order
19
+ data = {f: 0 for f in feature_names}
20
 
21
+ # 2. Set one-hot encoded categorical features
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
+ # 3. 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
+ # 4. Create DataFrame with model feature order
32
  input_df = pd.DataFrame([data], columns=feature_names)
33
 
34
+ # 5. Select scaler columns in scaler's order
35
+ scaler_cols = scaler.feature_names_in_
36
+ scaler_df = input_df[scaler_cols]
37
 
38
+ # 6. Transform numeric/scaler features
39
+ scaled_features = scaler.transform(scaler_df)
40
 
41
+ # 7. Replace scaler columns in input_df (which is in model order) with scaled data
42
+ # Use .loc to avoid dtype warning, cast scaled_features to float explicitly
43
+ input_df.loc[:, scaler_cols] = scaled_features.astype(float)
44
 
45
+ # 8. Now predict using the model, passing full input_df in model feature order
 
 
 
46
  pred_proba = model.predict_proba(input_df)[0, 1]
47
+
48
  threshold = 0.4
49
  pred_label = "Fall Detected" if pred_proba >= threshold else "No Fall"
50
 
 
53
  except Exception as e:
54
  import traceback
55
  traceback.print_exc()
56
+ return f"Error: {str(e)}. Check server logs."
57
 
58
 
59
  # Gradio interface setup