ArturG9 commited on
Commit
b60623e
·
verified ·
1 Parent(s): b54f628

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -13
app.py CHANGED
@@ -7,14 +7,14 @@ import pandas as pd
7
  import matplotlib.pyplot as plt
8
 
9
 
10
- # Load the LightGBM model and other necessary objects
11
  with open('lgb1_model.pkl', 'rb') as f:
12
  lgb1 = pickle.load(f)
13
 
14
  categorical_features = joblib.load("categorical_features.joblib")
15
  encoder = joblib.load("encoder.joblib")
16
 
17
- # Sidebar option to select the dashboard
18
  option = st.sidebar.selectbox("Which dashboard?", ("Model information", "Stroke prediction"))
19
  st.title(option)
20
 
@@ -24,7 +24,7 @@ def get_pred():
24
  """
25
  st.header("Stroke probability calculator ")
26
 
27
- # User input for prediction
28
  gender = st.selectbox("Select gender: ", ["Male", "Female", 'Other'])
29
  work_type = st.selectbox("Work type: ", ["Private", "Self_employed", 'children', 'Govt_job', 'Never_worked'])
30
  residence_status = st.selectbox("Residence status: ", ["Urban", "Rural"])
@@ -36,7 +36,7 @@ def get_pred():
36
  avg_glucosis_lvl = st.slider("Average glucosis level: ", 50, 280)
37
  bmi = st.slider("Input Bmi: ", 10, 100)
38
 
39
- # User input data
40
  data = {
41
  "gender": gender,
42
  "work_type": work_type,
@@ -52,34 +52,28 @@ def get_pred():
52
 
53
 
54
  if st.button("Predict"):
55
- # Convert input data to a DataFrame
56
  X = pd.DataFrame([data])
57
-
58
-
59
  encoded_features = encoder.transform(X[categorical_features])
60
 
61
-
62
  feature_names = encoder.get_feature_names_out(input_features=categorical_features)
63
 
64
-
65
  encoded_df = pd.DataFrame(encoded_features, columns=feature_names)
 
66
  X_encoded = pd.concat([X.drop(columns=categorical_features), encoded_df], axis=1)
67
 
68
-
69
  prediction_proba = lgb1.predict_proba(X_encoded)
70
 
71
-
72
  explainer = shap.TreeExplainer(lgb1)
 
73
  shap_values = explainer.shap_values(X_encoded)
74
 
75
-
76
  probability = prediction_proba[0, 1] # Assuming binary classification
77
  st.subheader(f"The predicted probability of stroke is {probability}.")
78
  st.subheader("IF you see result , higher than 0.3, we advice you to see a doctor")
79
  st.header("Shap forceplot")
80
  st.subheader("Features values impact on model made prediction")
81
 
82
-
83
  shap.force_plot(explainer.expected_value[1], shap_values[1], features=X_encoded.iloc[0, :], matplotlib=True)
84
 
85
 
 
7
  import matplotlib.pyplot as plt
8
 
9
 
10
+
11
  with open('lgb1_model.pkl', 'rb') as f:
12
  lgb1 = pickle.load(f)
13
 
14
  categorical_features = joblib.load("categorical_features.joblib")
15
  encoder = joblib.load("encoder.joblib")
16
 
17
+
18
  option = st.sidebar.selectbox("Which dashboard?", ("Model information", "Stroke prediction"))
19
  st.title(option)
20
 
 
24
  """
25
  st.header("Stroke probability calculator ")
26
 
27
+
28
  gender = st.selectbox("Select gender: ", ["Male", "Female", 'Other'])
29
  work_type = st.selectbox("Work type: ", ["Private", "Self_employed", 'children', 'Govt_job', 'Never_worked'])
30
  residence_status = st.selectbox("Residence status: ", ["Urban", "Rural"])
 
36
  avg_glucosis_lvl = st.slider("Average glucosis level: ", 50, 280)
37
  bmi = st.slider("Input Bmi: ", 10, 100)
38
 
39
+
40
  data = {
41
  "gender": gender,
42
  "work_type": work_type,
 
52
 
53
 
54
  if st.button("Predict"):
 
55
  X = pd.DataFrame([data])
56
+
 
57
  encoded_features = encoder.transform(X[categorical_features])
58
 
 
59
  feature_names = encoder.get_feature_names_out(input_features=categorical_features)
60
 
 
61
  encoded_df = pd.DataFrame(encoded_features, columns=feature_names)
62
+
63
  X_encoded = pd.concat([X.drop(columns=categorical_features), encoded_df], axis=1)
64
 
 
65
  prediction_proba = lgb1.predict_proba(X_encoded)
66
 
 
67
  explainer = shap.TreeExplainer(lgb1)
68
+
69
  shap_values = explainer.shap_values(X_encoded)
70
 
 
71
  probability = prediction_proba[0, 1] # Assuming binary classification
72
  st.subheader(f"The predicted probability of stroke is {probability}.")
73
  st.subheader("IF you see result , higher than 0.3, we advice you to see a doctor")
74
  st.header("Shap forceplot")
75
  st.subheader("Features values impact on model made prediction")
76
 
 
77
  shap.force_plot(explainer.expected_value[1], shap_values[1], features=X_encoded.iloc[0, :], matplotlib=True)
78
 
79