코랩에서 베이스라인 다운로드 받으셔서
flow_validation 기준으로 작업하시면 됩니다
train만 바꾸시면 될 듯
Train.py
import numpy as np
import sys
import pickle
import xgboost as xgb
x_train_path = sys.argv[1]
y_train_path = sys.argv[2]
model_save_path = sys.argv[3]
x_train = np.load(x_train_path, allow_pickle=True)
y_train = np.load(y_train_path, allow_pickle=True)
# Convert to DMatrix
dtrain = xgb.DMatrix(x_train, label=y_train)
# Set parameters
params = {
'base_score': 0.5,
'booster': 'gbtree',
'objective': 'reg:squarederror',
'max_depth': 3,
'learning_rate': 0.05,
'subsample': 0.5, # Consider using subsample to reduce memory usage
'max_bin': 256, # Reduce number of bins
'device': 'cuda', # Use GPU accelerated algorithm
}
# Custom evaluation function for MAPE
def mape(preds, dtrain):
labels = dtrain.get_label()
return 'MAPE', np.mean(np.abs((labels - preds) / (labels + 1e-6))) * 100 # Avoid division by zero
# Train the XGBoost model with custom evaluation metric
model = xgb.train(params, dtrain, evals=[(dtrain, 'train')],
custom_metric=mape, # Use the custom evaluation function
verbose_eval=100, num_boost_round=400)
# save
pickle.dump(model, open(model_save_path, "wb")):)
model_inference.py
import numpy as np
import sys
import pickle
import xgboost as xgb
model_path = sys.argv[1]
x_test_path = sys.argv[2]
y_pred_save_path = sys.argv[3]
# 모델 로딩
with open(model_path, "rb") as f:
model = pickle.load(f)
x_test = np.load(x_test_path, allow_pickle=True)
# Convert numpy array to DMatrix
dtest = xgb.DMatrix(x_test)
y_pred = model.predict(dtest)
np.save(y_pred_save_path, y_pred)