You can download this code by clicking the button below.
This code is now available for download.
This function uses the XGBoost library to train a classification model and uses the trained model to make predictions on the test data.
Technology Stack : XGBoost, NumPy, Pandas, Scikit-learn
Code Type : The type of code
Code Difficulty : Intermediate
import xgboost as xgb
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
def random_xgb_model(X, y):
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a DMatrix object for the training data
dtrain = xgb.DMatrix(X_train, label=y_train)
# Create a DMatrix object for the testing data
dtest = xgb.DMatrix(X_test, label=y_test)
# Define the parameters for the XGBoost model
params = {
'max_depth': 3,
'eta': 0.1,
'objective': 'binary:logistic',
'eval_metric': 'logloss'
}
# Train the model
bst = xgb.train(params, dtrain, evals=[(dtest, 'eval')], early_stopping_rounds=10)
# Make predictions on the test set
y_pred = bst.predict(dtest)
return y_pred
# Code Information