zhangbo 1 year ago
parent
commit
5aa9e03218

+ 187 - 0
src/main/scala/com/aliyun/odps/spark/examples/makedata/makedata_12_rosData_v3_noweight.scala

@@ -0,0 +1,187 @@
+package com.aliyun.odps.spark.examples.makedata
+
+import com.alibaba.fastjson.{JSON, JSONObject}
+import com.aliyun.odps.TableSchema
+import com.aliyun.odps.data.Record
+import com.aliyun.odps.spark.examples.myUtils.{MyDateUtils, MyHdfsUtils, ParamUtils}
+import examples.dataloader.OfflineVlogShareLRFeatureExtractorV2
+import examples.extractor.ExtractorUtils
+import org.apache.hadoop.io.compress.GzipCodec
+import org.apache.spark.sql.SparkSession
+
+import java.util
+import scala.collection.JavaConversions._
+import scala.collection.mutable.ArrayBuffer
+
+object makedata_12_rosData_v3_noweight {
+  def main(args: Array[String]) {
+    val spark = SparkSession
+      .builder()
+      .appName(this.getClass.getName)
+      .getOrCreate()
+    val sc = spark.sparkContext
+
+    // 1 读取参数
+    val param = ParamUtils.parseArgs(args)
+    val partitionPrefix = param.getOrElse("partitionPrefix", "dt=")
+    val beginStr = param.getOrElse("beginStr", "20230101")
+    val endStr = param.getOrElse("endStr", "20230101")
+    val readPath = param.getOrElse("readPath", "/dw/recommend/model/10_sample_data_v3/")
+    val savePath = param.getOrElse("savePath", "/dw/recommend/model/12_ros_data_v3_noweight/")
+    val featureVersion =  param.getOrElse("featureVersion", "v2")
+    val ifRepart = param.getOrElse("ifRepart", "10").toInt
+    val labelVersion = param.getOrElse("labelVersion", "v1")
+
+    // 3 循环执行数据生产
+    val dateRange = MyDateUtils.getDateRange(beginStr, endStr)
+    for (date <- dateRange) {
+      val partition = partitionPrefix + date
+      println("开始执行partiton:" + partition)
+      var hdfsPath = readPath + "/" + partition
+
+      //1 样本过滤(分享样本、012345中的、可推荐的video、不同产品)
+      val data1 = sc.textFile(hdfsPath).map(r => {
+        val rList = r.split("\t")
+        val logKeyStr = rList(0)
+        val (mid, videoid, logtimestamp, apptype, pagesource_change, abcode, video_recommend) = ParamUtils.parseLogKey(logKeyStr)
+        val labelStr = rList(1)
+        val feaStr = rList(2)
+        val labelJson = JSON.parseObject(labelStr)
+        val is_share = labelJson.getString("is_share")
+        (logKeyStr, labelJson, feaStr, is_share, pagesource_change, video_recommend, apptype, logtimestamp.toLong)
+      }).filter({
+        case (logKeyStr, labelJson, feaStr, is_share, pagesource_change, video_recommend, apptype, logtimestamp) =>
+          val pages = Set("2")
+          val video_status = Set("-6")
+          val apps = Set("0", "4", "5", "21", "3", "6")
+          "1".equals(is_share) && pages.contains(pagesource_change) && video_status.contains(video_recommend) && apps.contains(apptype)
+      })
+
+      //2 样本采样
+      val data2 = data1.map({
+        case (logKeyStr, labelJson, feaStr, is_share, pagesource_change, video_recommend, apptype, logtimestamp) =>
+          val feaJson = JSON.parseObject(feaStr)
+          val is_return = labelJson.getString("is_return")
+          if ("0".equals(is_return)){
+            ("0", feaJson)
+          }else{
+            ("1", feaJson)
+          }
+      })
+
+      //3 保留一份原始样本的中间数据
+      println("样本比例")
+      data2.map(r=> (r._1, 1)).reduceByKey(_+_).map(r=> r._1 + "\t" + r._2).collect().foreach(println)
+
+      //4 特征绝对值转换 如 0.456变成19
+      val data3 = data2.map({
+        case (label, feaJson) =>
+          Set(
+            "u_1day_ctr", "u_1day_str", "u_1day_rov", "u_1day_ros",
+            "u_3day_ctr", "u_3day_str", "u_3day_rov", "u_3day_ros",
+            "u_7day_ctr", "u_7day_str", "u_7day_rov", "u_7day_ros",
+            "u_3month_ctr", "u_3month_str", "u_3month_rov", "u_3month_ros",
+            // ----------
+            "i_1day_ctr", "i_1day_str", "i_1day_rov", "i_1day_ros",
+            "i_3day_ctr", "i_3day_str", "i_3day_rov", "i_3day_ros",
+            "i_7day_ctr", "i_7day_str", "i_7day_rov", "i_7day_ros",
+            "i_3month_ctr", "i_3month_str", "i_3month_rov", "i_3month_ros",
+            // ----------
+            "i_1day_ctr_rt", "i_1day_str_rt", "i_1day_ros_rt", "i_1day_rov_rt",
+            "i_1h_ctr_rt", "i_1h_str_rt", "i_1h_ros_rt", "i_1h_rov_rt"
+          ).foreach(key =>{
+            if (feaJson.containsKey(key)){
+              val value = ExtractorUtils.ceilLogRate(feaJson.getString(key).toDouble)
+              feaJson.put(key, value.toString)
+            }
+          })
+          Set(
+            "u_1day_exp_cnt", "u_1day_click_cnt", "u_1day_share_cnt", "u_1day_return_cnt",
+            "u_3day_exp_cnt", "u_3day_click_cnt", "u_3day_share_cnt", "u_3day_return_cnt",
+            "u_7day_exp_cnt", "u_7day_click_cnt", "u_7day_share_cnt", "u_7day_return_cnt",
+            "u_3month_exp_cnt", "u_3month_click_cnt", "u_3month_share_cnt", "u_3month_return_cnt",
+            // ----------
+            "total_time", "play_count", "play_count_total",
+            "i_1day_exp_cnt", "i_1day_click_cnt", "i_1day_share_cnt", "i_1day_return_cnt",
+            "i_3day_exp_cnt", "i_3day_click_cnt", "i_3day_share_cnt", "i_3day_return_cnt",
+            "i_7day_exp_cnt", "i_7day_click_cnt", "i_7day_share_cnt", "i_7day_return_cnt",
+            "i_3month_exp_cnt", "i_3month_click_cnt", "i_3month_share_cnt", "i_3month_return_cnt",
+            // ----------
+            "share_uv_list_1day_6_avg", "share_uv_list_1day_6_var", "share_uv_list_1day_diff_6_avg", "share_uv_list_1day_diff_6_var",
+            "return_uv_list_1day_6_avg", "return_uv_list_1day_6_var", "return_uv_list_1day_diff_6_avg", "return_uv_list_1day_diff_6_var",
+            "share_uv_list_1h_6_avg", "share_uv_list_1h_6_var", "share_uv_list_1h_diff_6_avg", "share_uv_list_1h_diff_6_var",
+            "return_uv_list_1h_6_avg", "return_uv_list_1h_6_var", "return_uv_list_1h_diff_6_avg", "return_uv_list_1h_diff_6_var",
+            // ----------
+            "view_pv_list_1day", "view_uv_list_1day", "play_pv_list_1day", "play_uv_list_1day",
+            "share_pv_list_1day", "share_uv_list_1day", "return_uv_list_1day",
+            "p_view_uv_list_1day", "p_view_pv_list_1day", "p_return_uv_list_1day",
+            "share_uv_list_2day", "share_pv_list_2day", "share_uv_list_3day", "share_pv_list_3day",
+            // ----------
+            "view_uv_list_1h", "view_pv_list_1h", "play_uv_list_1h", "play_pv_list_1h",
+            "share_uv_list_1h", "share_pv_list_1h", "return_uv_list_1h", "p_return_uv_list_1h"
+
+          ).foreach(key => {
+            if (feaJson.containsKey(key)) {
+              val value = ExtractorUtils.bucketCnt(feaJson.getString(key).toDouble)
+              feaJson.put(key, value.toString)
+            }
+          })
+          (label, feaJson)
+      })
+      //5 libsvm 转换
+      val data4 = data3.map({
+        case (label, feaJson) =>
+          val feaSet = Set(
+            "ctx_week", "ctx_hour", "ctx_region", "ctx_city",
+            "machineinfo_brand", "machineinfo_model", "machineinfo_platform", "machineinfo_system",
+            "u_1day_exp_cnt", "u_1day_click_cnt", "u_1day_share_cnt", "u_1day_return_cnt",
+            "u_3day_exp_cnt", "u_3day_click_cnt", "u_3day_share_cnt", "u_3day_return_cnt",
+            "u_7day_exp_cnt", "u_7day_click_cnt", "u_7day_share_cnt", "u_7day_return_cnt",
+            "total_time", "play_count_total",
+            "i_1day_exp_cnt", "i_1day_click_cnt", "i_1day_share_cnt", "i_1day_return_cnt",
+            "i_3day_exp_cnt", "i_3day_click_cnt", "i_3day_share_cnt", "i_3day_return_cnt",
+            "i_7day_exp_cnt", "i_7day_click_cnt", "i_7day_share_cnt", "i_7day_return_cnt",
+            "u_1day_ctr", "u_1day_str", "u_1day_rov", "u_1day_ros",
+            "u_3day_ctr", "u_3day_str", "u_3day_rov", "u_3day_ros",
+            "u_7day_ctr", "u_7day_str", "u_7day_rov", "u_7day_ros",
+            "i_1day_ctr", "i_1day_str", "i_1day_rov", "i_1day_ros",
+            "i_3day_ctr", "i_3day_str", "i_3day_rov", "i_3day_ros",
+            "i_7day_ctr", "i_7day_str", "i_7day_rov", "i_7day_ros",
+
+            "i_1day_ctr_rt", "i_1day_str_rt", "i_1day_ros_rt", "i_1day_rov_rt",
+            "i_1h_ctr_rt", "i_1h_str_rt", "i_1h_ros_rt", "i_1h_rov_rt"
+          )
+          val feaMap = new util.HashMap[String, String]()
+          feaSet.foreach(r => {
+            if (feaJson.containsKey(r)) {
+              feaMap.put(r, feaJson.getString(r))
+            }
+          })
+          val bytesFeatureExtractor = new OfflineVlogShareLRFeatureExtractorV2()
+          bytesFeatureExtractor.makeFeature4String(feaMap)
+          val featureMap = bytesFeatureExtractor.featureMap
+          label + "\t" + featureMap.entries().map(r => r.getValue.getIdentifier + ":1").mkString("\t")
+
+      })
+
+      // 7 保存数据到hdfs
+      hdfsPath = savePath + "/" + partition
+      if (hdfsPath.nonEmpty && hdfsPath.startsWith("/dw/recommend/model/")){
+        println("删除路径并开始数据写入:" + hdfsPath)
+        MyHdfsUtils.delete_hdfs_path(hdfsPath)
+        if (ifRepart == 0){
+          data4.saveAsTextFile(hdfsPath, classOf[GzipCodec])
+        }else{
+          data4.repartition(ifRepart).saveAsTextFile(hdfsPath, classOf[GzipCodec])
+        }
+      }else{
+        println("路径不合法,无法写入:" + hdfsPath)
+      }
+    }
+  }
+
+  def func(record: Record, schema: TableSchema): Record = {
+    record
+  }
+
+}

