XGBoost
multidimensional data
machine learning
data preprocessing
model training

How to pass in multidimensional data to xgboost model

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

XGBoost expects tabular input where each sample is one row and each feature is one column. Many errors happen when users pass tensors or images with more than two dimensions directly into training APIs. The fix is to reshape or engineer features so the final matrix has shape samples by features.

Core Sections

Why XGBoost rejects higher-rank arrays

XGBoost tree boosters operate on feature vectors, not native 3D or 4D tensors. If you pass shape like N by H by W or N by T by F, XGBoost cannot infer a valid feature matrix.

python
1import numpy as np
2
3X = np.random.rand(100, 32, 32)  # 3D
4print(X.shape)  # (100, 32, 32)

To train, transform this to 2D.

Flattening multidimensional inputs

For image-like or sequence-like data, the quickest baseline is flattening.

python
X_flat = X.reshape(X.shape[0], -1)
print(X_flat.shape)  # (100, 1024)

Then fit XGBoost:

python
1import xgboost as xgb
2
3y = np.random.randint(0, 2, size=100)
4model = xgb.XGBClassifier(n_estimators=200, max_depth=6, random_state=42)
5model.fit(X_flat, y)

This runs, but flattening may lose spatial or temporal structure.

Feature engineering alternative

Instead of flattening everything, compute summary features that preserve key signal and reduce dimensionality.

python
1# example: mean and std over axes
2means = X.mean(axis=(1, 2))
3stds = X.std(axis=(1, 2))
4X_feat = np.column_stack([means, stds])
5print(X_feat.shape)  # (100, 2)

This approach is often stronger when dataset is small.

Using DMatrix with explicit feature names

DMatrix provides control for advanced workflows.

python
dtrain = xgb.DMatrix(X_flat, label=y, feature_names=[f"f{i}" for i in range(X_flat.shape[1])])
params = {"objective": "binary:logistic", "eval_metric": "logloss"}
bst = xgb.train(params, dtrain, num_boost_round=100)

Named features improve debugging and model interpretation.

Train and inference shape consistency

Whatever transformation you choose for training must be exactly repeated at inference. Keep it in one function and test shape contracts.

python
def preprocess(raw: np.ndarray) -> np.ndarray:
    return raw.reshape(raw.shape[0], -1)

Validation and production readiness

Validate input rank and shape before model calls. Reject bad payloads early with clear error messages so pipelines fail fast instead of producing misleading predictions.

Track preprocessing version with model artifacts. If flattening logic changes, old model checkpoints may become incompatible with new features. Include integration tests that compare train preprocessing output shape and inference preprocessing output shape.

For large arrays, monitor memory pressure during reshape and batching. Converting very large tensors to contiguous arrays can spike memory and trigger OOM failures. Use chunked inference when needed.

End-to-end preprocessing pipeline

A practical training pipeline usually includes train and validation splits, one preprocessing function, and shape assertions. This avoids accidental drift where training uses one transform and inference uses another.

python
1import numpy as np
2from sklearn.model_selection import train_test_split
3from sklearn.metrics import roc_auc_score
4import xgboost as xgb
5
6
7def preprocess(raw: np.ndarray) -> np.ndarray:
8    if raw.ndim != 3:
9        raise ValueError("expected rank-3 array")
10    return raw.reshape(raw.shape[0], -1)
11
12X = np.random.rand(500, 16, 16)
13y = np.random.randint(0, 2, size=500)
14
15X2 = preprocess(X)
16X_train, X_val, y_train, y_val = train_test_split(
17    X2, y, test_size=0.2, random_state=7, stratify=y
18)
19
20model = xgb.XGBClassifier(
21    n_estimators=300,
22    max_depth=5,
23    learning_rate=0.05,
24    subsample=0.9,
25    colsample_bytree=0.9,
26    eval_metric="logloss",
27)
28model.fit(X_train, y_train)
29proba = model.predict_proba(X_val)[:, 1]
30print("AUC", roc_auc_score(y_val, proba))

When flattening is not enough

Flattening can work as a baseline, but it discards locality. If your data has strong spatial structure, extract engineered features first. For image-like arrays, simple statistics by region can retain more signal than a raw full flatten on small datasets.

A common compromise is two-stage modeling: use a neural encoder or domain feature extractor to build dense vectors, then fit XGBoost on those vectors. This keeps XGBoost explainability and tabular strengths while preserving richer structure than pure flattening. Regardless of strategy, lock feature-generation code to a versioned artifact and test output shape and feature order in CI.

Common Pitfalls

  • Passing 3D or 4D arrays directly to XGBoost fit methods.
  • Training with one reshape rule and serving with a different rule.
  • Flattening huge tensors without memory planning.
  • Forgetting to persist preprocessing alongside model weights.
  • Ignoring feature leakage when engineering summary statistics.

Summary

  • XGBoost needs 2D input with one row per sample.
  • Reshape multidimensional data using a deterministic preprocessing step.
  • Consider engineered summary features when flattening is too noisy.
  • Keep training and inference preprocessing identical.
  • Add shape validation and memory checks in production pipelines.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.