Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,6 +69,7 @@ ship_type = st.selectbox("船舶種類", list(ship_type_to_code.keys()))
|
|
| 69 |
|
| 70 |
if st.button("🔮 開始預測"):
|
| 71 |
ship_type_encoded = ship_type_to_code[ship_type]
|
|
|
|
| 72 |
input_df = pd.DataFrame({
|
| 73 |
"航線組合數": [port_count],
|
| 74 |
"年": [year],
|
|
@@ -80,25 +81,23 @@ if st.button("🔮 開始預測"):
|
|
| 80 |
|
| 81 |
st.subheader("🧠 模型決策解釋圖(SHAP Waterfall plot)")
|
| 82 |
|
| 83 |
-
shap_values = explainer(input_df)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
feature_name_map = {
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
}
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
if text.get_text().startswith('\u2212'):
|
| 100 |
-
text.set_text(text.get_text().replace('\u2212', '-'))
|
| 101 |
-
|
| 102 |
-
st.pyplot(ax.figure)
|
| 103 |
-
plt.close(ax.figure)
|
| 104 |
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if st.button("🔮 開始預測"):
|
| 71 |
ship_type_encoded = ship_type_to_code[ship_type]
|
| 72 |
+
# input_df 用中文欄位名,符合模型訓練時格式
|
| 73 |
input_df = pd.DataFrame({
|
| 74 |
"航線組合數": [port_count],
|
| 75 |
"年": [year],
|
|
|
|
| 81 |
|
| 82 |
st.subheader("🧠 模型決策解釋圖(SHAP Waterfall plot)")
|
| 83 |
|
| 84 |
+
shap_values = explainer(input_df)
|
| 85 |
|
| 86 |
+
# 把SHAP特徵名稱轉成英文,但不改input_df欄位名稱
|
| 87 |
+
feature_name_map = {
|
| 88 |
+
"航線組合數": "Route Count",
|
| 89 |
+
"年": "Year",
|
| 90 |
+
"月": "Month",
|
| 91 |
+
"船舶種類_編碼": "Ship Type Code"
|
| 92 |
+
}
|
| 93 |
+
shap_values.feature_names = [feature_name_map.get(f, f) for f in shap_values.feature_names]
|
| 94 |
|
| 95 |
+
ax = shap.plots.waterfall(shap_values[0], show=False)
|
| 96 |
|
| 97 |
+
# 負號替換(避免顯示問題)
|
| 98 |
+
for text in ax.texts:
|
| 99 |
+
if text.get_text().startswith('\u2212'):
|
| 100 |
+
text.set_text(text.get_text().replace('\u2212', '-'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
st.pyplot(ax.figure)
|
| 103 |
+
plt.close(ax.figure)
|