Package org.apache.spark.ml.linalg
Class SparseMatrix
Object
org.apache.spark.ml.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
is stored as 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 Summary
ConstructorDescriptionSparseMatrix
(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 Summary
Modifier and TypeMethodDescriptiondouble
apply
(int i, int j) Gets the (i, j)-th element.scala.collection.Iterator<Vector>
colIter()
Returns an iterator of column vectors.int[]
colPtrs()
copy()
Get a deep copy of the matrix.boolean
void
foreachActive
(scala.Function3<Object, Object, Object, scala.runtime.BoxedUnit> f) Applies a functionf
to all the active elements of dense and sparse matrix.static SparseMatrix
fromCOO
(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object, Object, Object>> entries) Generate aSparseMatrix
from Coordinate List (COO) format.int
hashCode()
boolean
Flag that keeps track whether the matrix is transposed or not.int
Find the number of values stored explicitly.int
numCols()
Number of columns.int
Find the number of non-zero active values.int
numRows()
Number of rows.int[]
static SparseMatrix
Generate a diagonal matrix inSparseMatrix
format from the supplied values.static SparseMatrix
speye
(int n) Generate an Identity Matrix inSparseMatrix
format.static SparseMatrix
Generate aSparseMatrix
consisting ofi.i.d
.static SparseMatrix
Generate aSparseMatrix
consisting ofi.i.d
.Transpose the Matrix.double[]
values()
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.spark.ml.linalg.Matrix
compressed, compressedColMajor, compressedRowMajor, getDenseSizeInBytes, getSparseSizeInBytes, isColMajor, isRowMajor, multiply, multiply, multiply, rowIter, toArray, toDense, toDenseColMajor, toDenseRowMajor, toSparse, toSparseColMajor, toSparseRowMajor, toString, toString
-
Constructor Details
-
SparseMatrix
public SparseMatrix(int numRows, int numCols, int[] colPtrs, int[] rowIndices, double[] values, boolean isTransposed) -
SparseMatrix
public 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.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]
.- Parameters:
numRows
- number of rowsnumCols
- number of columnscolPtrs
- the index corresponding to the start of a new columnrowIndices
- the row index of the entry. They must be in strictly increasing order for each columnvalues
- non-zero matrix entries in column major
-
-
Method Details
-
fromCOO
public static SparseMatrix fromCOO(int numRows, int numCols, scala.collection.Iterable<scala.Tuple3<Object, Object, Object>> entries) Generate aSparseMatrix
from 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 matrixnumCols
- number of columns of the matrixentries
- Array of (i, j, value) tuples- Returns:
- The corresponding
SparseMatrix
-
speye
Generate an Identity Matrix inSparseMatrix
format.- Parameters:
n
- number of rows and columns of the matrix- Returns:
SparseMatrix
with sizen
xn
and values of ones on the diagonal
-
sprand
Generate aSparseMatrix
consisting ofi.i.d
. uniform random numbers. The number of non-zero elements equal the ceiling ofnumRows
xnumCols
xdensity
- Parameters:
numRows
- number of rows of the matrixnumCols
- number of columns of the matrixdensity
- the desired density for the matrixrng
- a random number generator- Returns:
SparseMatrix
with sizenumRows
xnumCols
and values in U(0, 1)
-
sprandn
Generate aSparseMatrix
consisting ofi.i.d
. gaussian random numbers.- Parameters:
numRows
- number of rows of the matrixnumCols
- number of columns of the matrixdensity
- the desired density for the matrixrng
- a random number generator- Returns:
SparseMatrix
with sizenumRows
xnumCols
and values in N(0, 1)
-
spdiag
Generate a diagonal matrix inSparseMatrix
format from the supplied values.- Parameters:
vector
- aVector
that will form the values on the diagonal of the matrix- Returns:
- Square
SparseMatrix
with sizevalues.length
xvalues.length
and non-zerovalues
on the diagonal
-
numRows
public int numRows()Description copied from interface:Matrix
Number of rows. -
numCols
public int numCols()Description copied from interface:Matrix
Number of columns. -
colPtrs
public int[] colPtrs() -
rowIndices
public int[] rowIndices() -
values
public double[] values() -
isTransposed
public boolean isTransposed()Description copied from interface:Matrix
Flag that keeps track whether the matrix is transposed or not. False by default.- Specified by:
isTransposed
in interfaceMatrix
-
hashCode
public int hashCode() -
equals
-
apply
public double apply(int i, int j) Description copied from interface:Matrix
Gets the (i, j)-th element. -
copy
Description copied from interface:Matrix
Get a deep copy of the matrix. -
transpose
Description copied from interface:Matrix
Transpose the Matrix. Returns a newMatrix
instance sharing the same underlying data. -
foreachActive
Description copied from interface:Matrix
Applies a functionf
to all the active elements of dense and sparse matrix. The ordering of the elements are not defined.- Specified by:
foreachActive
in interfaceMatrix
- Parameters:
f
- the function takes three parameters where the first two parameters are the row and column indices respectively with the typeInt
, and the final parameter is the corresponding value in the matrix with typeDouble
.
-
numNonzeros
public int numNonzeros()Description copied from interface:Matrix
Find the number of non-zero active values.- Specified by:
numNonzeros
in interfaceMatrix
- Returns:
- (undocumented)
-
numActives
public int numActives()Description copied from interface:Matrix
Find the number of values stored explicitly. These values can be zero as well.- Specified by:
numActives
in interfaceMatrix
- Returns:
- (undocumented)
-
colIter
Description copied from interface:Matrix
Returns an iterator of column vectors. This operation could be expensive, depending on the underlying storage.
-