【问题标题】:How to display the result of a BlockMatrix multiplication in PySpark?如何在 PySpark 中显示 BlockMatrix 乘法的结果?
【发布时间】:2021-06-07 09:17:33
【问题描述】:

这听起来像一个简单的问题,但我无法弄清楚如何将 pyspark BlockMatrix 的内容显示到控制台。我应该调用什么方法才能真正看到我的结果?

【问题讨论】:

    标签: python apache-spark pyspark apache-spark-mllib


    【解决方案1】:

    您可以拨打toLocalMatrix()。我使用了Spark MLLib Python API docs 中的示例矩阵来说明:

    mat = mat1.toLocalMatrix()
    
    # which returns a DenseMatrix
    # DenseMatrix(6, 2, [1.0, 2.0, 3.0, 7.0, 8.0, 9.0, 4.0, 5.0, 6.0, 10.0, 11.0, 12.0], 0)
    
    # and could be further converted to a numpy array using `.toArray()`:
    np_mat = mat.toArray()
    # array([[ 1.,  4.],
    #        [ 2.,  5.],
    #        [ 3.,  6.],
    #        [ 7., 10.],
    #        [ 8., 11.],
    #        [ 9., 12.]])
    

    【讨论】:

    • 谢谢!所需要的只是 print(mat) 以显示本地矩阵。我又花了几分钟才意识到这个对象是可打印的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多