Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/5872#discussion_r29879018
--- Diff: python/pyspark/mllib/linalg.py ---
@@ -387,8 +424,73 @@ def __init__(self, size, *args):
if self.indices[i] >= self.indices[i + 1]:
raise TypeError("indices array must be sorted")
+ def numNonzeros(self):
+ return np.count_nonzero(self.values)
+
+ def norm(self, p):
+ """
+ Calculte the norm of a SparseVector.
+
+ >>> a = SparseVector(4, [0, 1], [3., -4.])
+ >>> a.norm(1)
+ 7.0
+ >>> a.norm(2)
+ 5.0
+ """
+ return np.linalg.norm(self.values, p)
+
def __reduce__(self):
- return (SparseVector, (self.size, self.indices.tostring(),
self.values.tostring()))
+ return (
+ SparseVector,
+ (self.size, self.indices.tostring(), self.values.tostring()))
+
+ @staticmethod
+ def parse(s):
+ """
+ Parse string representation back into the DenseVector.
+
+ >>> SparseVector.parse(' (4, [0,1 ],[ 4.0,5.0] )')
+ SparseVector(4, {0: 4.0, 1: 5.0})
+ """
+ start = s.find('(')
+ if start == -1:
+ raise ValueError("Tuple should start with '('")
+ end = s.find(')')
+ if start == -1:
+ raise ValueError("Tuple should end with ')'")
+ s = s[start + 1: end].strip()
+
+ size = s[: s.find(',')]
+ try:
+ size = int(size)
+ except ValueError:
+ raise ValueError("Cannot parse size %s." % size)
+
+ ind_start = s.find('[')
+ if ind_start == -1:
+ raise ValueError("Indices array should start with '('.")
+ ind_end = s.find(']')
+ if ind_end == -1:
+ raise ValueError("Indices array should end with ')'")
+ ind_list = s[ind_start + 1: ind_end].split(',')
+ try:
+ indices = [int(ind) for ind in ind_list]
+ except ValueError:
+ raise ValueError("Unabel to parse indices.")
+ s = s[ind_end + 1:].strip()
+
+ val_start = s.find('[')
+ if val_start == -1:
+ raise ValueError("Values array should start with '('.")
+ val_end = s.find(']')
+ if val_end == -1:
+ raise ValueError("Values array should end with ')'.")
+ val_list = s[val_start + 1: val_end].split(',')
+ try:
+ values = [float(val) for val in val_list]
+ except ValueError:
+ raise ValueError("Unable to parse values.")
--- End diff --
Print full string 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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]