如何在pyspark中将DenseMatrix转换为Spark DataFrame? [英] How to convert DenseMatrix to spark DataFrame in pyspark?

查看:415
本文介绍了如何在pyspark中将DenseMatrix转换为Spark DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除以下使用Scala的示例外,我没有找到任何将矩阵转换为Spark数据框的pyspark代码.有人知道如何使用python吗?

I didn't find any pyspark code to convert matrix to spark dataframe except the following example using Scala. Does anyone know how to use python instead?

如何将mllib矩阵转换为火花数据框?

推荐答案

我们可以使用toArray()方法将DenseMatrix转换为numpy ndarray,并使用tolist()从数组转换为列表.

We can use toArray() method to convert DenseMatrix to numpy ndarray and tolist() to convert from array to list.

>>> m = DenseMatrix(2, 2, range(4))
>>> m
DenseMatrix(2, 2, [0.0, 1.0, 2.0, 3.0], False)
>>> rows = m.toArray().tolist()
>>> rows
[[0.0, 2.0], [1.0, 3.0]]
>>> df = spark.createDataFrame(rows,['col1','col2'])
>>> df.show()
+----+----+
|col1|col2|
+----+----+
| 0.0| 2.0|
| 1.0| 3.0|
+----+----+

这篇关于如何在pyspark中将DenseMatrix转换为Spark DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