Skip to content

kellyliu06/restful_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

使用 Restful API 與 Ajax 來進行機器學習模型訓練與預測

安裝套件

pip install -r titanic_restful_project/requirements.txt
套件 版本
Flask 3.1.3
pandas 3.0.3
scikit-learn 1.8.0
xgboost 3.3.0
joblib 1.5.3
numpy 2.4.6

版本號可用 pip listconda list 查詢,請依實際環境自行調整。

執行方法

# 初始化資料庫(匯入 titanic.csv 到 SQLite)
python titanic_restful_project/init_db.py

# 啟動 Flask 伺服器
python titanic_restful_project/app.py

開啟瀏覽器前往 http://127.0.0.1:5000

說明

使用模型:XGBoost(XGBClassifier)

使用 GridSearchCV 進行 5-fold 交叉驗證,搜尋以下超參數組合(共 16 種):

超參數 搜尋範圍 說明
n_estimators 100, 200 決策樹棵數
max_depth 3, 5 每棵樹最大深度
learning_rate 0.05, 0.1 學習率
subsample 0.8, 1.0 每棵樹使用的資料比例

訓練時以 accuracy 為評估指標,資料切割 80% 訓練 / 20% 測試(stratify=y 保持存活比例一致)。

特徵工程

  • Sex:male → 0,female → 1
  • Embarked:C → 0,Q → 1,S → 2
  • Age / Fare:缺失值以中位數填補
  • 使用欄位:Pclass, Sex, Age, SibSp, Parch, Fare, Embarked

主要功能

  • 乘客資料 CRUD:新增、查詢、修改、刪除 Titanic 乘客資料(含分頁與搜尋)
  • ML 訓練頁面 (/ml):一鍵觸發背景訓練,Ajax 即時顯示訓練進度與最佳超參數
  • 預測頁面 (/ml/predict):輸入單筆資料或上傳 CSV 批次預測,顯示生還/未生還與機率

成果

執行過程的影片連結 :

Titanic機器學習網頁展示

專案結構

models/
    titanic_model.joblib        # 訓練完成的 XGBoost 模型
titanic_restful_project/
    app.py                      # Flask 主程式(API + 頁面路由)
    init_db.py                  # 資料庫初始化腳本
    titanic.csv                 # 原始資料集
    requirements.txt            # 套件清單
    ML學習筆記.md               # 學習過程筆記
    templates/
        index.html              # 乘客列表頁
        new.html                # 新增乘客頁
        edit.html               # 編輯乘客頁
        ml.html                 # ML 訓練頁
        predict.html            # 預測頁
README.md

About

titanic_ML_restful_project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors