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

    https://github.com/apache/spark/pull/11663#discussion_r56406644
  
    --- Diff: python/pyspark/ml/param/__init__.py ---
    @@ -65,6 +72,106 @@ def __eq__(self, other):
                 return False
     
     
    +class TypeConverters(object):
    +    """
    +    .. note:: DeveloperApi
    +
    +    Factory methods for common type conversion functions for 
`Param.typeConverter`.
    +
    +    .. versionadded:: 2.0.0
    +    """
    +
    +    @staticmethod
    +    def _is_numeric(value):
    +        vtype = type(value)
    +        return vtype == int or vtype == float or vtype == np.float64 \
    +            or vtype == np.int64 or vtype.__name__ == 'long'
    +
    +    @staticmethod
    +    def _can_convert_to_list(value):
    +        vtype = type(value)
    +        return vtype == list or vtype == np.ndarray or isinstance(value, 
Vector)
    +
    +    @staticmethod
    +    def identity(value):
    +        """
    +        Dummy converter that just returns value.
    +        """
    +        return value
    +
    +    @staticmethod
    +    def convertToList(value):
    +        """
    +        Convert a value to a list, if possible.
    +        """
    +        if type(value) == list:
    +            return value
    +        elif type(value) == np.ndarray:
    +            return list(value)
    +        elif isinstance(value, Vector):
    +            return value.toArray()
    +        else:
    +            raise TypeError("Could not convert %s to list" % value)
    +
    +    @staticmethod
    +    def convertToListFloat(value):
    +        """
    +        Convert a value to list of floats, if possible.
    +        """
    +        if TypeConverters._can_convert_to_list(value) and \
    +                all(map(lambda v: TypeConverters._is_numeric(v), value)):
    --- End diff --
    
    I don't think this will work for SparseVector.  Can you please add a unit 
test for that?  I'd recommend calling ```value = 
TypeConverters.convertToList(value)``` first and then converting values to 
float.


---
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