Github user MechCoder commented on a diff in the pull request: https://github.com/apache/spark/pull/7554#discussion_r35616054 --- Diff: python/pyspark/mllib/linalg.py --- @@ -1152,9 +1156,392 @@ def sparse(numRows, numCols, colPtrs, rowIndices, values): return SparseMatrix(numRows, numCols, colPtrs, rowIndices, values) +class DistributedMatrix(object): + """ + Represents a distributively stored matrix backed by one or + more RDDs. + + """ + def numRows(self): + """Get or compute the number of rows.""" + raise NotImplementedError + + def numCols(self): + """Get or compute the number of cols.""" + raise NotImplementedError + + +class RowMatrix(DistributedMatrix): + """ + .. note:: Experimental + + Represents a row-oriented distributed Matrix with no meaningful + row indices. + + """ + def __init__(self, rows, numRows=0, numCols=0): + """ + Create a wrapper over a Java RowMatrix. + + :param rows: An RDD of vectors. + :param numRows: Number of rows in the matrix. + :param numCols: Number of columns in the matrix. + """ + if not isinstance(rows, RDD): + raise TypeError("rows should be an RDD of vectors, got %s" % type(rows)) + first = rows.first() + if not isinstance(first, Vector): + rows = rows.map(_convert_to_vector) + + javaRowMatrix = callMLlibFunc("createRowMatrix", rows, long(numRows), int(numCols)) + self._jrm = JavaModelWrapper(javaRowMatrix) + self.rows = rows + + @staticmethod + def _from_java(javaRowMatrix): + """Create a PySpark RowMatrix from a Java RowMatrix object.""" + rows = JavaModelWrapper(javaRowMatrix).call("rows") + return RowMatrix(rows) + + def numRows(self): + """ + Get or compute the number of rows. + + >>> rows = sc.parallelize([[1, 2, 3], [4, 5, 6], + ... [7, 8, 9], [10, 11, 12]]) + + >>> rm = RowMatrix(rows) + >>> int(rm.numRows()) + 4 + + >>> rm = RowMatrix(rows, 7, 6) + >>> int(rm.numRows()) + 7 + """ + return self._jrm.call("numRows") + + def numCols(self): + """ + Get or compute the number of cols. + + >>> rows = sc.parallelize([[1, 2, 3], [4, 5, 6], + ... [7, 8, 9], [10, 11, 12]]) + + >>> rm = RowMatrix(rows) + >>> int(rm.numCols()) + 3 + + >>> rm = RowMatrix(rows, 7, 6) + >>> int(rm.numCols()) + 6 + """ + return self._jrm.call("numCols") + + +class IndexedRow(object): + """ + .. note:: Experimental + + Represents a row of an IndexedRowMatrix. + + Just a wrapper over a (long, vector) tuple. + + :param index: The index for the given row, as a long. + :param vector: The given row, as a vector. + """ + def __init__(self, index, vector): + self.index = long(index) + self.vector = _convert_to_vector(vector) + + def __repr__(self): + return "IndexedRow(%s, %s)" % (self.index, self.vector) + + +def _convert_to_indexed_row(row): + if isinstance(row, IndexedRow): + return row + elif isinstance(row, tuple) and len(row) == 2: + return IndexedRow(*row) + else: + raise TypeError("Cannot convert type %s into IndexedRow" % type(row)) + + +class IndexedRowMatrix(DistributedMatrix): + """ + .. note:: Experimental + + Represents a row-oriented distributed Matrix with indexed rows. + + """ + def __init__(self, rows, numRows=0, numCols=0): + """ + Create a wrapper over a Java IndexedRowMatrix. + + :param rows: An RDD of IndexedRows or (long, vector) tuples. + :param numRows: Number of rows in the matrix. --- End diff -- you could also mention that if it is non-positive, it is set internally (like in the Scala doc)
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org