<jupyter_start><jupyter_text>Taxi CrossValidation with GPU accelerating on XGBoostIn this notebook, we will show you how to levarage GPU to accelerate taxi CrossValidation on XGBoost to find out the best model given a group parameters. Import classesFirst we need load some common classes that both GPU version and CPU version will use:<jupyter_code>import ml.dmlc.xgboost4j.scala.spark.{XGBoostRegressionModel, XGBoostRegressor}
import org.apache.spark.ml.evaluation.{RegressionEvaluator}
import org.apache.spark.ml.tuning.ParamGridBuilder
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{FloatType, IntegerType, StructField, StructType}<jupyter_output><empty_output><jupyter_text>what is new to xgboost-spark users is rapids.GpuDataReader and **rapids.CrossValidator**<jupyter_code>import ml.dmlc.xgboost4j.scala.spark.rapids.CrossValidator<jupyter_output><empty_output><jupyter_text>Set dataset path<jupyter_code>// You need to update them to your real paths!
val dataRoot = sys.env.getOrElse("DATA_ROOT", "/data")
val trainParquetPath=dataRoot + "/taxi/parquet/train"
val evalParquetPath=dataRoot + "/taxi/parquet/eval"<jupyter_output><empty_output><jupyter_text>Set the schema of the dataset<jupyter_code>val labelColName = "fare_amount"
val schema =
    StructType(Array(
      StructField("vendor_id", FloatType),
      StructField("passenger_count", FloatType),
      StructField("trip_distance", FloatType),
      StructField("pickup_longitude", FloatType),
      StructField("pickup_latitude", FloatType),
      StructField("rate_code", FloatType),
      StructField("store_and_fwd", FloatType),
      StructField("dropoff_longitude", FloatType),
      StructField("dropoff_latitude", FloatType),
      StructField(labelColName, FloatType),
      StructField("hour", FloatType),
      StructField("year", IntegerType),
      StructField("month", IntegerType),
      StructField("day", FloatType),
      StructField("day_of_week", FloatType),
      StructField("is_weekend", FloatType)
    ))<jupyter_output><empty_output><jupyter_text>Create a new spark session and load datawe must create a new spark session to continue all spark operations. It will also be used to initilize the `GpuDataReader` which is a data reader powered by GPU.NOTE: in this notebook, we have uploaded dependency jars when installing toree kernel. If we don't upload them at installation time, we can also upload in notebook by [%AddJar magic](https://toree.incubator.apache.org/docs/current/user/faq/). However, there's one restriction for `%AddJar`: the jar uploaded can only be available when `AddJar` is called after a new spark session is created. We must use it as below:```scalaimport org.apache.spark.sql.SparkSessionval spark = SparkSession.builder().appName("Taxi-GPU").getOrCreate%AddJar file:/data/libs/cudf-21.10.0-cuda11.jar%AddJar file:/data/libs/xgboost4j_3.0-1.4.2-0.2.0.jar%AddJar file:/data/libs/xgboost4j-spark_3.0-1.4.2-0.2.0.jar%AddJar file:/data/libs/rapids-4-spark-21.10.0.jar// ...```<jupyter_code>val spark = SparkSession.builder().appName("taxi-gpu-cv").getOrCreate()
val trainDs = spark.read.parquet(trainParquetPath)<jupyter_output><empty_output><jupyter_text>Find out features to train<jupyter_code>val featureNames = schema.filter(_.name != labelColName).map(_.name)
val regressorParam = Map(
    "learning_rate" -> 0.05,
    "max_depth" -> 8,
    "subsample" -> 0.8,
    "gamma" -> 1,
    "num_round" -> 100,
    "tree_method" -> "gpu_hist")<jupyter_output><empty_output><jupyter_text>Construct CrossValidator<jupyter_code>val regressor = new XGBoostRegressor(regressorParam)
    .setLabelCol(labelColName)
    .setFeaturesCols(featureNames)
val paramGrid = new ParamGridBuilder()
    .addGrid(regressor.maxDepth, Array(3, 10))
    .addGrid(regressor.eta, Array(0.2, 0.6))
    .build()
val evaluator = new RegressionEvaluator().setLabelCol(labelColName)
val cv = new CrossValidator()
    .setEstimator(regressor)
    .setEvaluator(evaluator)
    .setEstimatorParamMaps(paramGrid)
    .setNumFolds(3)<jupyter_output><empty_output><jupyter_text>train with CrossValidator<jupyter_code>val model = cv.fit(trainDs).bestModel.asInstanceOf[XGBoostRegressionModel]<jupyter_output>Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERVER=0, DMLC_TRACKER_URI=10.19.183.93, DMLC_TRACKER_PORT=9091, DMLC_NUM_WORKER=1}
Tracker started, with env={DMLC_NUM_SERV[...]<jupyter_text>tranform with best model trained by CrossValidator<jupyter_code>val transformDs = spark.read.parquet(evalParquetPath)
val df = model.transform(transformDs).cache()
df.select("fare_amount", "prediction").show(5)
val evaluator = new RegressionEvaluator().setLabelCol(labelColName)
val rmse = evaluator.evaluate(df)
spark.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11536397782149883, 'fraction_numerical': 0.030763727419066356, 'mean_word_length': 6.416445623342175, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 42, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '117571', 'license': 'Apache-2.0', 'n_tokens_mistral': 2115, 'n_tokens_neox': 1988, 'n_words': 379, 'path': 'examples/taxi/notebooks/scala/taxi_gpu_crossvalidation.ipynb', 'repo_name': 'wbo4958/spark-rapids-examples', 'size': '5591'}