@@ -68,16 +68,27 @@ def _load_models(self):
6868 except Exception as e :
6969 print (f"WARNING: XGBoost pickle load failed: { e } " )
7070 if not xgb_loaded :
71+ # Try loading as XGBRegressor JSON
7172 try :
7273 xgb_model = xgb .XGBRegressor ()
7374 xgb_model .load_model (f"{ self .models_dir } /xgboost_model.json" )
7475 self .models ["xgboost" ] = xgb_model
75- print ("XGBoost loaded (JSON)" )
76+ print ("XGBoost loaded (JSON as XGBRegressor )" )
7677 xgb_loaded = True
7778 except Exception as e :
78- print (f"WARNING: XGBoost JSON load failed: { e } " )
79+ print (f"WARNING: XGBoost JSON load as XGBRegressor failed: { e } " )
7980 if not xgb_loaded :
80- print ("WARNING: XGBoost not loaded: both pickle and JSON failed." )
81+ # Try loading as Booster (if model was saved using get_booster().save_model)
82+ try :
83+ booster = xgb .Booster ()
84+ booster .load_model (f"{ self .models_dir } /xgboost_model.json" )
85+ self .models ["xgboost" ] = booster
86+ print ("XGBoost loaded (JSON as Booster)" )
87+ xgb_loaded = True
88+ except Exception as e :
89+ print (f"WARNING: XGBoost JSON load as Booster failed: { e } " )
90+ if not xgb_loaded :
91+ print ("WARNING: XGBoost not loaded: all formats failed." )
8192
8293 if TENSORFLOW_AVAILABLE :
8394 try :
@@ -281,7 +292,13 @@ def predict_single_step(self, features: np.ndarray, model_name: str = "xgboost")
281292 if self .aqi_index is not None :
282293 features_scaled = np .delete (features_scaled , self .aqi_index , axis = 1 )
283294
284- prediction = self .models [model_name ].predict (features_scaled )[0 ]
295+ model = self .models [model_name ]
296+ # If Booster, use .predict with DMatrix
297+ if isinstance (model , xgb .Booster ):
298+ dmatrix = xgb .DMatrix (features_scaled )
299+ prediction = model .predict (dmatrix )[0 ]
300+ else :
301+ prediction = model .predict (features_scaled )[0 ]
285302
286303 return max (0 , prediction ) # AQI can't be negative
287304
0 commit comments