Skip to content
Snippets Groups Projects
Commit 2fa3b47d authored by MechCoder's avatar MechCoder Committed by Xiangrui Meng
Browse files

[SPARK-6576] [MLlib] [PySpark] DenseMatrix in PySpark should support indexing

Support indexing in DenseMatrices in PySpark

Author: MechCoder <manojkumarsivaraj334@gmail.com>

Closes #5232 from MechCoder/SPARK-6576 and squashes the following commits:

a735078 [MechCoder] Change bounds
a062025 [MechCoder] Matrices are stored in column order
7917bc1 [MechCoder] [SPARK-6576] DenseMatrix in PySpark should support indexing
parent ccafd757
No related branches found
No related tags found
No related merge requests found
...@@ -670,6 +670,16 @@ class DenseMatrix(Matrix): ...@@ -670,6 +670,16 @@ class DenseMatrix(Matrix):
""" """
return self.values.reshape((self.numRows, self.numCols), order='F') return self.values.reshape((self.numRows, self.numCols), order='F')
def __getitem__(self, indices):
i, j = indices
if i < 0 or i >= self.numRows:
raise ValueError("Row index %d is out of range [0, %d)"
% (i, self.numRows))
if j >= self.numCols or j < 0:
raise ValueError("Column index %d is out of range [0, %d)"
% (j, self.numCols))
return self.values[i + j * self.numRows]
def __eq__(self, other): def __eq__(self, other):
return (isinstance(other, DenseMatrix) and return (isinstance(other, DenseMatrix) and
self.numRows == other.numRows and self.numRows == other.numRows and
......
...@@ -135,6 +135,13 @@ class VectorTests(PySparkTestCase): ...@@ -135,6 +135,13 @@ class VectorTests(PySparkTestCase):
for ind in [4, -5, 7.8]: for ind in [4, -5, 7.8]:
self.assertRaises(ValueError, sv.__getitem__, ind) self.assertRaises(ValueError, sv.__getitem__, ind)
def test_matrix_indexing(self):
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10])
expected = [[0, 6], [1, 8], [4, 10]]
for i in range(3):
for j in range(2):
self.assertEquals(mat[i, j], expected[i][j])
class ListTests(PySparkTestCase): class ListTests(PySparkTestCase):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment