Skip to content
Snippets Groups Projects
Commit f9a82a88 authored by Davies Liu's avatar Davies Liu Committed by Xiangrui Meng
Browse files

[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 <davies@databricks.com>

Closes #7476 from davies/fix_vectors_dense and squashes the following commits:

e0fd292 [Davies Liu] fix Vectors.dense
parent 587c315b
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,7 @@ if sys.version >= '3':
basestring = str
xrange = range
import copyreg as copy_reg
long = int
else:
from itertools import izip as zip
import copy_reg
......@@ -770,14 +771,18 @@ class Vectors(object):
return SparseVector(size, *args)
@staticmethod
def dense(elements):
def dense(*elements):
"""
Create a dense vector of 64-bit floats from a Python list. Always
returns a NumPy array.
Create a dense vector of 64-bit floats from a Python list or numbers.
>>> Vectors.dense([1, 2, 3])
DenseVector([1.0, 2.0, 3.0])
>>> Vectors.dense(1.0, 2.0)
DenseVector([1.0, 2.0])
"""
if len(elements) == 1 and not isinstance(elements[0], (float, int, long)):
# it's list, numpy.array or other iterable object.
elements = elements[0]
return DenseVector(elements)
@staticmethod
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment