CosmickVisions commited on
Commit
5551882
·
verified ·
1 Parent(s): 576736c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -30,6 +30,7 @@ import time
30
  from sklearn.cluster import KMeans
31
  import scipy.stats as stats
32
  import mimetypes
 
33
  import matplotlib.pyplot as plt
34
  from sklearn.model_selection import learning_curve
35
 
@@ -869,6 +870,7 @@ elif app_mode == "Model Training":
869
  st.write(f"Accuracy: {accuracy:.4f}")
870
  except Exception as e: #local error
871
  st.error(f"An error occurred during model evaluation: {e}")
 
872
  elif app_mode == "Predictions":
873
  st.title("🔮 Make Predictions")
874
 
@@ -883,7 +885,7 @@ elif app_mode == "Predictions":
883
  except AttributeError as e:
884
  st.error(f"Error accessing model transformers: {e}. Please ensure a valid model is trained and loaded.")
885
  st.stop()
886
-
887
  model_is_classification = hasattr(model, 'predict_proba') # Check for classification or other problem
888
  if not set(model_columns).issubset(set(df.columns)): #Fixed comparison
889
  st.error("The model was trained on a dataframe that contains different columns than the currently uploaded dataframe. Please upload the correct dataframe.")
@@ -897,7 +899,7 @@ elif app_mode == "Predictions":
897
  else:
898
  input_data[col] = st.selectbox(f"Select {col}", df[col].unique())
899
 
900
- # Make Prediction Button
901
  if st.button("Make Prediction"):
902
  try:
903
  input_df = pd.DataFrame([input_data])
@@ -924,17 +926,17 @@ elif app_mode == "Predictions":
924
 
925
  shap.initjs()
926
  fig_shap, ax_shap = plt.subplots(1, figsize = (10,10))
927
- shap.summary_plot(shap_values, features = X_train, feature_names = feature_columns, plot_type = "bar")#plot for multi class labels
928
  st.pyplot(fig_shap) #Show the figure
929
  except Exception as e:
930
  st.write(f"Can show shap values on tree based model: {e}") #Show error
931
  # Additional Feedback (Example for Classification)
932
  if hasattr(st.session_state.model.steps[-1][1], 'predict_proba'): #If the end variable has predict_proba and is therefore a predictor
933
- probabilities = st.session_state.model.predict_proba(input_df)[0]
934
  st.write("Predicted Probabilities:")
935
  st.write(probabilities) #write here
936
  except Exception as e:
937
- st.error(f"An error occurred during prediction: {e}") #Base case error
938
 
939
  #Add batch prediction section in prediction tab
940
  st.subheader("Batch Predictions")
 
30
  from sklearn.cluster import KMeans
31
  import scipy.stats as stats
32
  import mimetypes
33
+ import shap
34
  import matplotlib.pyplot as plt
35
  from sklearn.model_selection import learning_curve
36
 
 
870
  st.write(f"Accuracy: {accuracy:.4f}")
871
  except Exception as e: #local error
872
  st.error(f"An error occurred during model evaluation: {e}")
873
+
874
  elif app_mode == "Predictions":
875
  st.title("🔮 Make Predictions")
876
 
 
885
  except AttributeError as e:
886
  st.error(f"Error accessing model transformers: {e}. Please ensure a valid model is trained and loaded.")
887
  st.stop()
888
+
889
  model_is_classification = hasattr(model, 'predict_proba') # Check for classification or other problem
890
  if not set(model_columns).issubset(set(df.columns)): #Fixed comparison
891
  st.error("The model was trained on a dataframe that contains different columns than the currently uploaded dataframe. Please upload the correct dataframe.")
 
899
  else:
900
  input_data[col] = st.selectbox(f"Select {col}", df[col].unique())
901
 
902
+ # Prediction Button
903
  if st.button("Make Prediction"):
904
  try:
905
  input_df = pd.DataFrame([input_data])
 
926
 
927
  shap.initjs()
928
  fig_shap, ax_shap = plt.subplots(1, figsize = (10,10))
929
+ shap.summary_plot(shap_values, features = input_processed, feature_names = model_columns, plot_type = "bar")#plot for multi class labels
930
  st.pyplot(fig_shap) #Show the figure
931
  except Exception as e:
932
  st.write(f"Can show shap values on tree based model: {e}") #Show error
933
  # Additional Feedback (Example for Classification)
934
  if hasattr(st.session_state.model.steps[-1][1], 'predict_proba'): #If the end variable has predict_proba and is therefore a predictor
935
+ probabilities = st.session_state.model.predict_proba(input_processed)[0]
936
  st.write("Predicted Probabilities:")
937
  st.write(probabilities) #write here
938
  except Exception as e:
939
+ st.error(f"An error occurred during prediction: {e}")
940
 
941
  #Add batch prediction section in prediction tab
942
  st.subheader("Batch Predictions")