From 9f63d6fead67348552854c38822a01c393e318a3 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Fri, 17 Sep 2021 04:35:04 +0800 Subject: [PATCH] [jvm-packages] Deprecate constructors with implicit missing value. (#7225) --- .../src/main/java/ml/dmlc/xgboost4j/java/DMatrix.java | 4 ++++ .../src/main/scala/ml/dmlc/xgboost4j/scala/DMatrix.scala | 1 + .../src/test/java/ml/dmlc/xgboost4j/java/DMatrixTest.java | 8 ++++---- .../test/scala/ml/dmlc/xgboost4j/scala/DMatrixSuite.scala | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/DMatrix.java b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/DMatrix.java index 47a280793c69..18d19eaef74f 100644 --- a/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/DMatrix.java +++ b/jvm-packages/xgboost4j/src/main/java/ml/dmlc/xgboost4j/java/DMatrix.java @@ -124,7 +124,11 @@ public DMatrix(long[] headers, int[] indices, float[] data, DMatrix.SparseType s * @param nrow number of rows * @param ncol number of columns * @throws XGBoostError native error + * + * @deprecated Please specify the missing value explicitly using + * {@link DMatrix(float[], int, int, float)} */ + @Deprecated public DMatrix(float[] data, int nrow, int ncol) throws XGBoostError { long[] out = new long[1]; XGBoostJNI.checkCall(XGBoostJNI.XGDMatrixCreateFromMat(data, nrow, ncol, 0.0f, out)); diff --git a/jvm-packages/xgboost4j/src/main/scala/ml/dmlc/xgboost4j/scala/DMatrix.scala b/jvm-packages/xgboost4j/src/main/scala/ml/dmlc/xgboost4j/scala/DMatrix.scala index 629a39dbf7ec..3e8c597d1ec0 100644 --- a/jvm-packages/xgboost4j/src/main/scala/ml/dmlc/xgboost4j/scala/DMatrix.scala +++ b/jvm-packages/xgboost4j/src/main/scala/ml/dmlc/xgboost4j/scala/DMatrix.scala @@ -79,6 +79,7 @@ class DMatrix private[scala](private[scala] val jDMatrix: JDMatrix) { * @param nrow number of rows * @param ncol number of columns */ + @deprecated("Please specify the missing value explicitly", "XGBoost 1.5") @throws(classOf[XGBoostError]) def this(data: Array[Float], nrow: Int, ncol: Int) { this(new JDMatrix(data, nrow, ncol)) diff --git a/jvm-packages/xgboost4j/src/test/java/ml/dmlc/xgboost4j/java/DMatrixTest.java b/jvm-packages/xgboost4j/src/test/java/ml/dmlc/xgboost4j/java/DMatrixTest.java index 3f6435b9e9b1..721b9a25f038 100644 --- a/jvm-packages/xgboost4j/src/test/java/ml/dmlc/xgboost4j/java/DMatrixTest.java +++ b/jvm-packages/xgboost4j/src/test/java/ml/dmlc/xgboost4j/java/DMatrixTest.java @@ -212,7 +212,7 @@ public void testCreateFromDenseMatrix() throws XGBoostError { label0[i] = random.nextFloat(); } - DMatrix dmat0 = new DMatrix(data0, nrow, ncol); + DMatrix dmat0 = new DMatrix(data0, nrow, ncol, Float.NaN); dmat0.setLabel(label0); //check @@ -281,7 +281,7 @@ public void testCreateFromDenseMatrixRef() throws XGBoostError { label0[i] = random.nextFloat(); } - dmat0 = new DMatrix(data0); + dmat0 = new DMatrix(data0, Float.NaN); dmat0.setLabel(label0); //check @@ -318,7 +318,7 @@ public void testTrainWithDenseMatrixRef() throws XGBoostError { for (int j = 0; j < data0.ncol; j++) data0.set(i, j, data[i][j]); - trainMat = new DMatrix(data0); + trainMat = new DMatrix(data0, Float.NaN); trainMat.setLabel(new float[]{1f, 2f, 3f}); HashMap params = new HashMap<>(); @@ -338,7 +338,7 @@ public void testTrainWithDenseMatrixRef() throws XGBoostError { // (3,1) -> 2 // (2,3) -> 3 for (int i = 0; i < 3; i++) { - float[][] preds = booster.predict(new DMatrix(data[i], 1, 2)); + float[][] preds = booster.predict(new DMatrix(data[i], 1, 2, Float.NaN)); assertEquals(1, preds.length); assertArrayEquals(new float[]{(float) (i + 1)}, preds[0], 1e-2f); } diff --git a/jvm-packages/xgboost4j/src/test/scala/ml/dmlc/xgboost4j/scala/DMatrixSuite.scala b/jvm-packages/xgboost4j/src/test/scala/ml/dmlc/xgboost4j/scala/DMatrixSuite.scala index 87ff8e006a98..50bc1a548dd1 100644 --- a/jvm-packages/xgboost4j/src/test/scala/ml/dmlc/xgboost4j/scala/DMatrixSuite.scala +++ b/jvm-packages/xgboost4j/src/test/scala/ml/dmlc/xgboost4j/scala/DMatrixSuite.scala @@ -130,7 +130,7 @@ class DMatrixSuite extends FunSuite { for (i <- label0.indices) { label0(i) = Random.nextFloat() } - val dmat0 = new DMatrix(data0, nrow, ncol) + val dmat0 = new DMatrix(data0, nrow, ncol, Float.NaN) dmat0.setLabel(label0) // check assert(dmat0.rowNum === 10)