You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by itg-abby <gi...@git.apache.org> on 2016/10/15 00:07:30 UTC
[GitHub] spark pull request #15496: Match SparseVector behavior with DenseVector
GitHub user itg-abby opened a pull request:
https://github.com/apache/spark/pull/15496
Match SparseVector behavior with DenseVector
## What changes were proposed in this pull request?
Simply added the `__getattr__` to SparseVector that DenseVector has, but calls self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
i.e. you can simply call SparseVector.mean() to average the values in the entire vector.
## How was this patch tested?
Manual testing on local machine.
No UI changes.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/itg-abby/spark patch-2
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/15496.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 #15496
----
commit d9b6ecc9c7aaebf834556797404ff783fef44dcf
Author: itg-abby <ab...@gmail.com>
Date: 2016-10-15T00:03:08Z
Match SparseVector behavior with DenseVector
<< What changes were proposed in this pull request? >>
Simply added the __getattr__ to SparseVector that DenseVector has, but calls to self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
<<How was this patch tested?>>
Manual testing on local machine.
----
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r93116390
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,23 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((np.append(self.values, 0),
--- End diff --
More concretely thouhh, why are we padding the data/values with a 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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Sure - I'll try and take a look tomorrow just driving back from Monterey
tonight.
On Sun, Dec 18, 2016 at 4:28 PM AbderRahman N. Sobh <
notifications@github.com> wrote:
> ping @holdenk <https://github.com/holdenk>, can you take a look at this
> please? sorry for leaving this off for so long.
>
>
>
>
> \u2014
> You are receiving this because you were mentioned.
>
> Reply to this email directly, view it on GitHub
> <https://github.com/apache/spark/pull/15496#issuecomment-267858571>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AADp9T7TGN_IN6PQwTjcqc1vdW5EoTXBks5rJc-sgaJpZM4KXjGl>
> .
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby closed the pull request at:
https://github.com/apache/spark/pull/15496
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark pull request #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by itg-abby <gi...@git.apache.org>.
GitHub user itg-abby reopened a pull request:
https://github.com/apache/spark/pull/15496
[SPARK-17950] [Python] Match SparseVector behavior with DenseVector
## What changes were proposed in this pull request?
Simply added the `__getattr__` to SparseVector that DenseVector has, but calls self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
i.e. you can simply call SparseVector.mean() to average the values in the entire vector.
## How was this patch tested?
Manual testing on local machine.
Temporary close: Failed ./python/run-tests
No UI changes.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/itg-abby/spark patch-2
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/15496.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 #15496
----
commit d9b6ecc9c7aaebf834556797404ff783fef44dcf
Author: itg-abby <ab...@gmail.com>
Date: 2016-10-15T00:03:08Z
Match SparseVector behavior with DenseVector
<< What changes were proposed in this pull request? >>
Simply added the __getattr__ to SparseVector that DenseVector has, but calls to self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
<<How was this patch tested?>>
Manual testing on local machine.
----
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Thanks for working on this and taking the time to update to avoid the array copy :) There doesn't appear to be any tests for the new functionality which is generally a requirement and that could help us see how we would expect people to use this as well. Also if we make this change here we would probably want to something similar in `ml/linalg/` as well.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
@holdenk , are we there yet :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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Jenkins Ok to test
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r86468202
--- Diff: python/pyspark/mllib/tests.py ---
@@ -861,6 +861,19 @@ def test_dot(self):
dv = DenseVector(array([1., 2., 3., 4.]))
self.assertEqual(10.0, dv.dot(lil))
+ def test_assorted_functs(self):
--- End diff --
It would be good to have the same tests for ml as well.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Thanks! I'm glad you got rid of the append (and sorry this slipped my radar). I'll add this to my queue to review and try and take a more thorough look this week. I'm sorry its taken so long.
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r84179904
--- Diff: python/pyspark/mllib/linalg/__init__.py ---
@@ -794,6 +794,13 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((self.values, self.indices, [0, 2]))
+ return getattr(csr, item)
+ else:
+ return self
--- End diff --
This doesn't look like the correct behaviour. Can you test this on a system without scipy installed? (Probably easist with a simple virtualenv).
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by srowen <gi...@git.apache.org>.
Github user srowen commented on the issue:
https://github.com/apache/spark/pull/15496
As I say on the JIRA, if I understand this correctly, this turns O(n) operations into O(n^2) etc. I don't think that actually helps anything.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
@sethah
Thanks for your input! SciPy is definitely a much better solution, I was naively operating under the assumption that I must match DenseVector types 1:1.
Actually, there is already a flag "_have_scipy" so I have added the check for that in order to create the type when it is available. Otherwise, users just get the standard SparseVector back.
The PR has been updated.
---
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 #15496: Match SparseVector behavior with DenseVector
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby closed the pull request at:
https://github.com/apache/spark/pull/15496
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by itg-abby <gi...@git.apache.org>.
GitHub user itg-abby reopened a pull request:
https://github.com/apache/spark/pull/15496
[SPARK-17950] [Python] Match SparseVector behavior with DenseVector
## What changes were proposed in this pull request?
Simply added the `__getattr__` to SparseVector that DenseVector has, but calls self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
i.e. you can simply call SparseVector.mean() to average the values in the entire vector.
## How was this patch tested?
Manual testing on local machine.
Passed ./dev/run-tests
No UI changes.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/itg-abby/spark patch-2
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/15496.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 #15496
----
commit d9b6ecc9c7aaebf834556797404ff783fef44dcf
Author: itg-abby <ab...@gmail.com>
Date: 2016-10-15T00:03:08Z
Match SparseVector behavior with DenseVector
<< What changes were proposed in this pull request? >>
Simply added the __getattr__ to SparseVector that DenseVector has, but calls to self.toArray() instead of storing a vector all the time in self.array
This allows for use of numpy functions on the values of a SparseVector in the same direct way that users interact with DenseVectors.
<<How was this patch tested?>>
Manual testing on local machine.
----
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
I have applied the code change to both ML and MLLIB now. And, I added some simple tests to check if the SciPy sparse functions are behaving correctly. (Only MLLIB has tests for SciPy functions so I only added test cases there).
Additionally, I updated the implementation with a wrapper script which 1) allows for functions with inputs to work correctly and 2) seamlessly allows for SciPy's functions which generate a SciPy matrix output to be automatically returned as a SparseVector object.
Example use case:
```
c = SparseVector(40, {1: 1, 3: 2, 23: 7, 25:9, 39:3})
c.power(4)
SparseVector(40, {1: 1.0, 3: 16.0, 23: 2401.0, 25: 6561.0, 39: 81.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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
Whoops, looks like this code is fine and I just had a bug in my local build. Reopening.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
Hi, can you give a ref to the new style guides? I'm not sure if anything major needs changing.
In the meantime I resolved the one conflict at the bottom of ml/tests.py and extended the docstring to include your comment.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Oh I'm sorry I haven't had a chance l try and take a look soon.
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r90139924
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,22 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((np.append(self.values, 0),
+ np.append(self.indices, self.size-1),
+ [0, len(self.values)]))
+ func = getattr(csr, item)
+ result = func(*args, **kwargs)
+ if isinstance(result, scipy.sparse.csr.csr_matrix):
+ return SparseVector(result.shape[1],result.indices,result.data)
+ return result
+ else:
+ raise AttributeError(
+ "'{0}' object has no attribute '{1}' or SciPy not installed.".format(self.__class__, item))
--- End diff --
Ok so maybe we can improve the error message to something like "'{0}' object has no attribute '{1}' and SciPy is not installed to proxy request to SparseVector" (or similar).
Because saying its X or Y is confusing since this error message only happens in the event SciPy is not installed.
What do you think?
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r86468128
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,22 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scip:
+ csr = scipy.sparse.csr_matrix((\
--- End diff --
Do we need the `\`s?
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
Sorry, I forgot to ping anyone after the last update.
@holdenk how does this look? What next steps should we take to push this PR?
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
@holdenk
Thanks for the review and your availability, I really appreciate the work you are doing too by mentoring me! I didn't realize np.append was making copies, though it made a lot of sense once I thought about what is actually going on there in terms of memory.
The appends were also ultimately unnecessary and have been removed in favor of a much better, simpler, efficient call to make the csr 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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
@holdenk
I did a quick benchmark on my Macbook to get a single CSR's approximate construction time (in minutes) on a large case as well as compared to LIL and DOK types.
2mil x 2mil Elements:
CSR, 1.157418
DOK, 5.531311
LIL, 0.839315
LIL2CSR, 1.650390
Snippet used for benchmarking:
```
setup_small1 = 'from scipy.sparse import csr_matrix;'
method1 = 'csr_matrix((range(0, 2000000),range(0, 2000000),[0, 2000000]))'
setup_small2 = 'from scipy.sparse import dok_matrix;'
method2 = 'dok_matrix((range(0, 2000000),range(0, 2000000)))'
setup_small3 = 'from scipy.sparse import lil_matrix;'
method3 = 'lil_matrix((2000000,2000000))'
setup_small4 = 'from scipy.sparse import lil_matrix;'
method4 = 'lil_matrix((2000000,2000000)).tocsr()'
t = timeit.Timer(method1, setup_small1)
print('CSR construction: ' + str(t.timeit(100)/float(100)))
t = timeit.Timer(method2, setup_small2)
print('DOK construction: ' + str(t.timeit(100)/float(100)))
t = timeit.Timer(method3, setup_small3)
print('LIL construction: ' + str(t.timeit(100)/float(100)))
t = timeit.Timer(method4, setup_small4)
print('LIL construction + Convert to CSR: ' + str(t.timeit(100)/float(100)))
```
I expect any changing of the Sparse Vector structure will take place using the PySpark object class, so CSR will definitely outperform LIL and DOK matrix types for function execution as well. This comes from the SciPy documentation [Advantages of the CSR format - efficient arithmetic operations CSR + CSR, CSR * CSR, etc., efficient row slicing, fast matrix vector products / Disadvantages of the CSR format - slow column slicing operations (consider CSC), changes to the sparsity structure are expensive (consider LIL or DOK)].
Other items resolved:
- The \s have been removed
- The error message has been lengthened to mention SciPy not being available as a possible cause for a lack of object attribute (the other cause being a call to something that does not exist in SciPy either).
- I have only added my single test function to ML since I do not know the reason why all of the SciPy tests in MLlib are not in ML.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Sorry for letting this slide for so long. This looks really close, I think now that we don't have append I don't have the concerns with the copy any more. Can you update this to master and we can make sure it passes the new style guides? Would be nice to get for Spark 3 for sure :)
And really sorry this slipped my plate.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
While you update to master i might include in the docstring that the similar funcitonality in densevector is done with manual delegation in `_delegate`.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Gentle ping, are you still interested in this?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark pull request #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r84350100
--- Diff: python/pyspark/mllib/linalg/__init__.py ---
@@ -794,6 +794,13 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((self.values, self.indices, [0, 2]))
+ return getattr(csr, item)
+ else:
+ return self
--- End diff --
Systems without Scipy return the following:
```
from pyspark.mllib.linalg import SparseVector
a = SparseVector(4, {1: 1.0, 3: 5.5})
a.sum()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'SparseVector' object is not callable
```
This has been corrected in the latest commit.
`__getattr__` essentially is the catch for "any attribute that does not have a name" so standard behavior should be an AttributeError not just returning the object (thank you for catching this!). The new code gives the following error when SciPy is not available:
```
a.sum()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/sobh/spark/python/pyspark/mllib/linalg/__init__.py", line 802, in __getattr__
raise AttributeError("'{0}' object has no attribute '{1}'.".format(self.__class__, item))
AttributeError: '<class 'pyspark.mllib.linalg.SparseVector'>' object has no attribute 'sum'.
```
-----------
Systems with Scipy will give the result as expected:
```
from pyspark.mllib.linalg import SparseVector
a = SparseVector(4, {1: 1.0, 3: 5.5})
a.sum()
6.5
```
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
ping @holdenk, can you take a look at this please? sorry for leaving this off for so long.
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r93112496
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,23 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((np.append(self.values, 0),
--- End diff --
Super minor nit, but using named parameters might make this a bit easier when skimming the code (same for mllin)
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:
https://github.com/apache/spark/pull/15496
@holdenk any comments on this PR?
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on the issue:
https://github.com/apache/spark/pull/15496
Ah sorry, I've been travelling a lot this month - let me take a look once I'm back from Strata singapore. I don't think we are going to get this in for 2.1 (sorry).
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] spark pull request #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r89464558
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,22 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((np.append(self.values, 0),
+ np.append(self.indices, self.size-1),
+ [0, len(self.values)]))
+ func = getattr(csr, item)
+ result = func(*args, **kwargs)
+ if isinstance(result, scipy.sparse.csr.csr_matrix):
+ return SparseVector(result.shape[1],result.indices,result.data)
+ return result
+ else:
+ raise AttributeError(
+ "'{0}' object has no attribute '{1}' or SciPy not installed.".format(self.__class__, item))
--- End diff --
This error message would probably be better of just saying SciPy not installed since its on the else branch of `if _have_scipy` unless I'm missing something?
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r93116698
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,23 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scipy:
+ csr = scipy.sparse.csr_matrix((np.append(self.values, 0),
+ np.append(self.indices, self.size-1),
--- End diff --
So this "works", in that it's skipped by the indexptrs range we supply bellow - but it makes the code confusing to read so why do we need it and maybe we could add a comment explaining why?
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
ping @holdenk , please
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by sethah <gi...@git.apache.org>.
Github user sethah commented on the issue:
https://github.com/apache/spark/pull/15496
@itg-abby I see what you're going for, but I don't think it's a great idea in general. For many operations on sparse vectors, we should not materialize the sparse vector as dense. I'm not sure if it's reasonable to use Scipy, but a better solution to me would be:
````python
def __getattr__(self, item):
csr = scipy.sparse.csr_matrix((self.values, self.indices, [0, 2]))
return getattr(csr, item)
````
As you say, we can alternatively write our own. But the current patch would materialize potentially enormous arrays unnecessarily. Thoughts?
cc @holdenk
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
ping @holdenk , revisiting this PR for the memories today. Wondering whether it was shipworthy in the end, 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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r93113956
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,23 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
--- End diff --
Would be good to have a comment here explaining its purpose.
---
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 #15496: [SPARK-17950] [Python] Match SparseVector behavio...
Posted by holdenk <gi...@git.apache.org>.
Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/15496#discussion_r86468088
--- Diff: python/pyspark/ml/linalg/__init__.py ---
@@ -705,6 +705,22 @@ def __eq__(self, other):
return Vectors._equals(self.indices, self.values, list(xrange(len(other))), other.array)
return False
+ def __getattr__(self, item):
+ def wrapper(*args, **kwargs):
+ if _have_scip:
+ csr = scipy.sparse.csr_matrix((\
+ np.append(self.values, 0),\
+ np.append(self.indices, self.size-1),\
+ [0, len(self.values)]))
+ func = getattr(csr, item)
+ result = func(*args, **kwargs)
+ if isinstance(result, scipy.sparse.csr.csr_matrix):
+ return SparseVector(result.shape[1],result.indices,result.data)
+ return result
+ else:
+ raise AttributeError("'{0}' object has no attribute '{1}'.".format(self.__class__, item))
--- End diff --
This seems like it might be kind of a confusing way to communicate that the user doesn't have scipy installed
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by itg-abby <gi...@git.apache.org>.
Github user itg-abby commented on the issue:
https://github.com/apache/spark/pull/15496
It is possible that I am missing something or that I have unintentionally obfuscated this pull request, I will try summarizing my understanding/purpose and see if it sheds any light:
DenseVector allows calls to numpy directly (i.e. DenseVector.mean() ) and always stores the array values in the object attribute DenseVector.array , this allows for a lot of neat numpy functions to be run on the array values without any trouble.
SparseVector works differently, it never stores the full set of values as a full array. Instead, it uses a 'trick' which only searches non-zero index/value pairs if a specific entry is asked for (this can be found in the __geitem__ attribute for SparseVector). This prevents numpy functions from being usable on the SparseVector since there is no actual array to operate on directly. However, a conversion function is provided, toArray().
The solution proposed can, in effect, be thought of as a purely syntactical shortening from SparseVector.toArray().mean() to simply SparseVector.mean() . Thus, this should not introduce any increased complexity compared to how things are now. The current status of this object is confusing in that the intuitive function-call SparseVector.mean() just throws out an "AttributeError: 'SparseVector' object has no attribute 'mean'".
As mildly hinted at on JIRA, there are even better implementations which could follow this one. For example simply replacing directly calling numpy by manually providing the same functions with reduced complexity. Much along the lines of how __getitem__ was made for SparseVectors, rather than the typical array slicing that DenseVector has.
---
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 issue #15496: [SPARK-17950] [Python] Match SparseVector behavior with ...
Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:
https://github.com/apache/spark/pull/15496
Can one of the admins verify this patch?
---
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