forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
69 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 27 additions & 21 deletions
48
...oost4j-spark/src/test/scala/ml/dmlc/xgboost4j/scala/spark/NewXGBoostClassifierSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,57 @@ | ||
package ml.dmlc.xgboost4j.scala.spark | ||
|
||
import org.apache.spark.ml.feature.VectorAssembler | ||
import org.apache.spark.sql.functions.{array, col} | ||
import org.apache.spark.sql.types.{DoubleType, StructField, StructType} | ||
import org.apache.spark.sql.functions.{array, col, lit, rand} | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class NewXGBoostClassifierSuite extends AnyFunSuite with PerTest with TmpFolderPerSuite { | ||
|
||
test("test NewXGBoostClassifierSuite") { | ||
// Define the schema for the fake data | ||
val features = Array("feature1", "feature2", "feature3", "feature4") | ||
|
||
val spark = ss | ||
import spark.implicits._ | ||
val df = Seq( | ||
(1.0, 0.0, 0.0, 0.0, 0.0, 30), | ||
(2.0, 3.0, 4.0, 4.0, 0.0, 31), | ||
(3.0, 4.0, 5.0, 5.0, 1.0, 32), | ||
(4.0, 5.0, 6.0, 6.0, 1.0, 33), | ||
).toDF("feature1", "feature2", "feature3", "feature4", "label", "base_margin") | ||
// val features = Array("feature1", "feature2", "feature3", "feature4") | ||
|
||
// val df = Seq( | ||
// (1.0, 0.0, 0.0, 0.0, 0.0, 30), | ||
// (2.0, 3.0, 4.0, 4.0, 0.0, 31), | ||
// (3.0, 4.0, 5.0, 5.0, 1.0, 32), | ||
// (4.0, 5.0, 6.0, 6.0, 1.0, 33), | ||
// ).toDF("feature1", "feature2", "feature3", "feature4", "label", "base_margin") | ||
|
||
var df = spark.read.parquet("/home/bobwang/data/iris/parquet") | ||
|
||
// Select the features and label columns | ||
val labelCol = "label" | ||
val labelCol = "class" | ||
|
||
val features = df.schema.names.filter(_ != labelCol) | ||
|
||
df = df.withColumn("base_margin", lit(20)) | ||
.withColumn("weight", rand(1)) | ||
|
||
|
||
// Assemble the feature columns into a single vector column | ||
val assembler = new VectorAssembler() | ||
.setInputCols(features) | ||
.setOutputCol("features") | ||
val dataset = assembler.transform(df) | ||
|
||
val arrayInput = df.select(array(features.map(col(_)): _*).as("features"), | ||
col("label"), col("base_margin")) | ||
// val arrayInput = df.select(array(features.map(col(_)): _*).as("features"), | ||
// col("label"), col("base_margin")) | ||
|
||
val est = new NewXGBoostClassifier() | ||
.setNumWorkers(1) | ||
// .setWeightCol("weight") | ||
// .setBaseMarginCol("base_margin") | ||
.setLabelCol(labelCol) | ||
.setBaseMarginCol("base_margin") | ||
.setRawPredictionCol("raw") | ||
.setProbabilityCol("") | ||
.setProbabilityCol("prob") | ||
.setContribPredictionCol("contrb") | ||
.setLeafPredictionCol("leaf") | ||
// val est = new XGBoostClassifier().setLabelCol(labelCol) | ||
|
||
// est.fit(arrayInput) | ||
// est.fit(arrayInput) | ||
val model = est.fit(dataset) | ||
// model.setProbabilityCol("") | ||
model.setLeafPredictionCol("leaf") | ||
model.setContribPredictionCol("conb") | ||
model.transform(dataset).show() | ||
model.transform(dataset).show(150) | ||
} | ||
|
||
} |