Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/mllib-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ rowMat = mat.toRowMatrix()

# Convert to a CoordinateMatrix.
coordinateMat = mat.toCoordinateMatrix()

# Convert to a BlockMatrix.
blockMat = mat.toBlockMatrix()
{% endhighlight %}
</div>

Expand Down Expand Up @@ -594,6 +597,9 @@ rowMat = mat.toRowMatrix()

# Convert to an IndexedRowMatrix.
indexedRowMat = mat.toIndexedRowMatrix()

# Convert to a BlockMatrix.
blockMat = mat.toBlockMatrix()
{% endhighlight %}
</div>

Expand Down Expand Up @@ -661,4 +667,39 @@ matA.validate();
BlockMatrix ata = matA.transpose().multiply(matA);
{% endhighlight %}
</div>

<div data-lang="python" markdown="1">

A [`BlockMatrix`](api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.BlockMatrix)
can be created from an `RDD` of sub-matrix blocks, where a sub-matrix block is a
`((blockRowIndex, blockColIndex), sub-matrix)` tuple.

{% highlight python %}
from pyspark.mllib.linalg import Matrices
from pyspark.mllib.linalg.distributed import BlockMatrix

# Create an RDD of sub-matrix blocks.
blocks = sc.parallelize([((0, 0), Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6])),
((1, 0), Matrices.dense(3, 2, [7, 8, 9, 10, 11, 12]))])

# Create a BlockMatrix from an RDD of sub-matrix blocks.
mat = BlockMatrix(blocks, 3, 2)

# Get its size.
m = mat.numRows() # 6
n = mat.numCols() # 2

# Get the blocks as an RDD of sub-matrix blocks.
blocksRDD = mat.blocks

# Convert to a LocalMatrix.
localMat = mat.toLocalMatrix()

# Convert to an IndexedRowMatrix.
indexedRowMat = mat.toIndexedRowMatrix()

# Convert to a CoordinateMatrix.
coordinateMat = mat.toCoordinateMatrix()
{% endhighlight %}
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,21 @@ private[python] class PythonMLLibAPI extends Serializable {
new CoordinateMatrix(entries, numRows, numCols)
}

/**
* Wrapper around BlockMatrix constructor.
*/
def createBlockMatrix(blocks: DataFrame, rowsPerBlock: Int, colsPerBlock: Int,
numRows: Long, numCols: Long): BlockMatrix = {
// We use DataFrames for serialization of sub-matrix blocks from
// Python, so map each Row in the DataFrame back to a
// ((blockRowIndex, blockColIndex), sub-matrix) tuple.
val blockTuples = blocks.map {
case Row(Row(blockRowIndex: Long, blockColIndex: Long), subMatrix: Matrix) =>
((blockRowIndex.toInt, blockColIndex.toInt), subMatrix)
}
new BlockMatrix(blockTuples, rowsPerBlock, colsPerBlock, numRows, numCols)
}

/**
* Return the rows of an IndexedRowMatrix.
*/
Expand All @@ -1147,6 +1162,16 @@ private[python] class PythonMLLibAPI extends Serializable {
val sqlContext = new SQLContext(coordinateMatrix.entries.sparkContext)
sqlContext.createDataFrame(coordinateMatrix.entries)
}

/**
* Return the sub-matrix blocks of a BlockMatrix.
*/
def getMatrixBlocks(blockMatrix: BlockMatrix): DataFrame = {
// We use DataFrames for serialization of sub-matrix blocks to
// Python, so return a DataFrame.
val sqlContext = new SQLContext(blockMatrix.blocks.sparkContext)
sqlContext.createDataFrame(blockMatrix.blocks)
}
}

/**
Expand Down
Loading