How to convert a mllib matrix to a spark dataframe

2020-03-30 06:10发布

问题:

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

val Row(coeff: Matrix) = Correlation.corr(data, "features").head

One of the ways to achieve this is to convert the result into a DataFrame with each value in a separate column and call z.show().

However, looking into the Matrix api I don't see any way to do this.

Is there another straight forward way to achieve this?

Edit:

The dataframe has 50 columns. Just converting to a string would not help as the output get truncated.

回答1:

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 |
+----+----+----+