Github user jkbradley commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2378#discussion_r17700424
  
    --- Diff: python/pyspark/mllib/linalg.py ---
    @@ -23,14 +23,148 @@
     SciPy is available in their environment.
     """
     
    -import numpy
    -from numpy import array, array_equal, ndarray, float64, int32
    +import sys
    +import array
    +import copy_reg
     
    +import numpy as np
     
    -__all__ = ['SparseVector', 'Vectors']
    +__all__ = ['Vector', 'DenseVector', 'SparseVector', 'Vectors']
     
     
    -class SparseVector(object):
    +if sys.version_info[:2] == (2, 7):
    +    # speed up pickling array in Python 2.7
    +    def fast_pickle_array(ar):
    +        return array.array, (ar.typecode, ar.tostring())
    +    copy_reg.pickle(array.array, fast_pickle_array)
    +
    +
    +# Check whether we have SciPy. MLlib works without it too, but if we have 
it, some methods,
    +# such as _dot and _serialize_double_vector, start to support scipy.sparse 
matrices.
    +
    +try:
    +    import scipy.sparse
    +    _have_scipy = True
    +except:
    +    # No SciPy in environment, but that's okay
    +    _have_scipy = False
    +
    +
    +def _convert_to_vector(l):
    +    if isinstance(l, Vector):
    +        return l
    +    elif type(l) in (array.array, np.array, np.ndarray, list, tuple):
    +        return DenseVector(l)
    +    elif _have_scipy and scipy.sparse.issparse(l):
    +        assert l.shape[1] == 1, "Expected column vector"
    +        csc = l.tocsc()
    +        return SparseVector(l.shape[0], csc.indices, csc.data)
    +    else:
    +        raise TypeError("Cannot convert type %s into Vector" % type(l))
    +
    +
    +class Vector(object):
    +    """
    +    Abstract class for DenseVector and SparseVector
    +    """
    +    def toArray(self):
    +        """
    +        Convert the vector into an numpy.ndarray
    +        :return: numpy.ndarray
    +        """
    +        raise NotImplementedError
    +
    +
    +class DenseVector(Vector):
    +    def __init__(self, ar):
    +        if not isinstance(ar, array.array):
    +            ar = array.array('d', ar)
    +        self.array = ar
    +
    +    def __reduce__(self):
    +        return DenseVector, (self.array,)
    +
    +    def dot(self, other):
    +        """
    +        Compute the dot product of two Vectors. We support
    +        (Numpy array, list, SparseVector, or SciPy sparse)
    +        and a target NumPy array that is either 1- or 2-dimensional.
    +        Equivalent to calling numpy.dot of the two vectors.
    +
    +        >>> dense = DenseVector(array.array('d', [1., 2.]))
    +        >>> dense.dot(dense)
    +        5.0
    +        >>> dense.dot(SparseVector(2, [0, 1], [2., 1.]))
    +        4.0
    +        >>> dense.dot(range(1, 3))
    +        5.0
    +        >>> dense.dot(np.array(range(1, 3)))
    +        5.0
    +        """
    +        if isinstance(other, SparseVector):
    +            return other.dot(self)
    +        elif _have_scipy and scipy.sparse.issparse(other):
    +            return other.transpose().dot(self.toArray())[0]
    +        elif isinstance(other, Vector):
    +            return np.dot(self.toArray(), other.toArray())
    +        else:
    +            return np.dot(self.toArray(), other)
    +
    +    def squared_distance(self, other):
    +        """
    +        Squared distance of two Vectors.
    +
    +        >>> dense1 = DenseVector(array.array('d', [1., 2.]))
    +        >>> dense1.squared_distance(dense1)
    +        0.0
    +        >>> dense2 = np.array([2., 1.])
    +        >>> dense1.squared_distance(dense2)
    +        2.0
    +        >>> dense3 = [2., 1.]
    +        >>> dense1.squared_distance(dense2)
    --- End diff --
    
    "dense2" --> "dense3"


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to