Package org.apache.spark.mllib.linalg
Class SparseMatrix
Object
org.apache.spark.mllib.linalg.SparseMatrix
- All Implemented Interfaces:
- Serializable,- Matrix
Column-major sparse matrix.
 The entry values are stored in Compressed Sparse Column (CSC) format.
 For example, the following matrix
 
   1.0 0.0 4.0
   0.0 3.0 5.0
   2.0 0.0 6.0
 values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
 rowIndices=[0, 2, 1, 0, 1, 2], colPointers=[0, 2, 3, 6].
 
 param:  numRows number of rows
 param:  numCols number of columns
 param:  colPtrs the index corresponding to the start of a new column (if not transposed)
 param:  rowIndices the row index of the entry (if not transposed). They must be in strictly
                   increasing order for each column
 param:  values nonzero matrix entries in column major (if not transposed)
 param:  isTransposed whether the matrix is transposed. If true, the matrix can be considered
                     Compressed Sparse Row (CSR) format, where colPtrs behaves as rowPtrs,
                     and rowIndices behave as colIndices, and values are stored in row major.
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionSparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values) Column-major sparse matrix.SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed) 
- 
Method SummaryModifier and TypeMethodDescriptiondoubleapply(int i, int j) Gets the (i, j)-th element.asML()Convert this matrix to the new mllib-local representation.scala.collection.Iterator<Vector>colIter()Returns an iterator of column vectors.int[]colPtrs()copy()Get a deep copy of the matrix.booleanstatic SparseMatrixfromCOO(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object, Object, Object>> entries) Generate aSparseMatrixfrom Coordinate List (COO) format.static SparseMatrixConvert new linalg type to spark.mllib type.inthashCode()booleanFlag that keeps track whether the matrix is transposed or not.intFind the number of values stored explicitly.intnumCols()Number of columns.intFind the number of non-zero active values.intnumRows()Number of rows.int[]static SparseMatrixGenerate a diagonal matrix inSparseMatrixformat from the supplied values.static SparseMatrixspeye(int n) Generate an Identity Matrix inSparseMatrixformat.static SparseMatrixGenerate aSparseMatrixconsisting ofi.i.d.static SparseMatrixGenerate aSparseMatrixconsisting ofi.i.d.toDense()Generate aDenseMatrixfrom the givenSparseMatrix.Transpose the Matrix.double[]values()
- 
Constructor Details- 
SparseMatrixpublic SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed) 
- 
SparseMatrixpublic SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values) Column-major sparse matrix. The entry values are stored in Compressed Sparse Column (CSC) format. For example, the following matrix
 is stored as1.0 0.0 4.0 0.0 3.0 5.0 2.0 0.0 6.0values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],rowIndices=[0, 2, 1, 0, 1, 2],colPointers=[0, 2, 3, 6].- Parameters:
- numRows- number of rows
- numCols- number of columns
- colPtrs- the index corresponding to the start of a new column
- rowIndices- the row index of the entry. They must be in strictly increasing order for each column
- values- non-zero matrix entries in column major
 
 
- 
- 
Method Details- 
fromCOOpublic static SparseMatrix fromCOO(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object, Object, Object>> entries) Generate aSparseMatrixfrom Coordinate List (COO) format. Input must be an array of (i, j, value) tuples. Entries that have duplicate values of i and j are added together. Tuples where value is equal to zero will be omitted.- Parameters:
- numRows- number of rows of the matrix
- numCols- number of columns of the matrix
- entries- Array of (i, j, value) tuples
- Returns:
- The corresponding SparseMatrix
 
- 
speyeGenerate an Identity Matrix inSparseMatrixformat.- Parameters:
- n- number of rows and columns of the matrix
- Returns:
- SparseMatrixwith size- nx- nand values of ones on the diagonal
 
- 
sprandGenerate aSparseMatrixconsisting ofi.i.d. uniform random numbers. The number of non-zero elements equal the ceiling ofnumRowsxnumColsxdensity- Parameters:
- numRows- number of rows of the matrix
- numCols- number of columns of the matrix
- density- the desired density for the matrix
- rng- a random number generator
- Returns:
- SparseMatrixwith size- numRowsx- numColsand values in U(0, 1)
 
- 
sprandnGenerate aSparseMatrixconsisting ofi.i.d. gaussian random numbers.- Parameters:
- numRows- number of rows of the matrix
- numCols- number of columns of the matrix
- density- the desired density for the matrix
- rng- a random number generator
- Returns:
- SparseMatrixwith size- numRowsx- numColsand values in N(0, 1)
 
- 
spdiagGenerate a diagonal matrix inSparseMatrixformat from the supplied values.- Parameters:
- vector- a- Vectorthat will form the values on the diagonal of the matrix
- Returns:
- Square SparseMatrixwith sizevalues.lengthxvalues.lengthand non-zerovalueson the diagonal
 
- 
fromMLConvert new linalg type to spark.mllib type. Light copy; only copies references- Parameters:
- m- (undocumented)
- Returns:
- (undocumented)
 
- 
numRowspublic int numRows()Description copied from interface:MatrixNumber of rows.
- 
numColspublic int numCols()Description copied from interface:MatrixNumber of columns.
- 
colPtrspublic int[] colPtrs()
- 
rowIndicespublic int[] rowIndices()
- 
valuespublic double[] values()
- 
isTransposedpublic boolean isTransposed()Description copied from interface:MatrixFlag that keeps track whether the matrix is transposed or not. False by default.- Specified by:
- isTransposedin interface- Matrix
 
- 
equals
- 
hashCodepublic int hashCode()
- 
applypublic double apply(int i, int j) Description copied from interface:MatrixGets the (i, j)-th element.
- 
copyDescription copied from interface:MatrixGet a deep copy of the matrix.
- 
transposeDescription copied from interface:MatrixTranspose the Matrix. Returns a newMatrixinstance sharing the same underlying data.
- 
toDenseGenerate aDenseMatrixfrom the givenSparseMatrix. The new matrix will have isTransposed set to false.- Returns:
- (undocumented)
 
- 
numNonzerospublic int numNonzeros()Description copied from interface:MatrixFind the number of non-zero active values.- Specified by:
- numNonzerosin interface- Matrix
- Returns:
- (undocumented)
 
- 
numActivespublic int numActives()Description copied from interface:MatrixFind the number of values stored explicitly. These values can be zero as well.- Specified by:
- numActivesin interface- Matrix
- Returns:
- (undocumented)
 
- 
colIterDescription copied from interface:MatrixReturns an iterator of column vectors. This operation could be expensive, depending on the underlying storage.
- 
asMLDescription copied from interface:MatrixConvert this matrix to the new mllib-local representation. This does NOT copy the data; it copies references.
 
-