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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -15,27 +15,33 @@ 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
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]
@@ -47,7 +53,7 @@ def predict_fall(movement_activity, location, day_of_week, hour_of_day, minute_o
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
 
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]
 
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