Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/5872#discussion_r29708718
--- Diff: python/pyspark/mllib/linalg.py ---
@@ -208,9 +209,55 @@ def __init__(self, ar):
ar = ar.astype(np.float64)
self.array = ar
+ def toString(self):
+ """
+ Convert DenseVector to string representation.
+
+ >>> a = DenseVector([0, 1, 2, 3])
+ >>> a.toString()
+ '[0.0,1.0,2.0,3.0]'
+ """
+ return str(self)
+
+ def copy(self):
+ return DenseVector(np.copy(self.array))
+
+ @staticmethod
+ def parse(vectorString):
+ """
+ Parse string representation back into the DenseVector.
+
+ >>> DenseVector.parse('[0.0,1.0,2.0,3.0]')
+ DenseVector([0.0, 1.0, 2.0, 3.0])
+ """
+ vectorString = vectorString[1:-1]
+ return DenseVector([float(val) for val in vectorString.split(',')])
+
def __reduce__(self):
return DenseVector, (self.array.tostring(),)
+ def numNonzeros(self):
+ return np.nonzero(self.array)[0].size
+
+ def norm(self, p):
+ """
+ Calculte the norm of a DenseVector.
+
+ >>> a = DenseVector([0, -1, 2, -3])
+ >>> a.norm(2)
+ 3.7...
+ >>> a.norm(1)
+ 6.0
+ """
+ if p == 1:
+ return np.sum(np.abs(self.array))
--- End diff --
You can use ```numpy.linalg.norm(self.array, 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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]