spark熵权法验证

指标权重计算流程

参考:https://www.zhihu.com/question/357680646/answer/1748591262

1、归一化

2、指标占比

3、计算熵

4、计算差异系数

5、计算权重

6、验证:权重和为1

样例:WeightScoreTest.scala


case class Room(name:String,x1:Double,x2:Double,x3:Double,x4:Double,x5:Double,x6:Double,x7:Double,x8:Double){

}
object Room{
 // def apply(name: String, x1: Double, x2: Double, x3: Double, x4: Double, x5: Double, x6: Double, x7: Double, x8: Double): Room = new Room(name, x1, x2, x3, x4, x5, x6, x7, x8)
  def apply(row:String)={
    val r = row.split(" ")
    new Room(r(0),r(1).toDouble,r(2).toDouble,r(3).toDouble,r(4).toDouble,r(5).toDouble,r(6).toDouble,r(7).toDouble,r(8).toDouble)
  }
}
object WeightScoreTest   {


  /** l1个科室9项整体护理评价指标得分表 */
  val samples =
    """
      |A    100 90  100 84  90  100 100 100 100
      |B    100 100 78.6    100 90  100 100 100 100
      |C    75  100 85.7    100 90  100 100 100 100
      |D    100 100 78.6    100 90  100 94.4    100 100
      |E    100 90  100 100 100 90  100 100 80
      |F    100 100 100 100 90  100 100 85.7    100
      |G    100 100 78.6    100 90  100 55.6    100 100
      |H    87.5    100 85.7    100 100 100 100 100 100
      |I    100 100 92.9    100 80  100 100 100 100
      |J    100 90  100 100 100 100 100 100 100
      |K    100 100 92.9    100 90  100 100 100 100
      |
    """.stripMargin
   
   def start(): Unit = {
     val sparkConf = new SparkConf().setAppName("WeightScoreTest")
     sparkConf.setMaster("local[*]")
     val sparkContext = new SparkContext(sparkConf)

     val sparkSessionBuilder = SparkSession.builder()
       .enableHiveSupport()
       .config(sparkConf)
       .appName(sparkContext.appName)
     val spark = sparkSessionBuilder.getOrCreate()
     spark.udf.register("sumofsquares", new Sumofsquares())
     val rooms = samples.split("\r\n").filter(StringUtils.isNoneBlank(_)).map(r=>{
      Room(r)
     }).toList.asJava
    val df = spark.createDataFrame(rooms)
    df.show(20)
     //TODO 对每一列指标进行归一化,
     val summary = df.summary("count", "mean", "max", "min", "stddev").cache()
     var features = summary.columns.filterNot(r => r == "summary" || r == "name" )
     val feaMaxMap = summary.filter("summary = 'max'").collect().head.getValuesMap[Double](features)
     val feaMinMap = summary.filter("summary = 'min'").collect().head.getValuesMap[Double](features)
     summary.show(20)
     val feaMeanMap = summary.filter("summary = 'mean'").collect().head.getValuesMap[String](features)
     val feaStdMap = summary.filter("summary = 'stddev'").collect().head.getValuesMap[String](features)

    val df2 = df.selectExpr(features.map{ f =>
       val maxVal = feaMaxMap.getOrElse(f, 1)
       val minVal = feaMinMap.getOrElse(f, 0)
       //s"($f - $minVal)/($maxVal - $minVal + 1e-6) as ${f}"  //
      if("name".equals(f)){
        s"${f}"
      }else {
        s"($f - $minVal)/($maxVal - $minVal ) as ${f}"
      }
    }:_*)
     df2.show(20)



     // 然后计算权重
     val diverse = features.map(f => calWeight(df2, f))
     val s = diverse.sum + 1e-6
     val weights = diverse.map(_ / s)
     println(s"weights,list:${JSON.toJSONString(weights,false)}")
     println(s"weights,sum:${weights.sum}")

     println("features:"+JSON.toJSONString(features,false))
     println("summary = 'max':"+JSON.toJSONString(feaMaxMap.asJava,false))
     println("summary = 'min':"+JSON.toJSONString(feaMinMap.asJava,false))
     println("summary = 'mean':"+JSON.toJSONString(feaMeanMap.asJava,false))
     println("summary = 'stddev':"+JSON.toJSONString(feaStdMap.asJava,false))
  }

  def calWeight(dataDF:DataFrame, field: String):Double={
    val scoreDf =  dataDF.rdd.map{_.getAs[Any](field).toString.toDouble}//.select(field)
    val sumIter=scoreDf.sum() + 1e-6
    val scalar = -1.0/math.log(scoreDf.count())
    val Ej = scoreDf.map{ v=>val l1value = math.abs(v)/sumIter
      l1value * math.log(l1value + 1e-6)
    }.sum * scalar
    println(s"sum:${Ej}")
    if(1 - Ej < 0) println("差异系数为负数")
    1 - Ej // 差异系数*/
  }

  def main(args: Array[String]): Unit = {

    start()
  }

计算结果

