forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPARK-3278 added isotonic regression for weighted data. Added tests f…
…or Java api
- Loading branch information
1 parent
05d9048
commit 629a1ce
Showing
5 changed files
with
345 additions
and
63 deletions.
There are no files selected for viewing
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/main/scala/org/apache/spark/mllib/regression/WeightedLabeledPoint.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.regression | ||
|
||
import org.apache.spark.mllib.linalg.Vector | ||
import org.apache.spark.rdd.RDD | ||
|
||
import scala.beans.BeanInfo | ||
|
||
object WeightedLabeledPointConversions { | ||
implicit def labeledPointToWeightedLabeledPoint( | ||
labeledPoint: LabeledPoint): WeightedLabeledPoint = { | ||
WeightedLabeledPoint(labeledPoint.label, labeledPoint.features, 1) | ||
} | ||
|
||
implicit def labeledPointRDDToWeightedLabeledPointRDD( | ||
rdd: RDD[LabeledPoint]): RDD[WeightedLabeledPoint] = { | ||
rdd.map(lp => WeightedLabeledPoint(lp.label, lp.features, 1)) | ||
} | ||
} | ||
|
||
/** | ||
* Labeled point with weight | ||
*/ | ||
@BeanInfo | ||
case class WeightedLabeledPoint(label: Double, features: Vector, weight: Double) |
57 changes: 57 additions & 0 deletions
57
mllib/src/main/scala/org/apache/spark/mllib/util/IsotonicDataGenerator.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,57 @@ | ||
/* | ||
* 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.apache.spark.mllib.linalg.Vectors | ||
import org.apache.spark.mllib.regression.WeightedLabeledPointConversions._ | ||
import org.apache.spark.mllib.regression.{LabeledPoint, WeightedLabeledPoint} | ||
|
||
import scala.collection.JavaConversions._ | ||
|
||
object IsotonicDataGenerator { | ||
|
||
/** | ||
* Return a Java List of ordered labeled points | ||
* @param labels list of labels for the data points | ||
* @return Java List of input. | ||
*/ | ||
def generateIsotonicInputAsList(labels: Array[Double]): java.util.List[WeightedLabeledPoint] = { | ||
seqAsJavaList(generateIsotonicInput(wrapDoubleArray(labels):_*)) | ||
} | ||
|
||
/** | ||
* Return an ordered sequence of labeled data points with default weights | ||
* @param labels list of labels for the data points | ||
* @return sequence of data points | ||
*/ | ||
def generateIsotonicInput(labels: Double*): Seq[WeightedLabeledPoint] = { | ||
labels.zip(1 to labels.size) | ||
.map(point => labeledPointToWeightedLabeledPoint(LabeledPoint(point._1, Vectors.dense(point._2)))) | ||
} | ||
|
||
/** | ||
* Return an ordered sequence of labeled weighted data points | ||
* @param labels list of labels for the data points | ||
* @param weights list of weights for the data points | ||
* @return sequence of data points | ||
*/ | ||
def generateWeightedIsotonicInput(labels: Seq[Double], weights: Seq[Double]): Seq[WeightedLabeledPoint] = { | ||
labels.zip(1 to labels.size).zip(weights) | ||
.map(point => WeightedLabeledPoint(point._1._1, Vectors.dense(point._1._2), point._2)) | ||
} | ||
} |
Oops, something went wrong.