【问题标题】:How to get correlation matrix for Scala dataframe如何获取Scala数据框的相关矩阵
【发布时间】:2022-08-19 12:12:32
【问题描述】:

我有带有数字数据的 Scala 数据框:

df2_num.printSchema

root
 |-- ot2_total_sum: decimal(38,18) (nullable = true)
 |-- s42_3: decimal(38,0) (nullable = true)
 |-- s109_5: decimal(38,0) (nullable = true)
 |-- is_individual: decimal(38,0) (nullable = true)
 |-- s118_5: decimal(38,0) (nullable = true)
 |-- s46_3: decimal(38,0) (nullable = true)
 |-- ot1_nds_10: decimal(38,18) (nullable = true)
 |-- s45_3: decimal(38,0) (nullable = true)
 |-- s10_3: decimal(38,0) (nullable = true)
 |-- nb: decimal(38,0) (nullable = true)
 |-- s80_5: decimal(38,0) (nullable = true)
 |-- ot2_nds_10: decimal(38,18) (nullable = true)
 |-- pr: decimal(38,0) (nullable = true)
 |-- IP: integer (nullable = true)
 |-- s70_5: decimal(38,0) (nullable = true)
 |-- ot1_sum_without_nds: decimal(38,18) (nullable = true)
 |-- s109_3: decimal(38,0) (nullable = true)
 |-- s60_3: decimal(38,0) (nullable = true)
 |-- s190_3: decimal(38,0) (nullable = true)
 |-- ot3_total_sum: decimal(38,18) (nullable = true)
 |-- s130_3: decimal(38,0) (nullable = true)
 |-- region: integer (nullable = true)
 |-- s170_3: decimal(38,0) (nullable = true)
 |-- s20_3: decimal(38,0) (nullable = true)
 |-- s90_5: decimal(38,0) (nullable = true)
 |-- ot2_nds_20: decimal(38,18) (nullable = true)
 |-- s70_3: decimal(38,0) (nullable = true)
 |-- ot1_nds_0: decimal(38,18) (nullable = true)
 |-- s200_3: decimal(38,0) (nullable = true)
 |-- ot2_sum_without_nds: decimal(38,18) (nullable = true)
 |-- ot1_nds_20: decimal(38,18) (nullable = true)
 |-- s120_3: decimal(38,0) (nullable = true)
 |-- s150_3: decimal(38,0) (nullable = true)
 |-- s40_3: decimal(38,0) (nullable = true)
 |-- s10_5: decimal(38,0) (nullable = true)
 |-- nalog: decimal(38,0) (nullable = true)
 |-- ot1_total_sum: decimal(38,18) (nullable = true)

我需要获取此数据框所有列的相关矩阵。 我尝试使用 org.apache.spark.mllib.stat.Statistics.corr 。它需要 RDD 数据,所以我已将我的数据帧转换为 RDD

val df2_num_rdd =  df2_num.rdd

然后我尝试使用 Statistics.cor ,并得到错误:

val correlMatrix = Statistics.corr(df2_num_rdd , \"pearson\")

<console>:82: error: overloaded method value corr with alternatives:
  (x: org.apache.spark.api.java.JavaRDD[java.lang.Double],y: org.apache.spark.api.java.JavaRDD[java.lang.Double])scala.Double <and>
  (x: org.apache.spark.rdd.RDD[scala.Double],y: org.apache.spark.rdd.RDD[scala.Double])scala.Double <and>
  (X: org.apache.spark.rdd.RDD[org.apache.spark.mllib.linalg.Vector],method: String)org.apache.spark.mllib.linalg.Matrix
 cannot be applied to (org.apache.spark.rdd.RDD[org.apache.spark.sql.Row], String)
       val correlMatrix = Statistics.corr(df2_num_rdd , \"pearson\")

那么我需要如何处理 Statistics.corr 的数据?

    标签: scala apache-spark apache-spark-sql rdd cross-correlation


    【解决方案1】:

    假设您运行的是相对较新版本的 Spark,我建议您改用 org.apache.spark.ml.stat.Correlation.corr

    首先,您必须组装要为其计算相关性的列,然后您可以将相关性作为数据框获取。从这里,您可以获取第一行并将其转换为适合您需要的任何内容。 这是一个例子:

    import org.apache.spark.ml.feature.VectorAssembler
    import org.apache.spark.ml.stat.Correlation
    
    val assembled: DataFrame = new VectorAssembler()
        .setInputCols(df2_num.columns)
        .setOutputCol("correlations")
        .transform(df2_num)
    val correlations: DataFrame = 
        Correlation.corr(assembled, column = "correlations", method = "pearson")
    

    以下是与此方法相关的指南的一些有用链接:

    【讨论】:

    • 谢谢!正在嗳气。但我不能使用结果。如何从数据框“相关性”中获取数字?我试过了:correlations.take(1)(0)(0) 并得到了任何对象:Any = 1.0 -8.324911678513317E-5 ...(总共 37 个)-8.324911678513317E-5 1.0 ... 我可以用任何对象做什么目的 ?如何从中获取数字?
    • 好吧,您可以使用密集矩阵,例如,使用val matrix = correlations.first.getAs[DenseMatrix],如果您愿意,甚至可以使用数组数组。如果这有帮助,请接受答案:)
    【解决方案2】:

    .getAs[DenseMatrix] 中的相关性.first.getAs[DenseMatrix] 抛出错误。

    @H.Leger - 你如何将最终结果转换为这种格式的适当矩阵

    Column c1 c2 c3
    c1 1 0.97 0.92
    c2 0.97 1 0.94
    c3 0.92 0.94 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2022-09-27
      • 2019-10-20
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多