zoukankan      html  css  js  c++  java
  • MLlib--保序回归

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/24cb3f38b55e5d7516d8059f9f105eb6.html 


    保序回归

    1.线性回归VS保序回归

       • 线性回归->线性拟合
       • 保序回归->保序的分段线性拟合,保序回归是拟合原始数据最佳的单调函数

    1.1保序回归

     
        保序回归是特殊的线性回归,如果业务上具有单调性,这时候就可以用保序回归,而不是用线性回归。

    1.2保序回归应用场景

        药剂和中毒的预测,剂量和毒性呈非递减函数

    1.3保序回归模型使用

    • 预测规则:
       – 如果预测输入能准确匹配训练特征,那么返回相关预测,如果有多个预测匹配训练特征,那么就返回其中之一。
       – 如果预测输入比所有的训练特征低或者高,那么最低和最高的训练特征各自返回。如果有多个预测比所有的训练特征低或者高,那么都会返回。
       – 如果预测输入介于两个训练特征,那么预测会被视为分段线性函数和从最接近的训练特征中计算得到的插值。

    1.4保序回归code

    IsotonicRegression_new
     1 import org.apache.log4j.{Level, Logger}
     2 import org.apache.spark.rdd.RDD
     3 import org.apache.spark.{SparkConf, SparkContext}
     4 import org.apache.spark.mllib.regression.{IsotonicRegressionModel, IsotonicRegression}
     5 
     6 /**
     7   * Created by hzf
     8   */
     9 object IsotonicRegression_new {
    10     //  F:额外项目pensionRiskdataIsR	rainsample_isotonic_regression_data.txt F:额外项目pensionRiskdataIsRmodel true local
    11     def main(args: Array[String]) {
    12         Logger.getLogger("org.apache.spark").setLevel(Level.ERROR)
    13         if (args.length < 4) {
    14             System.err.println("Usage: LRwithLGD <inputPath> <modelPath> Isotonic <master> [<AppName>]")
    15             System.err.println("eg: hdfs://192.168.57.104:8020/user/000000_0 hdfs://192.168.57.104:8020/user/model true  spark://192.168.57.104:7077  IsotonicRegression")
    16             System.exit(1)
    17         }
    18         val appName = if (args.length > 4) args(4) else "IsotonicRegression"
    19         val conf = new SparkConf().setAppName(appName).setMaster(args(3))
    20         val sc = new SparkContext(conf)
    21         var isotonic = true
    22         isotonic = args(2) match {
    23             case "true" => true
    24             case "false" => false
    25         }
    26         val data = sc.textFile(args(0))
    27         val parsedData: RDD[(Double, Double, Double)] = data.map { line =>
    28             val parts = line.split(',').map(_.toDouble)
    29             (parts(0), parts(1), 1.0)
    30         }
    31 
    32         val splitRdd: Array[RDD[(Double, Double, Double)]] = parsedData.randomSplit(Array(1.0, 9.0))
    33         val testData = splitRdd(0)
    34         val realTrainData: RDD[(Double, Double, Double)] = splitRdd(1)
    35 
    36         val model: IsotonicRegressionModel = new IsotonicRegression().setIsotonic(isotonic).run(realTrainData)
    37         val predictionAndLabel = testData.map { point =>
    38             val predictedLabel = model.predict(point._2)
    39             (predictedLabel, point._1)
    40         }
    41 
    42         val meanSquaredError = predictionAndLabel.map { case p => math.pow((p._1 - p._2), 2) }.mean()
    43         println("meanSquaredError = " + meanSquaredError)
    44         model.boundaries.zip(model.predictions).foreach(println(_))
    45         model.save(sc, args(1))
    46 
    47     }
    48 }
    View Code
    设置运行参数
    1. E:IDEA_ProjectsmlibdataIsR rainsample_isotonic_regression_data.txt E:IDEA_ProjectsmlibdataIsRmodel true local
     
  • 相关阅读:
    CodeForces 706C Hard problem
    CodeForces 706A Beru-taxi
    CodeForces 706B Interesting drink
    CodeForces 706E Working routine
    CodeForces 706D Vasiliy's Multiset
    CodeForces 703B Mishka and trip
    CodeForces 703C Chris and Road
    POJ 1835 宇航员
    HDU 4907 Task schedule
    HDU 4911 Inversion
  • 原文地址:https://www.cnblogs.com/haozhengfei/p/24cb3f38b55e5d7516d8059f9f105eb6.html
Copyright © 2011-2022 走看看