Random XGBoost Classification with Dataset Splitting

  • Share this:

Code introduction


This function uses the XGBoost library to split the given dataset randomly, then trains an XGBoost classifier, and makes predictions on the test set.


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_classification(data):
    # Split the data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(data.drop('target', axis=1), data['target'], test_size=0.2, random_state=42)
    
    # Initialize the XGBoost classifier
    xgb_model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='logloss')
    
    # Train the model
    xgb_model.fit(X_train, y_train)
    
    # Predict the labels for the test set
    predictions = xgb_model.predict(X_test)
    
    # Return the predictions
    return predictions