Skip to content

Instantly share code, notes, and snippets.

@lakshay-arora
Created October 22, 2019 10:50
Show Gist options
  • Save lakshay-arora/b872619468ec7bdbc02d986a4054bfa1 to your computer and use it in GitHub Desktop.
Save lakshay-arora/b872619468ec7bdbc02d986a4054bfa1 to your computer and use it in GitHub Desktop.
# import the Matrices
from pyspark.mllib.linalg import Matrices
# create a dense matrix of 3 Rows and 2 columns
matrix_1 = Matrices.dense(3, 2, [1,2,3,4,5,6])
print(matrix_1)
# >> DenseMatrix(3, 2, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0], False)
print(matrix_1.toArray())
"""
>> array([[1., 4.],
[2., 5.],
[3., 6.]])
"""
# create a sparse matrix
matrix_2 = Matrices.sparse(3, 3, [0, 1, 2, 3], [0, 0, 2], [9, 6, 8])
print(matrix_2)
# SparseMatrix(3, 3, [0, 1, 2, 3], [0, 0, 2], [9.0, 6.0, 8.0], False)
print(matrix_2.toArray())
"""
>> array([[9., 6., 0.],
[0., 0., 0.],
[0., 0., 8.]])
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment