Skip to content

Commit f9a82a8

Browse files
Davies Liumengxr
authored andcommitted
[SPARK-9138] [MLLIB] fix Vectors.dense
Vectors.dense() should accept numbers directly, like the one in Scala. We already use it in doctests, it worked by luck. cc mengxr jkbradley Author: Davies Liu <[email protected]> Closes #7476 from davies/fix_vectors_dense and squashes the following commits: e0fd292 [Davies Liu] fix Vectors.dense
1 parent 587c315 commit f9a82a8

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

python/pyspark/mllib/linalg.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
basestring = str
3131
xrange = range
3232
import copyreg as copy_reg
33+
long = int
3334
else:
3435
from itertools import izip as zip
3536
import copy_reg
@@ -770,14 +771,18 @@ def sparse(size, *args):
770771
return SparseVector(size, *args)
771772

772773
@staticmethod
773-
def dense(elements):
774+
def dense(*elements):
774775
"""
775-
Create a dense vector of 64-bit floats from a Python list. Always
776-
returns a NumPy array.
776+
Create a dense vector of 64-bit floats from a Python list or numbers.
777777
778778
>>> Vectors.dense([1, 2, 3])
779779
DenseVector([1.0, 2.0, 3.0])
780+
>>> Vectors.dense(1.0, 2.0)
781+
DenseVector([1.0, 2.0])
780782
"""
783+
if len(elements) == 1 and not isinstance(elements[0], (float, int, long)):
784+
# it's list, numpy.array or other iterable object.
785+
elements = elements[0]
781786
return DenseVector(elements)
782787

783788
@staticmethod

0 commit comments

Comments
 (0)