You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by mengxr <gi...@git.apache.org> on 2014/06/02 20:53:49 UTC

[GitHub] spark pull request: [SPARK-1752][MLLIB] Standardize text format fo...

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

    https://github.com/apache/spark/pull/685#discussion_r13300795
  
    --- Diff: python/pyspark/mllib/util.py ---
    @@ -160,6 +157,40 @@ def saveAsLibSVMFile(data, dir):
             lines.saveAsTextFile(dir)
     
     
    +    @staticmethod
    +    def loadLabeledPoints(sc, path, minPartitions=None):
    +        """
    +        Load labeled points saved using RDD.saveAsTextFile.
    +
    +        @param sc: Spark context
    +        @param path: file or directory path in any Hadoop-supported file
    +                     system URI
    +        @param minPartitions: min number of partitions
    +        @return: labeled data stored as an RDD of LabeledPoint
    +
    +        >>> from tempfile import NamedTemporaryFile
    +        >>> from pyspark.mllib.util import MLUtils
    +        >>> examples = [LabeledPoint(1.1, Vectors.sparse(3, [(0, -1.23), (2, 4.56e-7)])), \
    +                        LabeledPoint(0.0, Vectors.dense([1.01, 2.02, 3.03]))]
    +        >>> tempFile = NamedTemporaryFile(delete=True)
    +        >>> tempFile.close()
    +        >>> sc.parallelize(examples, 1).saveAsTextFile(tempFile.name)
    +        >>> loaded = MLUtils.loadLabeledPoints(sc, tempFile.name).collect()
    +        >>> type(loaded[0]) == LabeledPoint
    +        True
    +        >>> print examples[0]
    +        (1.1,(3,[0,2],[-1.23,4.56e-07]))
    +        >>> type(examples[1]) == LabeledPoint
    +        True
    +        >>> print examples[1]
    +        (0.0,[1.01,2.02,3.03])
    +        """
    +        minPartitions = minPartitions or min(sc.defaultParallelism, 2)
    +        jSerialized = sc._jvm.PythonMLLibAPI().loadLabeledPoints(sc._jsc, path, minPartitions)
    +        serialized = RDD(jSerialized, sc, NoOpSerializer())
    +        return serialized.map(lambda bytes: _deserialize_labeled_point(bytearray(bytes)))
    +
    --- End diff --
    
    It doesn't pair with `saveAsTextFile`, because there are many vector types in pyspark. Users need to call `map(Vector.stringify).saveAsTextFile`


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