|
|
@@ -0,0 +1,150 @@
|
|
|
+package com.tzld.piaoquan.recommend.model
|
|
|
+
|
|
|
+import ml.dmlc.xgboost4j.scala.spark.XGBoostRegressionModel
|
|
|
+import org.apache.commons.lang.math.NumberUtils
|
|
|
+import org.apache.commons.lang3.StringUtils
|
|
|
+import org.apache.hadoop.io.compress.GzipCodec
|
|
|
+import org.apache.spark.ml.evaluation.RegressionEvaluator
|
|
|
+import org.apache.spark.ml.feature.VectorAssembler
|
|
|
+import org.apache.spark.rdd.RDD
|
|
|
+import org.apache.spark.sql.types.DataTypes
|
|
|
+import org.apache.spark.sql.{DataFrame, Row, SparkSession}
|
|
|
+
|
|
|
+import java.util
|
|
|
+import scala.io.Source
|
|
|
+
|
|
|
+object pred_recsys_61_xgb_nor_hdfsfile_20241209 {
|
|
|
+ def main(args: Array[String]): Unit = {
|
|
|
+ val spark = SparkSession
|
|
|
+ .builder()
|
|
|
+ .appName(this.getClass.getName)
|
|
|
+ .getOrCreate()
|
|
|
+ val sc = spark.sparkContext
|
|
|
+
|
|
|
+ val param = ParamUtils.parseArgs(args)
|
|
|
+ val featureFile = param.getOrElse("featureFile", "20241209_recsys_nor_name.txt")
|
|
|
+ val testPath = param.getOrElse("testPath", "")
|
|
|
+ val labelLogType = param.getOrElse("labelLogType", "0").toInt
|
|
|
+ val labelLogBase = param.getOrElse("labelLogBase", "2").toDouble
|
|
|
+ val savePath = param.getOrElse("savePath", "/dw/recommend/model/61_recsys_nor_predict_data/")
|
|
|
+ val featureFilter = param.getOrElse("featureFilter", "XXXXXX").split(",")
|
|
|
+
|
|
|
+ val repartition = param.getOrElse("repartition", "20").toInt
|
|
|
+ val modelPath = param.getOrElse("modelPath", "/dw/recommend/model/61_recsys_nor_model/model_xgb")
|
|
|
+
|
|
|
+ val loader = getClass.getClassLoader
|
|
|
+ val resourceUrl = loader.getResource(featureFile)
|
|
|
+ val content =
|
|
|
+ if (resourceUrl != null) {
|
|
|
+ val content = Source.fromURL(resourceUrl).getLines().mkString("\n")
|
|
|
+ Source.fromURL(resourceUrl).close()
|
|
|
+ content
|
|
|
+ } else {
|
|
|
+ ""
|
|
|
+ }
|
|
|
+ println(content)
|
|
|
+
|
|
|
+ val features = content.split("\n")
|
|
|
+ .map(r => r.replace(" ", "").replaceAll("\n", ""))
|
|
|
+ .filter(r => r.nonEmpty || !featureFilter.contains(r))
|
|
|
+ println("features.size=" + features.length)
|
|
|
+
|
|
|
+ var fields = Array(
|
|
|
+ DataTypes.createStructField("label", DataTypes.DoubleType, true)
|
|
|
+ ) ++ features.map(f => DataTypes.createStructField(f, DataTypes.DoubleType, true))
|
|
|
+ fields = fields ++ Array(
|
|
|
+ DataTypes.createStructField("logKey", DataTypes.StringType, true),
|
|
|
+ DataTypes.createStructField("scoresMap", DataTypes.StringType, true)
|
|
|
+ )
|
|
|
+
|
|
|
+ val schema = DataTypes.createStructType(fields)
|
|
|
+ val vectorAssembler = new VectorAssembler().setInputCols(features).setOutputCol("features")
|
|
|
+
|
|
|
+ val model = XGBoostRegressionModel.load(modelPath)
|
|
|
+ model.setMissing(0.0f).setFeaturesCol("features")
|
|
|
+
|
|
|
+ val testData = createData(
|
|
|
+ sc.textFile(testPath),
|
|
|
+ features
|
|
|
+ )
|
|
|
+ println("recsys nor:test data size:" + testData.count())
|
|
|
+
|
|
|
+ val testDataSet = spark.createDataFrame(testData, schema)
|
|
|
+ val testDataSetTrans = vectorAssembler.transform(testDataSet).select("features", "label", "logKey", "scoresMap")
|
|
|
+ val predictions = model.transform(testDataSetTrans)
|
|
|
+ val clipPrediction = getClipData(spark, predictions, labelLogType, labelLogBase).persist()
|
|
|
+
|
|
|
+ val saveData = clipPrediction.select("label", "prediction", "clipPrediction", "logKey", "scoresMap").rdd
|
|
|
+ .map(r => {
|
|
|
+ (r.get(0), r.get(1), r.get(2), r.get(3), r.get(4)).productIterator.mkString("\t")
|
|
|
+ })
|
|
|
+ val hdfsPath = savePath
|
|
|
+ if (hdfsPath.nonEmpty && hdfsPath.startsWith("/dw/recommend/model/")) {
|
|
|
+ println("删除路径并开始数据写入:" + hdfsPath)
|
|
|
+ MyHdfsUtils.delete_hdfs_path(hdfsPath)
|
|
|
+ saveData.repartition(repartition).saveAsTextFile(hdfsPath, classOf[GzipCodec])
|
|
|
+ } else {
|
|
|
+ println("路径不合法,无法写入:" + hdfsPath)
|
|
|
+ }
|
|
|
+
|
|
|
+ val rmseEvaluator = new RegressionEvaluator()
|
|
|
+ .setLabelCol("label")
|
|
|
+ .setPredictionCol("clipPrediction")
|
|
|
+ .setMetricName("rmse")
|
|
|
+ val maeEvaluator = new RegressionEvaluator()
|
|
|
+ .setLabelCol("label")
|
|
|
+ .setPredictionCol("clipPrediction")
|
|
|
+ .setMetricName("mae")
|
|
|
+ val rmse = rmseEvaluator.evaluate(clipPrediction.select("label", "clipPrediction"))
|
|
|
+ val mae = maeEvaluator.evaluate(clipPrediction.select("label", "clipPrediction"))
|
|
|
+ val mape = MetricUtils.calMAPE(clipPrediction.select("label", "clipPrediction").rdd)
|
|
|
+ val rmsle = MetricUtils.calRMSLE(clipPrediction.select("label", "clipPrediction").rdd)
|
|
|
+ printf("recsys nor:rmse: %.6f\n", rmse)
|
|
|
+ printf("recsys nor:mae: %.6f\n", mae)
|
|
|
+ printf("recsys nor:mape: %.6f\n", mape)
|
|
|
+ printf("recsys nor:rmsle: %.6f\n", rmsle)
|
|
|
+
|
|
|
+ println("---------------------------------\n")
|
|
|
+ println("---------------------------------\n")
|
|
|
+ }
|
|
|
+
|
|
|
+ def createData(data: RDD[String], features: Array[String]): RDD[Row] = {
|
|
|
+ data.map(r => {
|
|
|
+ val line: Array[String] = StringUtils.split(r, '\t')
|
|
|
+ val logKey = line(0)
|
|
|
+ val label: Double = NumberUtils.toDouble(line(1))
|
|
|
+ val scoresMap = line(2)
|
|
|
+ val map: util.Map[String, Double] = new util.HashMap[String, Double]
|
|
|
+ for (i <- 3 until line.length) {
|
|
|
+ val fv: Array[String] = StringUtils.split(line(i), ':')
|
|
|
+ map.put(fv(0), NumberUtils.toDouble(fv(1), 0.0))
|
|
|
+ }
|
|
|
+
|
|
|
+ val v: Array[Any] = new Array[Any](features.length + 3)
|
|
|
+ v(0) = label
|
|
|
+ for (i <- 0 until features.length) {
|
|
|
+ v(i + 1) = map.getOrDefault(features(i), 0.0d)
|
|
|
+ }
|
|
|
+ v(features.length + 1) = logKey
|
|
|
+ v(features.length + 2) = scoresMap
|
|
|
+ Row(v: _*)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ def getClipData(spark: SparkSession, df: DataFrame, logType: Int, logBase: Double): DataFrame = {
|
|
|
+ import spark.implicits._
|
|
|
+ df.select("label", "prediction", "logKey", "scoresMap").rdd
|
|
|
+ .map(row => {
|
|
|
+ val label = row.getAs[Double]("label")
|
|
|
+ val prediction = MetricUtils.restoreLog(row.getAs[Double]("prediction"), logType, logBase)
|
|
|
+ val logKey = row.getAs[String]("logKey")
|
|
|
+ val scoresMap = row.getAs[String]("scoresMap")
|
|
|
+ if (prediction < 1E-8) {
|
|
|
+ (label, prediction, 0d, logKey, scoresMap)
|
|
|
+ } else {
|
|
|
+ (label, prediction, prediction, logKey, scoresMap)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ).toDF("label", "prediction", "clipPrediction", "logKey", "scoresMap")
|
|
|
+ }
|
|
|
+}
|