A local matrix has integer-typed row and column indices and double-typed values, stored on a single machine. MLlib supports dense matrices, whose entry values are stored in a single double array in column-major order, and sparse matrices, whose non-zero entry values are stored in the Compressed Sparse Column (CSC) format in column-major order. For example, the following dense matrixhtml
局部矩陣由整數型行和列的索引和浮點數類型的值組成,存儲在一個單獨節點上。MLlib支持密集矩陣,entry值被存儲在一個一維浮點數數組,以列爲排序主鍵。而稀疏矩陣,non-zero entry值,以Compressed Sparse Column (CSC) 格式存儲,以列主鍵排序。例如,下面的密集矩陣apache
|1.0 2.0|
api
|3.0 4.0|
數組
|5.0 6.0|
ide
is stored in a one-dimensional array [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]
with the matrix size (3, 2)
.spa
被存儲在一個一維數組 [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]裏,矩陣的size爲(3,2)
scala
Scalacode
The base class of local matrices is Matrix
, and we provide two implementations: DenseMatrix
, and SparseMatrix
. We recommend using the factory methods implemented in Matrices
to create local matrices. Remember, local matrices in MLlib are stored in column-major order.orm
局部矩陣的基類是Matrix,咱們提供了兩種實現:DenseMatrix
, and SparseMatrix
. htm
咱們推薦使用Matrices 已經實現的工廠方法來建立局部矩陣。
記住,局部矩陣在MLlib中是以列排序存儲的。
Refer to the Matrix
Scala docs and Matrices
Scala docs for details on the API.
更多信息請參見Matrix
Scala docs and Matrices
Scala docs API。
import org.apache.spark.mllib.linalg.{Matrix, Matrices} // Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0)) // Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0)) val sm: Matrix = Matrices.sparse(3, 2, Array(0, 1, 3), Array(0, 2, 1), Array(9, 6, 8))