 /**数据集探索*/   
 val summary=
      """
        |+-------+----+----------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+-----------------+
        ||summary|name|              x1|               x2|               x3|               x4|               x5|                x6|                x7|               x8|
        |+-------+----+----------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+-----------------+
        ||  count|  11|              11|               11|               11|               11|               11|                11|                11|               11|
        ||   mean|null|96.5909090909091|97.27272727272727|90.27272727272727|98.54545454545455|91.81818181818181|  99.0909090909091| 95.45454545454545|             98.7|
        ||    max|   K|           100.0|            100.0|            100.0|            100.0|            100.0|             100.0|             100.0|            100.0|
        ||    min|   A|            75.0|             90.0|             78.6|             84.0|             80.0|              90.0|              55.6|             85.7|
        || stddev|null|8.08337238353579|4.670993664969138|9.180750613004465|4.824181513244218|6.030226891555272|3.0151134457776365|13.324591073377345|4.311612227462018|
        |+-------+----+----------------+-----------------+-----------------+-----------------+-----------------+------------------+------------------+-----------------+
        |
      """.stripMargin
     /**指标标准化矩阵*/   
     val standString =
       """
         |+---+---+-------------------+---+---+---+-----------------+------------------+
         || x1| x2|                 x3| x4| x5| x6|               x7|                x8|
         |+---+---+-------------------+---+---+---+-----------------+------------------+
         ||1.0|0.0| 1.0000000000000002|0.0|0.5|1.0|              1.0|0.9999999999999998|
         ||1.0|1.0|                0.0|1.0|0.5|1.0|              1.0|0.9999999999999998|
         ||0.0|1.0|0.33177570093457986|1.0|0.5|1.0|              1.0|0.9999999999999998|
         ||1.0|1.0|                0.0|1.0|0.5|1.0|0.873873873873874|0.9999999999999998|
         ||1.0|0.0| 1.0000000000000002|1.0|1.0|0.0|              1.0|0.9999999999999998|
         ||1.0|1.0| 1.0000000000000002|1.0|0.5|1.0|              1.0|               0.0|
         ||1.0|1.0|                0.0|1.0|0.5|1.0|              0.0|0.9999999999999998|
         ||0.5|1.0|0.33177570093457986|1.0|1.0|1.0|              1.0|0.9999999999999998|
         ||1.0|1.0| 0.6682242990654211|1.0|0.0|1.0|              1.0|0.9999999999999998|
         ||1.0|0.0| 1.0000000000000002|1.0|1.0|1.0|              1.0|0.9999999999999998|
         ||1.0|1.0| 0.6682242990654211|1.0|0.5|1.0|              1.0|0.9999999999999998|
         |+---+---+-------------------+---+---+---+-----------------+------------------+
       """.stripMargin

      val weightString =
        """
          |weights,list:[0.08110818342879658,0.23453511631130167,0.2904122876143106,0.07019980217824295,0.11258311168727239,0.07019980217824295,0.07076012846443831,0.07019980217824295]
          |weights,sum:0.9999982340408485
          |features:["x1","x2","x3","x4","x5","x6","x7","x8"]
          |summary = 'max':{"x8":"100.0","x3":"100.0","x7":"100.0","x2":"100.0","x5":"100.0","x6":"100.0","x1":"100.0","x4":"100.0"}
          |summary = 'min':{"x8":"85.7","x3":"78.6","x7":"55.6","x2":"90.0","x5":"80.0","x6":"90.0","x1":"75.0","x4":"84.0"}
          |summary = 'mean':{"x8":"98.7","x3":"90.27272727272727","x7":"95.45454545454545","x2":"97.27272727272727","x5":"91.81818181818181","x6":"99.0909090909091","x1":"96.5909090909091","x4":"98.54545454545455"}
          |summary = 'stddev':{"x8":"4.311612227462018","x3":"9.180750613004465","x7":"13.324591073377345","x2":"4.670993664969138","x5":"6.030226891555272","x6":"3.0151134457776365","x1":"8.08337238353579","x4":"4.824181513244218"}
        """.stripMargin

打分原理

根据分布情况求累积概率
累计概率(cumulativeprobability)即所有可能取值的概率之和。
正向指标 发生概率越大,分数越高
反向指标 发生概率越小,分数越高

```
import org.apache.commons.math3.distribution.{ExponentialDistribution, NormalDistribution}
NormalDist => new NormalDistribution(平均值, 标准差)
ExponDist => new ExponentialDistribution(平均值)
{if(-1 == effect) (1 - dist.cumulativeProbability(indexVal)) * 100
else 100 * dist.cumulativeProbability(indexVal)}.formatted("%.2f").toFloat
```

打分说明

反向指标打分:
求疲劳驾驶发生次数小于等于10次/100km的概率P(X <= 10) ,该指标分数 (1-p)*100
正向指标打分:
经济速度占比,经济负载占比

总分以及二级指标处理

总分:各个指标的分数*指标权重 相加
二级指标:分数 * ( 权重占比 即 权重/二级权重之和 ) 相加

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342

推荐阅读更多精彩内容