How to convert a mllib matrix to a spark dataframe?

前端 未结 1 1026
挽巷
挽巷 2021-01-15 04:31

I want to pretty print the result of a correlation in a zeppelin notebook:

val Row(coeff: Matrix) = Correlation.corr(data, \"features\").head
1条回答
  •  迷失自我
    2021-01-15 05:26

    Using the toString method should be the easiest and fastest way if you simply want to print the matrix. You can change the output by inputting the maximum number of lines to print as well as max line width. You can change the formatting by splitting on new lines and ",". For example:

    val matrix = Matrices.dense(2,3, Array(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))
    matrix.toString
      .split("\n")
      .map(_.trim.split(" ").filter(_ != "").mkString("[", ",", "]"))
      .mkString("\n")
    

    which will give the following:

    [1.0,3.0,5.0]
    [2.0,4.0,6.0]
    

    However, if you want to convert the matrix to an DataFrame, the easiest way would be to first create an RDD and then use toDF().

    val matrixRows = matrix.rowIter.toSeq.map(_.toArray)
    val df = spark.sparkContext.parallelize(matrixRows).toDF("Row")
    

    Then to put each value in a separate column you can do the following

    val numOfCols = matrixRows.head.length
    val df2 = (0 until numOfCols).foldLeft(df)((df, num) => 
        df.withColumn("Col" + num, $"Row".getItem(num)))
      .drop("Row")
    df2.show(false)
    

    Result using the example data:

    +----+----+----+
    |Col0|Col1|Col2|
    +----+----+----+
    |1.0 |3.0 |5.0 |
    |2.0 |4.0 |6.0 |
    +----+----+----+
    

    0 讨论(0)
提交回复
热议问题