You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by MechCoder <gi...@git.apache.org> on 2015/04/03 23:18:33 UTC

[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

GitHub user MechCoder opened a pull request:

    https://github.com/apache/spark/pull/5355

    [SPARK-6577] SparseMatrix should be supported in PySpark

    Supporting of SparseMatrix in PySpark.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/MechCoder/spark spark-6577

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/5355.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #5355
    
----
commit b6384fea2d5173fc50e986a7277ef07191532178
Author: MechCoder <ma...@gmail.com>
Date:   2015-04-03T21:16:37Z

    [SPARK-6577] SparseMatrix should be supported in PySpark

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042932
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    --- End diff --
    
    put `) after `self.isTransposed`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28107453
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        colStart = self.colPtrs[j]
    +        colEnd = self.colPtrs[j + 1]
    +        nz = self.rowIndices[colStart: colEnd]
    +        ind = np.searchsorted(nz, i) + colStart
    +        if ind < colEnd and self.rowIndices[ind] == i:
    +            return self.values[ind]
    +        else:
    +            return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    --- End diff --
    
    This part can be simplified too.
    
    ~~~
    A = np.zeros((self.numRows, self.numCols), dtype=np.float64, order='F')
    for k in xrange(self.colPtrs.size - 1):
        startptr = self.colPtrs[k]
        endptr = self.colPtrs[k + 1]
        if self.isTransposed:
            A[k, self.rowIndices[startptr:endptr]] == self.values[startptr:endptr]
       else:
            A[self.rowIndices[startptr:endptr], k] == self.values[startptr:endptr]
       return A.reshape(self.numRows * self.numCols, order='F')
    ~~~


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042923
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    --- End diff --
    
    `np.uint64` -> `np.int32` (to be consistent with Scala implementation)


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042919
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    --- End diff --
    
    `Matrix.__init__(...)`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28077361
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    +        if nz.size == 0 or i > nz[-1]:
    +            return 0.0
    +        ind = np.searchsorted(nz, i)
    +        if i == nz[ind]:
    +            return self.values[self.colPtrs[j]: self.colPtrs[j + 1]][ind]
    --- End diff --
    
    `nz[ind]` may go out of bound. I would do this:
    
    ~~~python
    ind = np.searchsorted(nz, i) + colStart
    if ind < colEnd and self.indices[ind] == i:
      return self.values[ind]
    else:
      return 0.0
    ~~~


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91443441
  
    Merged into master. Thanks!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91234159
  
      [Test build #29939 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29939/consoleFull) for   PR 5355 at commit [`3d2700e`](https://github.com/apache/spark/commit/3d2700e95d09a52956648c956747a7e80c33f4dc).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91443003
  
      [Test build #30005 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/30005/consoleFull) for   PR 5355 at commit [`7492190`](https://github.com/apache/spark/commit/749219013731436ac64b7578d2e3afef83dc154f).
     * This patch **passes all tests**.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class SparseMatrix(Matrix):`
    
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91297848
  
      [Test build #29948 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29948/consoleFull) for   PR 5355 at commit [`61a864a`](https://github.com/apache/spark/commit/61a864a7f621f7d94132e891e89734acdb8e285b).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28121921
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        colStart = self.colPtrs[j]
    +        colEnd = self.colPtrs[j + 1]
    +        nz = self.rowIndices[colStart: colEnd]
    +        ind = np.searchsorted(nz, i) + colStart
    +        if ind < colEnd and self.rowIndices[ind] == i:
    +            return self.values[ind]
    +        else:
    +            return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    --- End diff --
    
    Thanks. I had a similar idea in mind, but I wrote this to prevent code duplication across `toArray` and `toDense`. In the above code, I'll have to reshape twice on converting to dense. Is that ok?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91324845
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29951/
    Test FAILed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91426570
  
    @mengxr fixed! anything else?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91297859
  
      [Test build #29947 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29947/consoleFull) for   PR 5355 at commit [`454ef2c`](https://github.com/apache/spark/commit/454ef2ca83ccd0046a8a1e714b86505f8059fda9).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-89440610
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29695/
    Test PASSed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042938
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    +        if nz.size == 0 or i > nz[-1]:
    +            return 0.0
    +        ind = np.searchsorted(nz, i)
    +        if i == nz[ind]:
    +            return self.values[self.colPtrs[j]: self.colPtrs[j + 1]][ind]
    +        return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        if self.isTransposed:
    +            offset_margin = self.numCols
    +        else:
    +            offset_margin = self.numRows
    +
    +        offset = 0
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    +            endptr = self.colPtrs[ptr + 1]
    +            sparsearr[offset + self.rowIndices[startptr: endptr]] = \
    +                self.values[startptr: endptr]
    +            offset += offset_margin
    +        return sparsearr
    +
    +    def toArray(self):
    +        """
    +        Return an numpy.ndarray
    +        """
    +        if self.isTransposed:
    +            order = 'C'
    +        else:
    +            order = 'F'
    --- End diff --
    
    I think we should always use Fortran order. We can modify `_densify_values()` such that the output is always column-majored.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91104754
  
    @mengxr @davies Can some initial comments be given on this?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-89440602
  
      [Test build #29695 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29695/consoleFull) for   PR 5355 at commit [`b6384fe`](https://github.com/apache/spark/commit/b6384fea2d5173fc50e986a7277ef07191532178).
     * This patch **passes all tests**.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class SparseMatrix(object):`
    
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28107444
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -670,6 +674,18 @@ def toArray(self):
             """
             return self.values.reshape((self.numRows, self.numCols), order='F')
     
    +    def toSparse(self):
    +        """Convert to SparseMatrix"""
    +        indices = np.nonzero(self.values)[0]
    +        colCount = np.bincount(indices / self.numRows + 1)
    --- End diff --
    
    On second thought, this might be more readable:
    
    ~~~python
    colCounts = np.bincount(indices / self.numRows)
    colPtrs = np.cumsum(np.hstack((0, colCounts, np.zeros(self.numCols - colCounts.size))))
    ~~~


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-90145472
  
    @mengxr I have added support for the transpose flag also.
    
    Also, I'm not sure where the support for scipy sparse matrices should be done? For example, _convert_to_vector is used when a scipy sparse matrix is used in place of a SparseMatrix (https://github.com/apache/spark/blob/master/python/pyspark/mllib/linalg.py#L278) 
    
    I'm not sure it is needed in the current PR. Could you have a look?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91302668
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29946/
    Test FAILed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91302683
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29950/
    Test FAILed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042925
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    --- End diff --
    
    It is cleaner to write
    
    ~~~python
    if self.isTranspose:
      ...
    else:
      ...
    ~~~


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28077362
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    +        if nz.size == 0 or i > nz[-1]:
    +            return 0.0
    --- End diff --
    
    This `if` is not necessary. `np.searchsorted` would implement the same logic (with little overhead).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28052706
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -670,6 +668,23 @@ def toArray(self):
             """
             return self.values.reshape((self.numRows, self.numCols), order='F')
     
    +    def toSparse(self):
    +        """Convert to Sparse Format"""
    +        numRows = self.numRows
    --- End diff --
    
    This will fail if the last (few) columns are only zeros. I have made a minor change to this. Thanks!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042914
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -670,6 +668,23 @@ def toArray(self):
             """
             return self.values.reshape((self.numRows, self.numCols), order='F')
     
    +    def toSparse(self):
    +        """Convert to Sparse Format"""
    +        numRows = self.numRows
    --- End diff --
    
    This could be simplified:
    
    ~~~python
    indices = np.nonzero(self.values)[0]
    colPtrs = np.cumsum(np.bincount(indices / self.numRows + 1))
    values = self.values[indices]
    rowIndices = indices % self.numRows
    return SparseMatrix(self.numRows, self.numCols, colPtrs, indices, values)
    ~~~  


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042937
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    +        if nz.size == 0 or i > nz[-1]:
    +            return 0.0
    +        ind = np.searchsorted(nz, i)
    +        if i == nz[ind]:
    +            return self.values[self.colPtrs[j]: self.colPtrs[j + 1]][ind]
    +        return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        if self.isTransposed:
    +            offset_margin = self.numCols
    +        else:
    +            offset_margin = self.numRows
    +
    +        offset = 0
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    +            endptr = self.colPtrs[ptr + 1]
    +            sparsearr[offset + self.rowIndices[startptr: endptr]] = \
    --- End diff --
    
    put a space before `:`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28053873
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    --- End diff --
    
    Are you sure this is much more beneficial? I think the present code just creates a memory slice and not an array, and I suspect np.searchsorted might be faster than bisect_left. If you still think this should be changed, I shall change it.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91444347
  
    @mengxr I'm really disappointed that I could not get the code right in the first attempt. Hopefully I shall improve in future PR's. Thanks!


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-90138441
  
      [Test build #29744 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29744/consoleFull) for   PR 5355 at commit [`db76caf`](https://github.com/apache/spark/commit/db76cafdf63ca8d1238fd793d8867ae67d5a0d60).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91293470
  
    @mengxr Thanks! I've fixed up your comment.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042916
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    --- End diff --
    
    extends `Matrix`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91317847
  
      [Test build #29947 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29947/consoleFull) for   PR 5355 at commit [`454ef2c`](https://github.com/apache/spark/commit/454ef2ca83ccd0046a8a1e714b86505f8059fda9).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class SparseMatrix(Matrix):`
    
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28121964
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        colStart = self.colPtrs[j]
    +        colEnd = self.colPtrs[j + 1]
    +        nz = self.rowIndices[colStart: colEnd]
    +        ind = np.searchsorted(nz, i) + colStart
    +        if ind < colEnd and self.rowIndices[ind] == i:
    +            return self.values[ind]
    +        else:
    +            return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    --- End diff --
    
    Never mind. I have a better idea. Will fix it up now.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28054087
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    +        if nz.size == 0 or i > nz[-1]:
    +            return 0.0
    +        ind = np.searchsorted(nz, i)
    +        if i == nz[ind]:
    +            return self.values[self.colPtrs[j]: self.colPtrs[j + 1]][ind]
    +        return 0.0
    +
    +    def _densify_values(self):
    +        sparsearr = np.zeros(self.numRows * self.numCols, dtype=np.float64)
    +
    +        if self.isTransposed:
    +            offset_margin = self.numCols
    +        else:
    +            offset_margin = self.numRows
    +
    +        offset = 0
    +        for ptr in xrange(self.colPtrs.size - 1):
    +            startptr = self.colPtrs[ptr]
    +            endptr = self.colPtrs[ptr + 1]
    +            sparsearr[offset + self.rowIndices[startptr: endptr]] = \
    --- End diff --
    
    There should not be a whitespace before `:` according to PEP8 :P


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-89514228
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29704/
    Test PASSed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042935
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    --- End diff --
    
    This block could be optimized (no temp array):
    
    ~~~python
    colStart = self.colPtrs[j]
    colEnd = self.colPtrs[j + 1]
    ind = bisect_left(self.rowIndices, i, colStart, colEnd)
    if ind < colEnd and self.rowIndices[ind] == i:
      return self.values[ind]
    else:
      return 0.0
    ~~~



---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91317669
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29948/
    Test FAILed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91429158
  
    LGTM pending Jenkins.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042947
  
    --- Diff: python/pyspark/mllib/tests.py ---
    @@ -143,6 +143,57 @@ def test_matrix_indexing(self):
                 for j in range(2):
                     self.assertEquals(mat[i, j], expected[i][j])
     
    +    def test_sparse_matrix(self):
    +        # Test sparse matrix creation.
    +        sm1 = SparseMatrix(
    +            3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0])
    +        self.assertEquals(sm1.numRows, 3)
    +        self.assertEquals(sm1.numCols, 4)
    +        self.assertEquals(sm1.colPtrs.tolist(), [0, 2, 2, 4, 4])
    +        self.assertEquals(sm1.rowIndices.tolist(), [1, 2, 1, 2])
    +        self.assertEquals(sm1.values.tolist(), [1.0, 2.0, 4.0, 5.0])
    +
    +        # Test indexing
    +        expected = zeros((3, 4))
    +        expected[1, 0] = 1.0
    +        expected[2, 0] = 2.0
    +        expected[1, 2] = 4.0
    +        expected[2, 2] = 5.0
    +
    +        for i in range(3):
    +            for j in range(4):
    +                self.assertEquals(expected[i, j], sm1[i, j])
    +        self.assertTrue(array_equal(sm1.toArray(), expected))
    +
    +        # Test conversion to dense and sparse.
    +        smnew = sm1.toDense().toSparse()
    +        self.assertEquals(sm1.numRows, smnew.numRows)
    +        self.assertEquals(sm1.numCols, smnew.numCols)
    +        self.assertTrue(array_equal(sm1.colPtrs, smnew.colPtrs))
    +        self.assertTrue(array_equal(sm1.rowIndices, smnew.rowIndices))
    +        self.assertTrue(array_equal(sm1.values, smnew.values))
    +
    +        sm1t = SparseMatrix(
    +            3, 4, [0, 2, 3, 5], [0, 1, 2, 0, 2], [3.0, 2.0, 4.0, 9.0, 8.0],
    +            isTransposed=True)
    +        self.assertEquals(sm1t.numRows, 3)
    +        self.assertEquals(sm1t.numCols, 4)
    +        self.assertEquals(sm1t.colPtrs.tolist(), [0, 2, 3, 5])
    +        self.assertEquals(sm1t.rowIndices.tolist(), [0, 1, 2, 0, 2])
    +        self.assertEquals(sm1t.values.tolist(), [3.0, 2.0, 4.0, 9.0, 8.0])
    +
    +        expected = zeros((3, 4))
    +        expected[0, 0] = 3.0
    +        expected[0, 1] = 2.0
    +        expected[1, 2] = 4.0
    +        expected[2, 0] = 9.0
    +        expected[2, 2] = 8.0
    --- End diff --
    
    ditto


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042921
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    --- End diff --
    
    Shall we define a utility method for this conversion?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28043066
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -670,6 +668,23 @@ def toArray(self):
             """
             return self.values.reshape((self.numRows, self.numCols), order='F')
     
    +    def toSparse(self):
    +        """Convert to Sparse Format"""
    --- End diff --
    
    `to sparse matrix.`


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91233329
  
    @mengxr I have addressed all your comments, except the one-inline one.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91426804
  
      [Test build #30005 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/30005/consoleFull) for   PR 5355 at commit [`7492190`](https://github.com/apache/spark/commit/749219013731436ac64b7578d2e3afef83dc154f).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91317651
  
      [Test build #29948 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29948/consoleFull) for   PR 5355 at commit [`61a864a`](https://github.com/apache/spark/commit/61a864a7f621f7d94132e891e89734acdb8e285b).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds no public classes.
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/5355


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042928
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    --- End diff --
    
    check the lengths of rowIndices and values are equal.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

Posted by MechCoder <gi...@git.apache.org>.
Github user MechCoder commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-89423133
  
    ping @mengxr ?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91443018
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/30005/
    Test PASSed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28107448
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +703,87 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(Matrix):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        Matrix.__init__(self, numRows, numCols)
    +        self.isTransposed = isTransposed
    +        self.colPtrs = self._convert_to_array(colPtrs, np.int32)
    +        self.rowIndices = self._convert_to_array(rowIndices, np.int32)
    +        self.values = self._convert_to_array(values, np.float64)
    +
    +        if self.isTransposed:
    +            if self.colPtrs.size != numRows + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numRows + 1, self.colPtrs.size))
    +        else:
    +            if self.colPtrs.size != numCols + 1:
    +                raise ValueError("Expected colPtrs of size %d, got %d."
    +                                 % (numCols + 1, self.colPtrs.size))
    +        if self.rowIndices.size != self.values.size:
    +            raise ValueError("Expected rowIndices of length %d, got %d."
    +                             % (self.rowIndices.size, self.values.size))
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed)
    +
    +    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:
    --- End diff --
    
    `if j < 0 or j >= self.numCols` (to be consistent)


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91324831
  
      [Test build #29951 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29951/consoleFull) for   PR 5355 at commit [`ea2c54b`](https://github.com/apache/spark/commit/ea2c54bd7f1639b596f200157732d2888bf21e4a).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class SparseMatrix(Matrix):`
    
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91317871
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29947/
    Test FAILed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-90177344
  
      [Test build #29744 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29744/consoleFull) for   PR 5355 at commit [`db76caf`](https://github.com/apache/spark/commit/db76cafdf63ca8d1238fd793d8867ae67d5a0d60).
     * This patch **passes all tests**.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class SparseMatrix(object):`
    
     * This patch does not change any dependencies.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042944
  
    --- Diff: python/pyspark/mllib/tests.py ---
    @@ -143,6 +143,57 @@ def test_matrix_indexing(self):
                 for j in range(2):
                     self.assertEquals(mat[i, j], expected[i][j])
     
    +    def test_sparse_matrix(self):
    +        # Test sparse matrix creation.
    +        sm1 = SparseMatrix(
    +            3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0])
    +        self.assertEquals(sm1.numRows, 3)
    +        self.assertEquals(sm1.numCols, 4)
    +        self.assertEquals(sm1.colPtrs.tolist(), [0, 2, 2, 4, 4])
    +        self.assertEquals(sm1.rowIndices.tolist(), [1, 2, 1, 2])
    +        self.assertEquals(sm1.values.tolist(), [1.0, 2.0, 4.0, 5.0])
    +
    +        # Test indexing
    +        expected = zeros((3, 4))
    +        expected[1, 0] = 1.0
    +        expected[2, 0] = 2.0
    +        expected[1, 2] = 4.0
    +        expected[2, 2] = 5.0
    --- End diff --
    
    We can create `expected` directly (maybe less code to write).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-90177352
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/29744/
    Test PASSed.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] SparseMatrix should be supported ...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-89423926
  
      [Test build #29695 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29695/consoleFull) for   PR 5355 at commit [`b6384fe`](https://github.com/apache/spark/commit/b6384fea2d5173fc50e986a7277ef07191532178).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] SparseMatrix should be su...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28042939
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -695,6 +804,13 @@ def dense(numRows, numCols, values):
             """
             return DenseMatrix(numRows, numCols, values)
     
    +    @staticmethod
    +    def sparse(numRows, numCols, indptr, indices, values):
    --- End diff --
    
    `indptr` -> `colPtrs`, `indices` -> `rowIndices` (must match Scala arg names)


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by mengxr <gi...@git.apache.org>.
Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5355#discussion_r28077224
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -687,6 +702,100 @@ def __eq__(self, other):
                     all(self.values == other.values))
     
     
    +class SparseMatrix(object):
    +    """Sparse Matrix stored in CSC format."""
    +    def __init__(self, numRows, numCols, colPtrs, rowIndices, values,
    +                 isTransposed=False):
    +        self.numRows = numRows
    +        self.numCols = numCols
    +        self.isTransposed = isTransposed
    +        if isinstance(colPtrs, basestring):
    +            self.colPtrs = np.frombuffer(colPtrs, dtype=np.uint64)
    +        else:
    +            self.colPtrs = np.asarray(colPtrs, dtype=np.uint64)
    +
    +        if self.isTransposed and self.colPtrs.size != numRows + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numRows + 1, self.colPtrs.size))
    +        elif not self.isTransposed and self.colPtrs.size != numCols + 1:
    +            raise ValueError("Expected colPtrs of size %d, got %d."
    +                             % (numCols + 1, self.colPtrs.size))
    +        if isinstance(rowIndices, basestring):
    +            self.rowIndices = np.frombuffer(rowIndices, dtype=np.uint64)
    +        else:
    +            self.rowIndices = np.asarray(rowIndices, dtype=np.uint64)
    +        if isinstance(values, basestring):
    +            self.values = np.frombuffer(values, dtype=np.float64)
    +        else:
    +            self.values = np.asarray(values, dtype=np.float64)
    +
    +    def __reduce__(self):
    +        return SparseMatrix, (
    +            self.numRows, self.numCols, self.colPtrs.tostring(),
    +            self.rowIndices.tostring(), self.values.tostring(),
    +            self.isTransposed
    +        )
    +
    +    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))
    +
    +        # If a CSR matrix is given, then the row index should be searched
    +        # for in ColPtrs, and the column index should be searched for in the
    +        # corresponding slice obtained from rowIndices.
    +        if self.isTransposed:
    +            j, i = i, j
    +
    +        nz = self.rowIndices[self.colPtrs[j]: self.colPtrs[j + 1]]
    --- End diff --
    
    I didn't test the performance. Usually it takes less than 10 lookups for sparse matrices. I just feel that this is slightly easier to read. It is okay to keep the current implementation. See my comment below.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request: [SPARK-6577] [MLlib] [PySpark] SparseMatrix sh...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the pull request:

    https://github.com/apache/spark/pull/5355#issuecomment-91298826
  
      [Test build #29951 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/29951/consoleFull) for   PR 5355 at commit [`ea2c54b`](https://github.com/apache/spark/commit/ea2c54bd7f1639b596f200157732d2888bf21e4a).


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org