Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import flet as ft
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.ensemble import RandomForestRegressor
|
7 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
8 |
+
import asyncio
|
9 |
+
|
10 |
+
async def main(page: ft.Page):
|
11 |
+
page.title = "C14_1 預測器"
|
12 |
+
page.theme_mode = "light"
|
13 |
+
page.padding = 50
|
14 |
+
page.scroll = "adaptive"
|
15 |
+
|
16 |
+
# 載入數據
|
17 |
+
def load_data():
|
18 |
+
data = pd.DataFrame({
|
19 |
+
'C14_1': np.random.rand(100),
|
20 |
+
'blood': np.random.rand(100),
|
21 |
+
'c7': np.random.rand(100),
|
22 |
+
'c17': np.random.rand(100),
|
23 |
+
'c22': np.random.rand(100),
|
24 |
+
'c17_d': np.random.rand(100),
|
25 |
+
'c22_d': np.random.rand(100),
|
26 |
+
'Skinprick': np.random.rand(100),
|
27 |
+
'MAST': np.random.rand(100),
|
28 |
+
'sex': np.random.randint(0, 2, 100),
|
29 |
+
'c12': np.random.rand(100),
|
30 |
+
'c14': np.random.rand(100),
|
31 |
+
'c18': np.random.rand(100),
|
32 |
+
'c20': np.random.rand(100),
|
33 |
+
'f27_1': np.random.rand(100),
|
34 |
+
'h12': np.random.rand(100)
|
35 |
+
})
|
36 |
+
return data
|
37 |
+
|
38 |
+
data = load_data()
|
39 |
+
|
40 |
+
# 準備特徵和目標變量
|
41 |
+
target_column = 'C14_1'
|
42 |
+
X = data.drop([target_column], axis=1)
|
43 |
+
y = data[target_column]
|
44 |
+
|
45 |
+
# 分割數據
|
46 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
47 |
+
|
48 |
+
# 訓練模型
|
49 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
50 |
+
model.fit(X_train, y_train)
|
51 |
+
|
52 |
+
# 創建輸入控件
|
53 |
+
input_controls = {}
|
54 |
+
for column in X.columns:
|
55 |
+
input_controls[column] = ft.Slider(
|
56 |
+
min=float(X[column].min()),
|
57 |
+
max=float(X[column].max()),
|
58 |
+
value=float(X[column].mean()),
|
59 |
+
label=f"{column}",
|
60 |
+
divisions=100,
|
61 |
+
)
|
62 |
+
|
63 |
+
# 預測結果文本
|
64 |
+
prediction_text = ft.Text(size=20)
|
65 |
+
|
66 |
+
# 模型性能文本
|
67 |
+
mse_text = ft.Text(size=16)
|
68 |
+
r2_text = ft.Text(size=16)
|
69 |
+
|
70 |
+
# 特徵重要性文本
|
71 |
+
feature_importance_text = ft.Text(size=16)
|
72 |
+
|
73 |
+
def predict(e):
|
74 |
+
input_data = {col: control.value for col, control in input_controls.items()}
|
75 |
+
input_df = pd.DataFrame([input_data])
|
76 |
+
prediction = model.predict(input_df)[0]
|
77 |
+
prediction_text.value = f"預測的 C14_1 值為: {prediction:.2f}"
|
78 |
+
page.update()
|
79 |
+
|
80 |
+
def show_model_performance(e):
|
81 |
+
y_pred = model.predict(X_test)
|
82 |
+
mse = mean_squared_error(y_test, y_pred)
|
83 |
+
r2 = r2_score(y_test, y_pred)
|
84 |
+
mse_text.value = f"均方誤差 (MSE): {mse:.2f}"
|
85 |
+
r2_text.value = f"決定係數 (R²): {r2:.2f}"
|
86 |
+
page.update()
|
87 |
+
|
88 |
+
def show_feature_importance(e):
|
89 |
+
feature_importance = pd.DataFrame({
|
90 |
+
'feature': X.columns,
|
91 |
+
'importance': model.feature_importances_
|
92 |
+
}).sort_values('importance', ascending=False)
|
93 |
+
importance_str = "\n".join([f"{row['feature']}: {row['importance']:.4f}" for _, row in feature_importance.iterrows()])
|
94 |
+
feature_importance_text.value = f"特徵重要性:\n{importance_str}"
|
95 |
+
page.update()
|
96 |
+
|
97 |
+
# 創建頁面佈局
|
98 |
+
await page.add_async(
|
99 |
+
ft.Text("C14_1 預測器", size=30, weight="bold"),
|
100 |
+
ft.Column([control for control in input_controls.values()]),
|
101 |
+
ft.ElevatedButton("預測", on_click=predict),
|
102 |
+
prediction_text,
|
103 |
+
ft.ElevatedButton("顯示模型性能", on_click=show_model_performance),
|
104 |
+
mse_text,
|
105 |
+
r2_text,
|
106 |
+
ft.ElevatedButton("顯示特徵重要性", on_click=show_feature_importance),
|
107 |
+
feature_importance_text,
|
108 |
+
)
|
109 |
+
|
110 |
+
# 運行應用
|
111 |
+
async def run_app():
|
112 |
+
await ft.app_async(target=main, view=ft.WEB_BROWSER)
|
113 |
+
|
114 |
+
# 在標準Python環境中運行
|
115 |
+
asyncio.run(run_app())
|