|
| 1 | +/* |
| 2 | + Copyright (c) 2014 by Contributors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package ml.dmlc.xgboost4j.scala.spark |
| 18 | + |
| 19 | +import ml.dmlc.xgboost4j.java.XGBoostError |
| 20 | +import org.apache.spark.Partitioner |
| 21 | +import org.apache.spark.ml.feature.VectorAssembler |
| 22 | +import org.apache.spark.sql.SparkSession |
| 23 | +import org.scalatest.FunSuite |
| 24 | + |
| 25 | +import scala.util.Random |
| 26 | + |
| 27 | +class FeatureSizeValidatingSuite extends FunSuite with PerTest { |
| 28 | + |
| 29 | + test("transform throwing exception if feature size of dataset is different with model's") { |
| 30 | + val modelPath = getClass.getResource("/model/0.82/model").getPath |
| 31 | + val model = XGBoostClassificationModel.read.load(modelPath) |
| 32 | + val r = new Random(0) |
| 33 | + // 0.82/model was trained with 251 features. and transform will throw exception |
| 34 | + // if feature size of data is not equal to 251 |
| 35 | + val df = ss.createDataFrame(Seq.fill(100)(r.nextInt(2)).map(i => (i, i))). |
| 36 | + toDF("feature", "label") |
| 37 | + val assembler = new VectorAssembler() |
| 38 | + .setInputCols(df.columns.filter(!_.contains("label"))) |
| 39 | + .setOutputCol("features") |
| 40 | + val thrown = intercept[Exception] { |
| 41 | + model.transform(assembler.transform(df)).show() |
| 42 | + } |
| 43 | + assert(thrown.getMessage.contains( |
| 44 | + "Number of columns does not match number of features in booster")) |
| 45 | + } |
| 46 | + |
| 47 | + test("train throwing exception if feature size of dataset is different on distributed train") { |
| 48 | + val paramMap = Map("eta" -> "1", "max_depth" -> "6", "silent" -> "1", |
| 49 | + "objective" -> "binary:logistic", |
| 50 | + "num_round" -> 5, "num_workers" -> 2, "use_external_memory" -> true, "missing" -> 0) |
| 51 | + import DataUtils._ |
| 52 | + val sparkSession = SparkSession.builder().getOrCreate() |
| 53 | + import sparkSession.implicits._ |
| 54 | + val repartitioned = sc.parallelize(Synthetic.trainWithDiffFeatureSize, 2) |
| 55 | + .map(lp => (lp.label, lp)).partitionBy( |
| 56 | + new Partitioner { |
| 57 | + override def numPartitions: Int = 2 |
| 58 | + |
| 59 | + override def getPartition(key: Any): Int = key.asInstanceOf[Float].toInt |
| 60 | + } |
| 61 | + ).map(_._2).zipWithIndex().map { |
| 62 | + case (lp, id) => |
| 63 | + (id, lp.label, lp.features) |
| 64 | + }.toDF("id", "label", "features") |
| 65 | + val xgb = new XGBoostClassifier(paramMap) |
| 66 | + intercept[XGBoostError] { |
| 67 | + xgb.fit(repartitioned) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments