【问题标题】:How to read and write coordinateMatrix to a file in pyspark如何在pyspark中读取和写入坐标矩阵到文件
【发布时间】:2018-05-17 07:08:56
【问题描述】:

我在 pyspark 中有一个非常稀疏的数据框,我将这个数据框转换为 CoordinatedMatrix。现在我想将这个矩阵保存在 hdfs 上,以便以后重用它来训练我的模型。不幸的是,我没有找到任何解决方案。有什么建议吗?

示例:

>> mat.entries.collect()
[MatrixEntry(1, 0, 1.0), MatrixEntry(1, 1, 1.0), MatrixEntry(1, 2, 2.0),MatrixEntry(1, 3, 1.0), MatrixEntry(2, 0, 2.0), MatrixEntry(2, 1, 1.0), MatrixEntry(2, 2, 3.0), MatrixEntry(2, 3, 5.0)]

【问题讨论】:

    标签: apache-spark pyspark


    【解决方案1】:

    这里没有推荐的格式,CoordinateMatrix 的结构非常简单,所以你可以选择几乎任何东西,例如 Parquet:

    mat.entries.toDF().write.parquet(path) 
    

    后来阅读:

    CoordinateMatrix(spark.read.parquet(path).rdd.map(lambda row: MatrixEntry(*row)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多