You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用单行CSR格式数据创建XGBoost DMatrix出错,求排查方案

Hey there! Let's break down what's going wrong here and fix your single-row DMatrix issue with XGBoost's Java API.

Core Issue: Mixing Up CSR/CSC Format Parameters

The root problem is that you're using the CSC sparse type but passing parameters structured for CSR format. XGBoost's Java API expects different input arrays for each sparse format, and mixing them leads to incorrect matrix parsing—hence the unexpected 0.5 output (that's the default initial probability for binary classification when the model can't interpret the input properly).

Let's Clarify CSR vs CSC for Single-Row Data

For a single row of data, CSR format is far simpler to construct. Here's what each format requires:

  • CSR (Compressed Sparse Row):

    1. rowPtr: An array of length number_of_rows + 1, where each entry marks the start index of a row in the colIndices and values arrays. For 1 row with 22 non-zero features, this would be [0, 22] (starts at index 0, ends at index 22).
    2. colIndices: Array of column indices for every non-zero feature (your existing list of 1, 9, 19, ... works here—note XGBoost accepts 1-based column indices as used in libSVM).
    3. values: Array of feature values corresponding to each entry in colIndices.
  • CSC (Compressed Sparse Column):
    This format is optimized for column-wise operations and requires far more work for a single row (you'd need to build a colPtr array spanning all possible columns, which is unnecessary here).

Fixing Your Code

Here's the corrected code using CSR format for your single libSVM row:

import java.util.Arrays;
import ml.dmlc.xgboost4j.java.DMatrix;

public class SingleRowDMatrixExample {
    public static void main(String[] args) throws Exception {
        // Your single row of libSVM data: label 0, 22 non-zero features all set to 1
        long[] rowPtr = new long[]{0, 22}; // 1 row: starts at 0, ends at 22 (22 elements)
        int[] colIndices = new int[]{1, 9, 19, 21, 24, 34, 36, 39, 42, 53, 56, 65, 69, 77, 86, 88, 92, 95, 102, 106, 117, 122};
        float[] values = new float[22];
        Arrays.fill(values, 1f); // All feature values are 1

        // Correctly create DMatrix with CSR format
        DMatrix dMatrix = new DMatrix(rowPtr, colIndices, values, DMatrix.SparseType.CSR, 1);
        
        // Now use your trained Agaricus model to predict—you should get ~4.941254E-4
    }
}

Why Your Original Code Failed

When you specified DMatrix.SparseType.CSC, XGBoost interpreted your input arrays as:

  1. new long[] {0}: Treated as colPtr (which should be length max_column + 1—way too short here)
  2. Your column indices array: Treated as rowIndices (all values would be invalid for a single row)
  3. Your values array: Mismatched with the incorrect row/column indices

This resulted in a malformed matrix that the model couldn't interpret, so it returned the default binary classification probability of 0.5.

Quick Note

Make sure you're using a trained model on the Agaricus dataset—without a proper model, even the correct DMatrix won't give you the expected ~4.94E-4 result.

内容的提问来源于stack exchange,提问作者mobiusinversion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:04:01