You can download this code by clicking the button below.
This code is now available for download.
This function generates a random XGBoost model for classification or regression tasks. It takes a feature matrix X and a label vector y as input and returns a trained XGBoost model.
Technology Stack : XGBoost, NumPy
Code Type : Custom function
Code Difficulty : Intermediate
import xgboost as xgb
import numpy as np
import random
def generate_random_model(X, y, num_rounds=100):
"""
Generates a random XGBoost model.
"""
# Define the parameters for the XGBoost model
params = {
'max_depth': random.randint(1, 6),
'eta': random.uniform(0.01, 0.3),
'subsample': random.uniform(0.5, 1.0),
'colsample_bytree': random.uniform(0.5, 1.0)
}
# Create the DMatrix
dtrain = xgb.DMatrix(X, label=y)
# Train the model
bst = xgb.train(params, dtrain, num_rounds)
return bst