Cycles/xgboost/custom_xgboost.py

40 lines
1.5 KiB
Python
Raw Permalink Normal View History

2025-05-30 20:06:28 +08:00
import xgboost as xgb
import numpy as np
class CustomXGBoostGPU:
def __init__(self, X_train, X_test, y_train, y_test):
self.X_train = X_train.astype(np.float32)
self.X_test = X_test.astype(np.float32)
self.y_train = y_train.astype(np.float32)
self.y_test = y_test.astype(np.float32)
self.model = None
self.params = None # Will be set during training
def train(self, **xgb_params):
params = {
'tree_method': 'hist',
'device': 'cuda',
'objective': 'reg:squarederror',
'eval_metric': 'rmse',
'verbosity': 1,
}
params.update(xgb_params)
self.params = params # Store params for later access
dtrain = xgb.DMatrix(self.X_train, label=self.y_train)
dtest = xgb.DMatrix(self.X_test, label=self.y_test)
evals = [(dtrain, 'train'), (dtest, 'eval')]
self.model = xgb.train(params, dtrain, num_boost_round=100, evals=evals, early_stopping_rounds=10)
return self.model
def predict(self, X):
if self.model is None:
raise ValueError('Model not trained yet.')
dmatrix = xgb.DMatrix(X.astype(np.float32))
return self.model.predict(dmatrix)
def save_model(self, file_path):
"""Save the trained XGBoost model to the specified file path."""
if self.model is None:
raise ValueError('Model not trained yet.')
self.model.save_model(file_path)