Spaces:
Sleeping
Sleeping
Update model_predictor.py
Browse files- model_predictor.py +15 -65
model_predictor.py
CHANGED
|
@@ -74,73 +74,23 @@ class XGBoostModel:
|
|
| 74 |
return False
|
| 75 |
|
| 76 |
def load_scaler(self, scaler_path):
|
| 77 |
-
"""
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
scaler_path (str): 標準化器檔案路徑 (.pkl 格式)
|
| 82 |
-
|
| 83 |
-
Returns:
|
| 84 |
-
bool: 是否成功載入
|
| 85 |
-
"""
|
| 86 |
-
try:
|
| 87 |
-
if os.path.exists(scaler_path):
|
| 88 |
-
self.scaler = joblib.load(scaler_path)
|
| 89 |
-
print(f"成功載入標準化器:{scaler_path}")
|
| 90 |
-
return True
|
| 91 |
-
else:
|
| 92 |
-
print(f"警告:找不到標準化器檔案 {scaler_path}")
|
| 93 |
-
print("將使用預設標準化器")
|
| 94 |
-
self.scaler = StandardScaler()
|
| 95 |
-
return False
|
| 96 |
-
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"載入標準化器時發生錯誤:{e}")
|
| 99 |
-
self.scaler = StandardScaler()
|
| 100 |
-
return False
|
| 101 |
|
| 102 |
def preprocess_features(self, input_df):
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
missing_features = [f for f in self.feature_columns if f not in input_df.columns]
|
| 115 |
-
if missing_features:
|
| 116 |
-
print(f"警告:缺少以下特徵:{missing_features}")
|
| 117 |
-
# 用 0 填補缺少的特徵
|
| 118 |
-
for feature in missing_features:
|
| 119 |
-
input_df[feature] = 0
|
| 120 |
-
|
| 121 |
-
# 按照預期順序重新排列特徵
|
| 122 |
-
input_df = input_df[self.feature_columns]
|
| 123 |
-
|
| 124 |
-
# 處理 NaN 值
|
| 125 |
-
input_df = input_df.fillna(0)
|
| 126 |
-
|
| 127 |
-
# 如果有標準化器,進行標準化
|
| 128 |
-
if self.scaler is not None:
|
| 129 |
-
try:
|
| 130 |
-
# 嘗試使用已訓練的標準化器
|
| 131 |
-
scaled_features = self.scaler.transform(input_df)
|
| 132 |
-
input_df = pd.DataFrame(scaled_features,
|
| 133 |
-
columns=input_df.columns,
|
| 134 |
-
index=input_df.index)
|
| 135 |
-
except Exception as scaler_error:
|
| 136 |
-
print(f"標準化過程發生錯誤:{scaler_error}")
|
| 137 |
-
print("跳過標準化步驟")
|
| 138 |
-
|
| 139 |
-
return input_df
|
| 140 |
-
|
| 141 |
-
except Exception as e:
|
| 142 |
-
print(f"特徵預處理時發生錯誤:{e}")
|
| 143 |
-
return input_df
|
| 144 |
|
| 145 |
def predict(self, model_name, input_df):
|
| 146 |
"""
|
|
|
|
| 74 |
return False
|
| 75 |
|
| 76 |
def load_scaler(self, scaler_path):
|
| 77 |
+
"""停用標準化流程"""
|
| 78 |
+
print("⚠️ 已停用標準化:模型使用原始特徵進行預測。")
|
| 79 |
+
self.scaler = None
|
| 80 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
def preprocess_features(self, input_df):
|
| 83 |
+
# 確保特徵齊全
|
| 84 |
+
missing_features = [f for f in self.feature_columns if f not in input_df.columns]
|
| 85 |
+
if missing_features:
|
| 86 |
+
print(f"警告:缺少以下特徵:{missing_features}")
|
| 87 |
+
for feature in missing_features:
|
| 88 |
+
input_df[feature] = 0
|
| 89 |
+
|
| 90 |
+
input_df = input_df[self.feature_columns].fillna(0)
|
| 91 |
+
|
| 92 |
+
# ✅ 直接回傳原始特徵
|
| 93 |
+
return input_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def predict(self, model_name, input_df):
|
| 96 |
"""
|