jialitan23 commited on
Commit
1ab61d4
·
verified ·
1 Parent(s): ea0bc09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -25
app.py CHANGED
@@ -10,19 +10,23 @@ scaler = None
10
  encoder = None
11
  all_feature_columns = None
12
  PREDICTION_THRESHOLD = 0.4
 
 
13
  features_to_encode = ['Movement Activity', 'Location', 'day_of_week']
 
14
 
15
  try:
16
  app_dir = os.path.dirname(os.path.abspath(__file__))
17
 
18
- # Load the model and preprocessing objects directly from the current directory
19
  model = joblib.load(os.path.join(app_dir, 'fall_detection_model.joblib'))
20
  scaler = joblib.load(os.path.join(app_dir, 'scaler.joblib'))
21
  encoder = joblib.load(os.path.join(app_dir, 'encoder.joblib'))
22
 
23
  # Get the complete list of column names that the model was trained on
 
24
  encoded_feature_names = encoder.get_feature_names_out(features_to_encode)
25
- all_feature_columns = encoded_feature_names.tolist() + ['time_since_last_event']
26
 
27
  print("Model and preprocessing tools loaded successfully!")
28
 
@@ -37,33 +41,23 @@ def predict_fall(movement_activity, location, day_of_week, time_since_last_event
37
  return "Error: Model not loaded. Check server logs."
38
 
39
  try:
40
- # Create a DataFrame from the inputs with all expected features.
41
- # This is the key fix to ensure all feature columns are present.
42
- input_data = {
43
- 'Movement Activity': [movement_activity],
44
- 'Location': [location],
45
- 'day_of_week': [day_of_week],
46
- 'time_since_last_event': [time_since_last_event]
47
- }
48
- input_df = pd.DataFrame(input_data)
49
 
50
- # Preprocess the input data using the loaded encoder and scaler
51
- encoded_features = encoder.transform(input_df[features_to_encode])
52
- encoded_df = pd.DataFrame(
53
- encoded_features,
54
- columns=encoder.get_feature_names_out(features_to_encode),
55
- index=input_df.index
56
- )
57
-
58
- # Combine numerical and categorical data
59
- processed_df = pd.concat([encoded_df, input_df[['time_since_last_event']]], axis=1)
60
 
61
- # Reindex the DataFrame to ensure it has all the columns the model expects.
62
- # This is the key fix. Any missing columns will be added with a value of 0.
63
- processed_df = processed_df.reindex(columns=all_feature_columns, fill_value=0)
64
 
65
  # Scale the numerical feature
66
- processed_df['time_since_last_event'] = scaler.transform(processed_df[['time_since_last_event']])
67
 
68
  # --- Prediction ---
69
  fall_probability = model.predict_proba(processed_df)[:, 1][0]
 
10
  encoder = None
11
  all_feature_columns = None
12
  PREDICTION_THRESHOLD = 0.4
13
+
14
+ # List of categorical and numerical features used during training
15
  features_to_encode = ['Movement Activity', 'Location', 'day_of_week']
16
+ numerical_features = ['time_since_last_event']
17
 
18
  try:
19
  app_dir = os.path.dirname(os.path.abspath(__file__))
20
 
21
+ # Load the model and preprocessing objects
22
  model = joblib.load(os.path.join(app_dir, 'fall_detection_model.joblib'))
23
  scaler = joblib.load(os.path.join(app_dir, 'scaler.joblib'))
24
  encoder = joblib.load(os.path.join(app_dir, 'encoder.joblib'))
25
 
26
  # Get the complete list of column names that the model was trained on
27
+ # This is the key to preventing the error
28
  encoded_feature_names = encoder.get_feature_names_out(features_to_encode)
29
+ all_feature_columns = encoded_feature_names.tolist() + numerical_features
30
 
31
  print("Model and preprocessing tools loaded successfully!")
32
 
 
41
  return "Error: Model not loaded. Check server logs."
42
 
43
  try:
44
+ # Create a dictionary to build the input DataFrame with all required columns
45
+ input_data = {}
46
+ for col in all_feature_columns:
47
+ input_data[col] = 0.0 # Initialize all columns to 0.0
 
 
 
 
 
48
 
49
+ # Populate the dictionary with the user's input, setting the one-hot encoded
50
+ # columns to 1.0
51
+ input_data[f'Movement Activity_{movement_activity}'] = 1.0
52
+ input_data[f'Location_{location}'] = 1.0
53
+ input_data[f'day_of_week_{day_of_week}'] = 1.0
54
+ input_data['time_since_last_event'] = float(time_since_last_event)
 
 
 
 
55
 
56
+ # Create the DataFrame in the exact order the model expects
57
+ processed_df = pd.DataFrame([input_data], columns=all_feature_columns)
 
58
 
59
  # Scale the numerical feature
60
+ processed_df[numerical_features] = scaler.transform(processed_df[numerical_features])
61
 
62
  # --- Prediction ---
63
  fall_probability = model.predict_proba(processed_df)[:, 1][0]