-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-1434] [MLLIB] change labelParser from anonymous function to trait
This is a patch to address @mateiz 's comment in #245 MLUtils#loadLibSVMData uses an anonymous function for the label parser. Java users won't like it. So I make a trait for LabelParser and provide two implementations: binary and multiclass. Author: Xiangrui Meng <[email protected]> Closes #345 from mengxr/label-parser and squashes the following commits: ac44409 [Xiangrui Meng] use singleton objects for label parsers 3b1a7c6 [Xiangrui Meng] add tests for label parsers c2e571c [Xiangrui Meng] rename LabelParser.apply to LabelParser.parse use extends for singleton 11c94e0 [Xiangrui Meng] add return types 7f8eb36 [Xiangrui Meng] change labelParser from annoymous function to trait
- Loading branch information
Showing
4 changed files
with
97 additions
and
25 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
mllib/src/main/scala/org/apache/spark/mllib/util/LabelParsers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.spark.mllib.util | ||
|
||
/** Trait for label parsers. */ | ||
trait LabelParser extends Serializable { | ||
/** Parses a string label into a double label. */ | ||
def parse(labelString: String): Double | ||
} | ||
|
||
/** | ||
* Label parser for binary labels, which outputs 1.0 (positive) if the value is greater than 0.5, | ||
* or 0.0 (negative) otherwise. So it works with +1/-1 labeling and +1/0 labeling. | ||
*/ | ||
object BinaryLabelParser extends LabelParser { | ||
/** Gets the default instance of BinaryLabelParser. */ | ||
def getInstance(): LabelParser = this | ||
|
||
/** | ||
* Parses the input label into positive (1.0) if the value is greater than 0.5, | ||
* or negative (0.0) otherwise. | ||
*/ | ||
override def parse(labelString: String): Double = if (labelString.toDouble > 0.5) 1.0 else 0.0 | ||
} | ||
|
||
/** | ||
* Label parser for multiclass labels, which converts the input label to double. | ||
*/ | ||
object MulticlassLabelParser extends LabelParser { | ||
/** Gets the default instance of MulticlassLabelParser. */ | ||
def getInstance(): LabelParser = this | ||
|
||
override def parse(labelString: String): Double = labelString.toDouble | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
mllib/src/test/scala/org/apache/spark/mllib/util/LabelParsersSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.apache.spark.mllib.util | ||
|
||
import org.scalatest.FunSuite | ||
|
||
class LabelParsersSuite extends FunSuite { | ||
test("binary label parser") { | ||
for (parser <- Seq(BinaryLabelParser, BinaryLabelParser.getInstance())) { | ||
assert(parser.parse("+1") === 1.0) | ||
assert(parser.parse("1") === 1.0) | ||
assert(parser.parse("0") === 0.0) | ||
assert(parser.parse("-1") === 0.0) | ||
} | ||
} | ||
|
||
test("multiclass label parser") { | ||
for (parser <- Seq(MulticlassLabelParser, MulticlassLabelParser.getInstance())) { | ||
assert(parser.parse("0") == 0.0) | ||
assert(parser.parse("+1") === 1.0) | ||
assert(parser.parse("1") === 1.0) | ||
assert(parser.parse("2") === 2.0) | ||
assert(parser.parse("3") === 3.0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters