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

    https://github.com/apache/spark/pull/672#discussion_r12357955
  
    --- Diff: python/pyspark/mllib/util.py ---
    @@ -0,0 +1,168 @@
    +#
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You under the Apache License, Version 2.0
    +# (the "License"); you may not use this file except in compliance with
    +# the License.  You may obtain a copy of the License at
    +#
    +#    http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +#
    +
    +import numpy as np
    +
    +from pyspark.mllib.linalg import Vectors, SparseVector
    +from pyspark.mllib.regression import LabeledPoint
    +from pyspark.mllib._common import _convert_vector
    +
    +class MLUtils:
    +    """
    +    Helper methods to load, save and pre-process data used in ML Lib.
    +    """
    +
    +    @staticmethod
    +    def _parse_libsvm_line(line, multiclass):
    +        """Parses a line in LIBSVM format into (label, indices, values)."""
    +        items = line.split(None)
    +        label = float(items[0])
    +        if not multiclass:
    +            label = 1.0 if label > 0.5 else 0.0
    +        nnz = len(items) - 1
    +        indices = np.zeros(nnz, dtype=np.int32)
    +        values = np.zeros(nnz)
    +        for i in xrange(nnz):
    +            index, value = items[1 + i].split(":")
    +            indices[i] = int(index) - 1
    +            values[i] = float(value)
    +        return label, indices, values
    +
    +
    +    @staticmethod
    +    def _convert_labeled_point_to_libsvm(p):
    +        """Converts a LabeledPoint to a string in LIBSVM format."""
    +        items = [str(p.label)]
    +        v = _convert_vector(p.features)
    +        if type(v) == np.ndarray:
    +            for i in xrange(len(v)):
    +                items.append(str(i + 1) + ":" + str(v[i]))
    +        elif type(v) == SparseVector:
    +            nnz = len(v.indices)
    +            for i in xrange(nnz):
    +                items.append(str(v.indices[i] + 1) + ":" + 
str(v.values[i]))
    +        else:
    +            raise TypeError("_convert_labeled_point_to_libsvm needs either 
ndarray or SparseVector"
    +                            " but got " % type(v))
    --- End diff --
    
    I don't think this can happen here, `_convert_vector` will complain instead


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to