XGBoosting Home | About | Contact | Examples

XGBoost Convert Python List to DMatrix

When working with XGBoost in Python, you might have your data stored in Python lists.

While XGBoost’s train() function can accept lists directly, converting them to a DMatrix object can offer better performance and flexibility.

Here’s how you can convert Python lists to a DMatrix and use it to train an XGBoost model:

from xgboost import DMatrix, train

# Create synthetic data using lists
X = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]  # Feature matrix
y = [0, 1, 1]  # Binary target vector

# Convert lists to DMatrix
dmatrix = DMatrix(data=X, label=y)

# Set XGBoost parameters
params = {
    'objective': 'binary:logistic',
    'eval_metric': 'logloss',
    'seed': 42
}

# Train the model
model = train(params, dmatrix)

In this example:

  1. We create synthetic data using Python lists. X is a list of lists representing the feature matrix, and y is a binary target vector. Replace these with your actual data.

  2. We convert the lists to a DMatrix object dmatrix using the DMatrix constructor. It takes the feature matrix X as the data argument and the target vector y as the label argument.

  3. We define the XGBoost parameters in a dictionary params, specifying the objective function, evaluation metric, and random seed. Adjust these based on your specific problem.

  4. We train the XGBoost model by passing the params dictionary and dmatrix to the train() function.

Using a DMatrix instead of Python lists directly offers several advantages:

Before converting your data to a DMatrix, ensure that it is properly preprocessed, such as encoding categorical variables or scaling numerical features if necessary.

By converting your Python lists to a DMatrix, you can leverage XGBoost’s optimized data structure and train your models more effectively.



See Also