Spaces:
Sleeping
Sleeping
jeff7522553
commited on
Commit
·
e970db4
1
Parent(s):
1e4bbff
調整畫面小數點呈現
Browse files
app.py
CHANGED
|
@@ -19,7 +19,12 @@ warnings.filterwarnings("ignore", category=FutureWarning)
|
|
| 19 |
# plt.rcParams['font.family'] = ['SimHei']
|
| 20 |
plt.rcParams['axes.unicode_minus'] = False
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def load_data():
|
| 25 |
"""
|
|
@@ -48,6 +53,9 @@ def update_eda_section(selected_features):
|
|
| 48 |
corrs.columns = ['Feature', 'Correlation with Response']
|
| 49 |
first_feature_plot = generate_feature_plot(selected_features[0])
|
| 50 |
plot_selector_update = gr.update(choices=selected_features, value=selected_features[0])
|
|
|
|
|
|
|
|
|
|
| 51 |
return stats, corrs, plot_selector_update, first_feature_plot
|
| 52 |
|
| 53 |
def generate_feature_plot(feature):
|
|
@@ -122,7 +130,13 @@ def train_and_evaluate(history_log, model_name, features, dt_criterion, dt_max_d
|
|
| 122 |
|
| 123 |
# --- 3. 評估與繪圖 ---
|
| 124 |
accuracy = accuracy_score(y_test, y_pred)
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
auc_score = f"ROC-AUC 分數: {roc_auc_score(y_test, y_pred_proba):.4f}"
|
| 127 |
cm = confusion_matrix(y_test, y_pred)
|
| 128 |
fig_cm, ax_cm = plt.subplots(); sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax_cm, xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual 1']); ax_cm.set_title('Confusion Matrix'); ax_cm.set_xlabel('Predicted Label'); ax_cm.set_ylabel('Actual Label'); plt.tight_layout()
|
|
@@ -147,7 +161,7 @@ def train_and_evaluate(history_log, model_name, features, dt_criterion, dt_max_d
|
|
| 147 |
updated_log = [new_log_entry] + history_log
|
| 148 |
log_df = pd.DataFrame(updated_log, columns=LOG_COLUMNS)
|
| 149 |
|
| 150 |
-
return
|
| 151 |
|
| 152 |
# --- Gradio 介面設計 ---
|
| 153 |
LOG_COLUMNS = ["時間", "模型", "特徵", "參數", "準確率"]
|
|
@@ -156,7 +170,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 156 |
# 用於儲存日誌的隱藏狀態元件
|
| 157 |
log_state = gr.State([])
|
| 158 |
|
| 159 |
-
gr.Markdown("#
|
| 160 |
gr.Markdown("在左側選擇特徵並點擊按鈕進行探索,或調整參數後點擊按鈕以訓練模型。")
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column(scale=1):
|
|
@@ -183,7 +197,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 183 |
run_btn = gr.Button("🚀 執行模型訓練", variant="primary")
|
| 184 |
with gr.Column(scale=2):
|
| 185 |
gr.Markdown("## 3. 模型評估結果")
|
| 186 |
-
model_output_report = gr.
|
| 187 |
model_output_auc = gr.Textbox(label="AUC 分數")
|
| 188 |
model_plot_cm = gr.Plot(label="混淆矩陣")
|
| 189 |
model_plot_importance = gr.Plot(label="特徵重要性/係數")
|
|
|
|
| 19 |
# plt.rcParams['font.family'] = ['SimHei']
|
| 20 |
plt.rcParams['axes.unicode_minus'] = False
|
| 21 |
|
| 22 |
+
def processDisplayDataframe(df):
|
| 23 |
+
# 假設 df 已經存在
|
| 24 |
+
num_cols = df.select_dtypes(include=np.number).columns # 先抓出數值欄位名稱
|
| 25 |
|
| 26 |
+
df[num_cols] = df[num_cols].map(lambda x: f"{x:.4f}")
|
| 27 |
+
return df
|
| 28 |
|
| 29 |
def load_data():
|
| 30 |
"""
|
|
|
|
| 53 |
corrs.columns = ['Feature', 'Correlation with Response']
|
| 54 |
first_feature_plot = generate_feature_plot(selected_features[0])
|
| 55 |
plot_selector_update = gr.update(choices=selected_features, value=selected_features[0])
|
| 56 |
+
|
| 57 |
+
stats = processDisplayDataframe(stats)
|
| 58 |
+
corrs = processDisplayDataframe(corrs)
|
| 59 |
return stats, corrs, plot_selector_update, first_feature_plot
|
| 60 |
|
| 61 |
def generate_feature_plot(feature):
|
|
|
|
| 130 |
|
| 131 |
# --- 3. 評估與繪圖 ---
|
| 132 |
accuracy = accuracy_score(y_test, y_pred)
|
| 133 |
+
report_dict = classification_report(y_test, y_pred, target_names=['not purchase insurance (0)', 'purchase insurance (1)'], output_dict=True)
|
| 134 |
+
# 2. 轉成 DataFrame(每個類別一列)
|
| 135 |
+
df_report = pd.DataFrame(report_dict).T # T = transpose,讓 index 變成類別名稱
|
| 136 |
+
df_report.insert(0, "index", df_report.index)
|
| 137 |
+
# print(df_report)
|
| 138 |
+
df_report = processDisplayDataframe(df_report)
|
| 139 |
+
|
| 140 |
auc_score = f"ROC-AUC 分數: {roc_auc_score(y_test, y_pred_proba):.4f}"
|
| 141 |
cm = confusion_matrix(y_test, y_pred)
|
| 142 |
fig_cm, ax_cm = plt.subplots(); sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax_cm, xticklabels=['Predicted 0', 'Predicted 1'], yticklabels=['Actual 0', 'Actual 1']); ax_cm.set_title('Confusion Matrix'); ax_cm.set_xlabel('Predicted Label'); ax_cm.set_ylabel('Actual Label'); plt.tight_layout()
|
|
|
|
| 161 |
updated_log = [new_log_entry] + history_log
|
| 162 |
log_df = pd.DataFrame(updated_log, columns=LOG_COLUMNS)
|
| 163 |
|
| 164 |
+
return df_report, auc_score, fig_cm, fig_imp, log_df, updated_log
|
| 165 |
|
| 166 |
# --- Gradio 介面設計 ---
|
| 167 |
LOG_COLUMNS = ["時間", "模型", "特徵", "參數", "準確率"]
|
|
|
|
| 170 |
# 用於儲存日誌的隱藏狀態元件
|
| 171 |
log_state = gr.State([])
|
| 172 |
|
| 173 |
+
gr.Markdown("# 投保預測模型建置專案")
|
| 174 |
gr.Markdown("在左側選擇特徵並點擊按鈕進行探索,或調整參數後點擊按鈕以訓練模型。")
|
| 175 |
with gr.Row():
|
| 176 |
with gr.Column(scale=1):
|
|
|
|
| 197 |
run_btn = gr.Button("🚀 執行模型訓練", variant="primary")
|
| 198 |
with gr.Column(scale=2):
|
| 199 |
gr.Markdown("## 3. 模型評估結果")
|
| 200 |
+
model_output_report = gr.DataFrame(label="分類報告")
|
| 201 |
model_output_auc = gr.Textbox(label="AUC 分數")
|
| 202 |
model_plot_cm = gr.Plot(label="混淆矩陣")
|
| 203 |
model_plot_importance = gr.Plot(label="特徵重要性/係數")
|