Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ object LinearRegression extends App {

Logger.getRootLogger.setLevel(Level.WARN)

val examples = MLUtils.loadLibSVMFile(sc, params.input, multiclass = true).cache()
val examples = MLUtils.loadLibSVMFile(sc, params.input).cache()

val splits = examples.randomSplit(Array(0.8, 0.2))
val training = splits(0).cache()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import scopt.OptionParser

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.classification.NaiveBayes
import org.apache.spark.mllib.util.{MLUtils, MulticlassLabelParser}
import org.apache.spark.mllib.util.MLUtils

/**
* An example naive Bayes app. Run with
Expand Down Expand Up @@ -76,7 +76,7 @@ object SparseNaiveBayes {
if (params.minPartitions > 0) params.minPartitions else sc.defaultMinPartitions

val examples =
MLUtils.loadLibSVMFile(sc, params.input, multiclass = true, params.numFeatures, minPartitions)
MLUtils.loadLibSVMFile(sc, params.input, params.numFeatures, minPartitions)
// Cache examples because it will be used in both training and evaluation.
examples.cache()

Expand Down

This file was deleted.

52 changes: 16 additions & 36 deletions mllib/src/main/scala/org/apache/spark/mllib/util/MLUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ object MLUtils {
*
* @param sc Spark context
* @param path file or directory path in any Hadoop-supported file system URI
* @param labelParser parser for labels
* @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
Expand All @@ -64,18 +63,17 @@ object MLUtils {
* @param minPartitions min number of partitions
* @return labeled data stored as an RDD[LabeledPoint]
*/
private def loadLibSVMFile(
def loadLibSVMFile(
sc: SparkContext,
path: String,
labelParser: LabelParser,
numFeatures: Int,
minPartitions: Int): RDD[LabeledPoint] = {
val parsed = sc.textFile(path, minPartitions)
.map(_.trim)
.filter(line => !(line.isEmpty || line.startsWith("#")))
.map { line =>
val items = line.split(' ')
val label = labelParser.parse(items.head)
val label = items.head.toDouble
val (indices, values) = items.tail.map { item =>
val indexAndValue = item.split(':')
val index = indexAndValue(0).toInt - 1 // Convert 1-based indices to 0-based.
Expand All @@ -102,64 +100,46 @@ object MLUtils {

// Convenient methods for `loadLibSVMFile`.

/**
* 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.
* @param minPartitions min number of partitions
* @return labeled data stored as an RDD[LabeledPoint]
*/
def loadLibSVMFile(
@deprecated("use method without multiclass argument, which no longer has effect", "1.1.0")
def loadLibSVMFile(
sc: SparkContext,
path: String,
multiclass: Boolean,
numFeatures: Int,
minPartitions: Int): RDD[LabeledPoint] =
loadLibSVMFile(sc, path, LabelParser.getInstance(multiclass), numFeatures, minPartitions)
loadLibSVMFile(sc, path, numFeatures, minPartitions)

/**
* Loads labeled data in the LIBSVM format into an RDD[LabeledPoint], with the default number of
* partitions.
*/
def loadLibSVMFile(
sc: SparkContext,
path: String,
numFeatures: Int): RDD[LabeledPoint] =
loadLibSVMFile(sc, path, numFeatures, sc.defaultMinPartitions)

@deprecated("use method without multiclass argument, which no longer has effect", "1.1.0")
def loadLibSVMFile(
sc: SparkContext,
path: String,
multiclass: Boolean,
numFeatures: Int): RDD[LabeledPoint] =
loadLibSVMFile(sc, path, multiclass, numFeatures, sc.defaultMinPartitions)
loadLibSVMFile(sc, path, numFeatures)

/**
* Loads labeled data in the LIBSVM format into an RDD[LabeledPoint], with the number of features
* determined automatically and the default number of partitions.
*/
@deprecated("use method without multiclass argument, which no longer has effect", "1.1.0")
def loadLibSVMFile(
sc: SparkContext,
path: String,
multiclass: Boolean): RDD[LabeledPoint] =
loadLibSVMFile(sc, path, multiclass, -1, sc.defaultMinPartitions)
loadLibSVMFile(sc, path)

/**
* Loads binary labeled data in the LIBSVM format into an RDD[LabeledPoint], with number of
* features determined automatically and the default number of partitions.
*/
def loadLibSVMFile(sc: SparkContext, path: String): RDD[LabeledPoint] =
loadLibSVMFile(sc, path, multiclass = false, -1, sc.defaultMinPartitions)
loadLibSVMFile(sc, path, -1)

/**
* Save labeled data in LIBSVM format.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ class MLUtilsSuite extends FunSuite with LocalSparkContext {
test("loadLibSVMFile") {
val lines =
"""
|+1 1:1.0 3:2.0 5:3.0
|-1
|-1 2:4.0 4:5.0 6:6.0
|1 1:1.0 3:2.0 5:3.0
|0
|0 2:4.0 4:5.0 6:6.0
""".stripMargin
val tempDir = Files.createTempDir()
tempDir.deleteOnExit()
val file = new File(tempDir.getPath, "part-00000")
Files.write(lines, file, Charsets.US_ASCII)
val path = tempDir.toURI.toString

val pointsWithNumFeatures = loadLibSVMFile(sc, path, multiclass = false, 6).collect()
val pointsWithNumFeatures = loadLibSVMFile(sc, path, 6).collect()
val pointsWithoutNumFeatures = loadLibSVMFile(sc, path).collect()

for (points <- Seq(pointsWithNumFeatures, pointsWithoutNumFeatures)) {
Expand All @@ -86,11 +86,11 @@ class MLUtilsSuite extends FunSuite with LocalSparkContext {
assert(points(2).features === Vectors.sparse(6, Seq((1, 4.0), (3, 5.0), (5, 6.0))))
}

val multiclassPoints = loadLibSVMFile(sc, path, multiclass = true).collect()
val multiclassPoints = loadLibSVMFile(sc, path).collect()
assert(multiclassPoints.length === 3)
assert(multiclassPoints(0).label === 1.0)
assert(multiclassPoints(1).label === -1.0)
assert(multiclassPoints(2).label === -1.0)
assert(multiclassPoints(1).label === 0.0)
assert(multiclassPoints(2).label === 0.0)

Utils.deleteRecursively(tempDir)
}
Expand Down
8 changes: 8 additions & 0 deletions project/MimaExcludes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ object MimaExcludes {
"org.apache.spark.mllib.tree.impurity.Entropy.calculate"),
ProblemFilters.exclude[IncompatibleMethTypeProblem](
"org.apache.spark.mllib.tree.impurity.Variance.calculate")
) ++
Seq ( // Package-private classes removed in SPARK-2341
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.BinaryLabelParser"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.BinaryLabelParser$"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.LabelParser"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.LabelParser$"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.MulticlassLabelParser"),
ProblemFilters.exclude[MissingClassProblem]("org.apache.spark.mllib.util.MulticlassLabelParser$")
)
case v if v.startsWith("1.0") =>
Seq(
Expand Down
23 changes: 12 additions & 11 deletions python/pyspark/mllib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ class MLUtils:
Helper methods to load, save and pre-process data used in MLlib.
"""

@deprecated
@staticmethod
def _parse_libsvm_line(line, multiclass):
return _parse_libsvm_line(line)

@staticmethod
def _parse_libsvm_line(line):
"""
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)
Expand All @@ -64,8 +67,13 @@ def _convert_labeled_point_to_libsvm(p):
" but got " % type(v))
return " ".join(items)

@deprecated
@staticmethod
def loadLibSVMFile(sc, path, multiclass=False, numFeatures=-1, minPartitions=None):
return loadLibSVMFile(sc, path, numFeatures, minPartitions)

@staticmethod
def loadLibSVMFile(sc, path, numFeatures=-1, minPartitions=None):
"""
Loads labeled data in the LIBSVM format into an RDD of
LabeledPoint. The LIBSVM format is a text-based format used by
Expand All @@ -81,13 +89,6 @@ def loadLibSVMFile(sc, path, multiclass=False, numFeatures=-1, minPartitions=Non
@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
Expand All @@ -105,7 +106,7 @@ def loadLibSVMFile(sc, path, multiclass=False, numFeatures=-1, minPartitions=Non
>>> tempFile.write("+1 1:1.0 3:2.0 5:3.0\\n-1\\n-1 2:4.0 4:5.0 6:6.0")
>>> tempFile.flush()
>>> examples = MLUtils.loadLibSVMFile(sc, tempFile.name).collect()
>>> multiclass_examples = MLUtils.loadLibSVMFile(sc, tempFile.name, True).collect()
>>> multiclass_examples = MLUtils.loadLibSVMFile(sc, tempFile.name).collect()
>>> tempFile.close()
>>> type(examples[0]) == LabeledPoint
True
Expand All @@ -124,7 +125,7 @@ def loadLibSVMFile(sc, path, multiclass=False, numFeatures=-1, minPartitions=Non
"""

lines = sc.textFile(path, minPartitions)
parsed = lines.map(lambda l: MLUtils._parse_libsvm_line(l, multiclass))
parsed = lines.map(lambda l: MLUtils._parse_libsvm_line(l))
if numFeatures <= 0:
parsed.cache()
numFeatures = parsed.map(lambda x: 0 if x[1].size == 0 else x[1][-1]).reduce(max) + 1
Expand Down