danielritchie commited on
Commit
de576e2
1 Parent(s): 100fe27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -42,21 +42,21 @@ model = xgb.XGBClassifier()
42
  model.fit(X_train, y_train)
43
 
44
  def predict(input_data):
45
- # Remove features the user wants to omit (e.g., 'Gender')
46
  for col in X.columns:
47
  if input_data.get(col) is None:
48
- input_data.pop(col, None)
49
-
50
- # Keep only the features the user provided
51
- data = pd.DataFrame([input_data], columns=input_data.keys())
52
-
53
- # Align with model columns and fill missing required columns with defaults
54
- data = data.reindex(columns=X.columns, fill_value=X.mean())
55
 
 
 
56
  prediction = model.predict(data)
57
  return prediction[0]
58
 
59
 
 
60
  # Set up Gradio interface for data exploration
61
  def explore_data(row_number):
62
  return df.iloc[row_number].to_dict()
@@ -70,11 +70,14 @@ with gr.Blocks() as demo:
70
  row_number_input.change(explore_data, inputs=[row_number_input], outputs=[data_output])
71
 
72
  gr.Markdown("## Make a Prediction")
73
- input_components = [gr.Number(label=col) for col in X.columns] # Unpack the input components
 
 
 
74
  output = gr.Textbox(label="Prediction")
75
 
76
  submit_button = gr.Button("Predict")
77
- submit_button.click(predict, inputs=input_components, outputs=[output]) # Pass as separate inputs
78
 
79
  demo.launch()
80
 
 
42
  model.fit(X_train, y_train)
43
 
44
  def predict(input_data):
45
+ # Handle missing values or intentionally omitted fields
46
  for col in X.columns:
47
  if input_data.get(col) is None:
48
+ if X[col].dtype == 'float64': # For numerical features
49
+ input_data[col] = X[col].mean() # Use the mean for missing numerical values
50
+ else: # For categorical features
51
+ input_data[col] = X[col].mode()[0] # Use the mode for missing categorical values
 
 
 
52
 
53
+ # Convert input data to a DataFrame
54
+ data = pd.DataFrame([input_data], columns=X.columns)
55
  prediction = model.predict(data)
56
  return prediction[0]
57
 
58
 
59
+
60
  # Set up Gradio interface for data exploration
61
  def explore_data(row_number):
62
  return df.iloc[row_number].to_dict()
 
70
  row_number_input.change(explore_data, inputs=[row_number_input], outputs=[data_output])
71
 
72
  gr.Markdown("## Make a Prediction")
73
+
74
+ # Create a dictionary for input components
75
+ input_components = {col: gr.Number(label=col) for col in X.columns} # Generate number inputs for each column
76
+
77
  output = gr.Textbox(label="Prediction")
78
 
79
  submit_button = gr.Button("Predict")
80
+ submit_button.click(predict, inputs=[input_components], outputs=[output]) # Pass the dictionary of inputs
81
 
82
  demo.launch()
83