Github user mateiz commented on a diff in the pull request:
https://github.com/apache/spark/pull/672#discussion_r12357905
--- 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))
+ return " ".join(items)
+
+
+ @staticmethod
+ def loadLibSVMFile(sc, path, multiclass=False, numFeatures=-1,
minPartitions=None):
+ """
+ Loads labeled data in the LIBSVM format into an RDD[LabeledPoint].
+ The LIBSVM format is a text-based format used by LIBSVM and
LIBLINEAR.
+ Each line represents a labeled sparse feature vector using the
following format:
+
+ label index1:value1 index2:value2 ...
+
+ where the indices are one-based and in ascending order.
+ This method parses each line into a
[[org.apache.spark.mllib.regression.LabeledPoint]],
+ where the feature indices are converted to zero-based.
+
+ :param sc: Spark context
+ :param path: file or directory path in any Hadoop-supported file
system URI
+ :param multiclass: whether the input labels contain more than two
classes. If false, any
+ label with value greater than 0.5 will be
mapped to 1.0, or 0.0
+ otherwise. So it works for both +1/-1 and 1/0
cases. If true, the double
+ value parsed directly from the label string
will be used as the label
+ value.
+ :param numFeatures: number of features, which will be determined
from the input data if a
+ nonpositive value is given. This is useful
when the dataset is already
+ split into multiple files and you want to load
them separately, because
+ some features may not present in certain
files, which leads to
+ inconsistent feature dimensions.
--- End diff --
Python convention is that doc comments must be at most 72 characters wide,
because they need to be displayed, possibly indented, in peoples' terminals.
Please make these shorter and change the indent of lines below to match other
files (e.g. conf.py, mllib/linalg.py). You can check the result in the built
docs.
---
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.
---