XGBoosting Home | About | Contact | Examples

XGBoost Load CSV File as DMatrix

Loading data from a CSV file is a common task in machine learning workflows.

When working with XGBoost, you can load a CSV file directly into a DMatrix object, which is an optimized data structure used by XGBoost for efficient computation and memory usage.

Here’s an example of how to load a CSV file into a DMatrix and use it to train an XGBoost model:

import numpy as np
import pandas as pd
from xgboost import DMatrix, train

# Generate synthetic data and save as CSV
X = np.random.rand(100, 5)
y = np.random.randint(2, size=100)
data = pd.DataFrame(X, columns=[f'feature_{i}' for i in range(5)])
data['target'] = y
data.to_csv('synthetic_data.csv', index=False)

# Load the CSV file into a DMatrix
dmatrix = DMatrix('synthetic_data.csv?format=csv&label_column=5')

# Set XGBoost parameters
params = {
    'objective': 'binary:logistic',
    'learning_rate': 0.1,
    'random_state': 42

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

# Print feature importances

In this example:

  1. We generate a synthetic dataset using NumPy and save it as a CSV file named ‘synthetic_data.csv’. The dataset has 5 features and a binary target variable.

  2. We load the CSV file directly into a DMatrix object using the DMatrix constructor. The first argument is the path to the CSV file. The abel_column=5 argument specifies the name of the column in the CSV file that contains the target variable.

  3. We set up the XGBoost parameters in a dictionary params, specifying the objective function, learning rate, 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.

  5. Finally, we print the feature importances of the trained model using the get_score() method to verify that the model was trained successfully.

By loading your data from a CSV file directly into a DMatrix, you can take advantage of XGBoost’s optimized data structure without the need for additional data conversion steps.

Note: Make sure your CSV file is properly formatted, with the target variable in a separate column. If your CSV file contains headers, XGBoost will automatically detect and use them. If there are no headers, you can specify the column indices using the feature_names and label_column arguments of the DMatrix constructor.

Remember to preprocess your data as needed before saving it to a CSV file. This might include scaling, encoding categorical variables, or handling missing values.

See Also