+ 1 - 0
zhangbo/01_train.sh

@@ -14,6 +14,7 @@ $HADOOP fs -text ${train_path}/dt=$day/* | /root/sunmingze/alphaFM/bin/fm_train
 
 
 # nohup sh 01_train.sh 20240222 /dw/recommend/model/12_ros_data_v3 model_ros_tom >p1_train.log 2>&1 &
+# nohup sh 01_train.sh 20240222 /dw/recommend/model/11_str_data_v3 model_str_tom >p1_train.log 2>&1 &
 
 
 # nohup sh 01_train.sh 20231214 /dw/recommend/model/share_ratio_samples_v2/ model_sharev2 >p1.log 2>&1 &

+ 1 - 1
zhangbo/02_train_go.sh

@@ -21,4 +21,4 @@ while [[ "$current_date" != "$end_date" ]]; do
     current_date=$(date -d "$current_date + 1 day" +%Y%m%d)
 done
 
-# nohup sh 02_train_go.sh 20240222 20240225 model_ros_tom /dw/recommend/model/12_ros_data_v3/ >p2.log 2>&1 &
+# nohup sh 02_train_go.sh 20240225 20240226 model_ros_tom /dw/recommend/model/12_ros_data_v3/ >p2.log 2>&1 &

+ 2 - 0
zhangbo/03_predict.sh

@@ -10,6 +10,8 @@ HADOOP="/opt/apps/HADOOP-COMMON/hadoop-common-current/bin/hadoop"
 $HADOOP fs -text ${train_path}/dt=$day/* | /root/sunmingze/alphaFM/bin/fm_predict -m model/$model_name -dim 0 -core 8 -out predict/${output_file}_$day.txt
 cat predict/${output_file}_$day.txt | /root/sunmingze/AUC/AUC
 
+# nohup sh 03_predict.sh 20240226 /dw/recommend/model/12_ros_data_v3/ model_ros_tom_20240225.txt model_ros_tom >p3_pred.log 2>&1 &
+
 # str:
 # nohup sh 03_predict.sh 20240115 /dw/recommend/model/01_str_data/ model_str_big_20240114.txt model_str_big >p1_pred.log 2>&1 &
 # nohup sh 03_predict.sh 20240115 /dw/recommend/model/01_str_data/ model_str_big1_20240114.txt model_str_big1 >p1_pred.log 2>&1 &