Skip to content

Commit

Permalink
* Made ProbabilisticClassificationModel into a subclass of Classifica…
Browse files Browse the repository at this point in the history
…tionModel. Also introduced ProbabilisticClassifier.

 * This was to support output column “probabilityCol” in transform().

* SPARK-4942 : ML Transformers should allow output cols to be turned on,off
 * Update validateAndTransformSchema
 * Update transform

* Update based on design review
 * Make prediction API protected, but add output columns
 * Remove training API

* LogisticRegression:
 * Changed output column “score” to “probability” in logreg.
 * I also implemented transform() to avoid repeated computation.  This improves upon the default implementation in ProbabilisticClassificationModel.  However, it’s a lot of code, so I would be fine with removing it.  There is also a question of whether all algorithms should implement a method which would allow the ProbabilisticClassificationModel.transform implementation to avoid repeated computation:
   * protected def raw2prob(rawPredictions: Vector): Vector = // compute probabilities from raw predictions

* trait Params:
 * Changed set() and get() from private[ml] to protected.  This was needed for the example of defining a class from outside of the MLlib namespace.

* VectorUDT: Changed from private[spark] to public.  This is needed for outside users to write their own validateAndTransformSchema() methods using vectors.

* Add example of defining class from outside of the MLlib namespace.
 * Scala
  • Loading branch information
jkbradley committed Feb 5, 2015
1 parent 4e2f711 commit 1c61723
Show file tree
Hide file tree
Showing 20 changed files with 820 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public static void main(String[] args) {

// Make predictions on test documents. cvModel uses the best model found (lrModel).
cvModel.transform(test).registerTempTable("prediction");
DataFrame predictions = jsql.sql("SELECT id, text, score, prediction FROM prediction");
DataFrame predictions = jsql.sql("SELECT id, text, probability, prediction FROM prediction");
for (Row r: predictions.collect()) {
System.out.println("(" + r.get(0) + ", " + r.get(1) + ") --> score=" + r.get(2)
System.out.println("(" + r.get(0) + ", " + r.get(1) + ") --> prob=" + r.get(2)
+ ", prediction=" + r.get(3));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void main(String[] args) {

// One can also combine ParamMaps.
ParamMap paramMap2 = new ParamMap();
paramMap2.put(lr.scoreCol().w("probability")); // Change output column name
paramMap2.put(lr.probabilityCol().w("myProbability")); // Change output column name
ParamMap paramMapCombined = paramMap.$plus$plus(paramMap2);

// Now learn a new model using the paramMapCombined parameters.
Expand All @@ -98,8 +98,8 @@ public static void main(String[] args) {

// Make predictions on test documents using the Transformer.transform() method.
// LogisticRegression.transform will only use the 'features' column.
// Note that model2.transform() outputs a 'probability' column instead of the usual 'score'
// column since we renamed the lr.scoreCol parameter previously.
// Note that model2.transform() outputs a 'myProbability' column instead of the usual
// 'probability' column since we renamed the lr.probabilityCol parameter previously.
model2.transform(test).registerTempTable("results");
DataFrame results =
jsql.sql("SELECT features, label, probability, prediction FROM results");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void main(String[] args) {
model.transform(test).registerTempTable("prediction");
DataFrame predictions = jsql.sql("SELECT id, text, score, prediction FROM prediction");
for (Row r: predictions.collect()) {
System.out.println("(" + r.get(0) + ", " + r.get(1) + ") --> score=" + r.get(2)
System.out.println("(" + r.get(0) + ", " + r.get(1) + ") --> prob=" + r.get(2)
+ ", prediction=" + r.get(3));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.feature.{HashingTF, Tokenizer}
import org.apache.spark.ml.tuning.{ParamGridBuilder, CrossValidator}
import org.apache.spark.mllib.linalg.Vector
import org.apache.spark.sql.{Row, SQLContext}

/**
Expand Down Expand Up @@ -100,10 +101,10 @@ object CrossValidatorExample {

// Make predictions on test documents. cvModel uses the best model found (lrModel).
cvModel.transform(test)
.select("id", "text", "score", "prediction")
.select('id, 'text, 'probability, 'prediction)
.collect()
.foreach { case Row(id: Long, text: String, score: Double, prediction: Double) =>
println("(" + id + ", " + text + ") --> score=" + score + ", prediction=" + prediction)
.foreach { case Row(id: Long, text: String, prob: Vector, prediction: Double) =>
println("(" + id + ", " + text + ") --> prob=" + prob + ", prediction=" + prediction)
}

sc.stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* 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.examples.ml

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.SparkContext._
import org.apache.spark.ml.classification.{Classifier, ClassifierParams, ClassificationModel}
import org.apache.spark.ml.param.{Params, IntParam, ParamMap}
import org.apache.spark.mllib.linalg.{BLAS, Vector, Vectors, VectorUDT}
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.sql.{DataType, SchemaRDD, Row, SQLContext}

/**
* A simple example demonstrating how to write your own learning algorithm using Estimator,
* Transformer, and other abstractions.
* This mimics [[org.apache.spark.ml.classification.LogisticRegression]].
* Run with
* {{{
* bin/run-example ml.DeveloperApiExample
* }}}
*/
object DeveloperApiExample {

def main(args: Array[String]) {
val conf = new SparkConf().setAppName("DeveloperApiExample")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
import sqlContext._

// Prepare training data.
// We use LabeledPoint, which is a case class. Spark SQL can convert RDDs of Java Beans
// into SchemaRDDs, where it uses the bean metadata to infer the schema.
val training = sparkContext.parallelize(Seq(
LabeledPoint(1.0, Vectors.dense(0.0, 1.1, 0.1)),
LabeledPoint(0.0, Vectors.dense(2.0, 1.0, -1.0)),
LabeledPoint(0.0, Vectors.dense(2.0, 1.3, 1.0)),
LabeledPoint(1.0, Vectors.dense(0.0, 1.2, -0.5))))

// Create a LogisticRegression instance. This instance is an Estimator.
val lr = new MyLogisticRegression()
// Print out the parameters, documentation, and any default values.
println("MyLogisticRegression parameters:\n" + lr.explainParams() + "\n")

// We may set parameters using setter methods.
lr.setMaxIter(10)

// Learn a LogisticRegression model. This uses the parameters stored in lr.
val model = lr.fit(training)

// Prepare test data.
val test = sparkContext.parallelize(Seq(
LabeledPoint(1.0, Vectors.dense(-1.0, 1.5, 1.3)),
LabeledPoint(0.0, Vectors.dense(3.0, 2.0, -0.1)),
LabeledPoint(1.0, Vectors.dense(0.0, 2.2, -1.5))))

// Make predictions on test data.
val sumPredictions: Double = model.transform(test)
.select('features, 'label, 'prediction)
.collect()
.map { case Row(features: Vector, label: Double, prediction: Double) =>
prediction
}.sum
assert(sumPredictions == 0.0,
"MyLogisticRegression predicted something other than 0, even though all weights are 0!")
}
}

/**
* Example of defining a parameter trait for a user-defined type of [[Classifier]].
*
* NOTE: This is private since it is an example. In practice, you may not want it to be private.
*/
private trait MyLogisticRegressionParams extends ClassifierParams {

/** param for max number of iterations */
val maxIter: IntParam = new IntParam(this, "maxIter", "max number of iterations")
def getMaxIter: Int = get(maxIter)
}

/**
* Example of defining a type of [[Classifier]].
*
* NOTE: This is private since it is an example. In practice, you may not want it to be private.
*/
private class MyLogisticRegression
extends Classifier[Vector, MyLogisticRegression, MyLogisticRegressionModel]
with MyLogisticRegressionParams {

setMaxIter(100) // Initialize

def setMaxIter(value: Int): this.type = set(maxIter, value)

override def fit(dataset: SchemaRDD, paramMap: ParamMap): MyLogisticRegressionModel = {
// Check schema (types). This allows early failure before running the algorithm.
transformSchema(dataset.schema, paramMap, logging = true)

// Extract columns from data using helper method.
val oldDataset = extractLabeledPoints(dataset, paramMap)

// Combine given parameters with the embedded parameters, where the given paramMap overrides
// any embedded settings.
val map = this.paramMap ++ paramMap

// Do learning to estimate the weight vector.
val numFeatures = oldDataset.take(1)(0).features.size
val weights = Vectors.zeros(numFeatures) // Learning would happen here.

// Create a model to return.
val lrm = new MyLogisticRegressionModel(this, map, weights)

// Copy model params.
// An Estimator stores the parameters for the Model it produces, and this copies any relevant
// parameters to the model.
Params.inheritValues(map, this, lrm)

// Return the learned model.
lrm
}

/**
* Returns the SQL DataType corresponding to the FeaturesType type parameter.
* This is used by [[ClassifierParams.validateAndTransformSchema()]] to check the input data.
*/
override protected def featuresDataType: DataType = new VectorUDT
}

/**
* Example of defining a type of [[ClassificationModel]].
*
* NOTE: This is private since it is an example. In practice, you may not want it to be private.
*/
private class MyLogisticRegressionModel(
override val parent: MyLogisticRegression,
override val fittingParamMap: ParamMap,
val weights: Vector)
extends ClassificationModel[Vector, MyLogisticRegressionModel]
with MyLogisticRegressionParams {

// This uses the default implementation of transform(), which reads column "features" and outputs
// columns "prediction" and "rawPrediction."

// This uses the default implementation of predict(), which chooses the label corresponding to
// the maximum value returned by [[predictRaw()]].

/**
* Raw prediction for each possible label.
* The meaning of a "raw" prediction may vary between algorithms, but it intuitively gives
* a measure of confidence in each possible label (where larger = more confident).
* This internal method is used to implement [[transform()]] and output [[rawPredictionCol]].
*
* @return vector where element i is the raw prediction for label i.
* This raw prediction may be any real number, where a larger value indicates greater
* confidence for that label.
*/
override protected def predictRaw(features: Vector): Vector = {
val margin = BLAS.dot(features, weights)
// There are 2 classes (binary classification), so we return a length-2 vector,
// where index i corresponds to class i (i = 0, 1).
Vectors.dense(-margin, margin)
}

/** Number of classes the label can take. 2 indicates binary classification. */
override val numClasses: Int = 2

/**
* Create a copy of the model.
* The copy is shallow, except for the embedded paramMap, which gets a deep copy.
*
* This is used for the defaul implementation of [[transform()]].
*/
override protected def copy(): MyLogisticRegressionModel = {
val m = new MyLogisticRegressionModel(parent, fittingParamMap, weights)
Params.inheritValues(this.paramMap, this, m)
m
}

/**
* Returns the SQL DataType corresponding to the FeaturesType type parameter.
* This is used by [[ClassifierParams.validateAndTransformSchema()]] to check the input data.
*/
override protected def featuresDataType: DataType = new VectorUDT
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ object SimpleParamsExample {
paramMap.put(lr.regParam -> 0.1, lr.threshold -> 0.55) // Specify multiple Params.

// One can also combine ParamMaps.
val paramMap2 = ParamMap(lr.scoreCol -> "probability") // Change output column name
val paramMap2 = ParamMap(lr.probabilityCol -> "myProbability") // Change output column name
val paramMapCombined = paramMap ++ paramMap2

// Now learn a new model using the paramMapCombined parameters.
// paramMapCombined overrides all parameters set earlier via lr.set* methods.
val model2 = lr.fit(training, paramMapCombined)
println("Model 2 was fit using parameters: " + model2.fittingParamMap)

// Prepare test documents.
val test = sc.parallelize(Seq(
// Prepare test data.
val test = sparkContext.parallelize(Seq(
LabeledPoint(1.0, Vectors.dense(-1.0, 1.5, 1.3)),
LabeledPoint(0.0, Vectors.dense(3.0, 2.0, -0.1)),
LabeledPoint(1.0, Vectors.dense(0.0, 2.2, -1.5))))

// Make predictions on test documents using the Transformer.transform() method.
// Make predictions on test data using the Transformer.transform() method.
// LogisticRegression.transform will only use the 'features' column.
// Note that model2.transform() outputs a 'probability' column instead of the usual 'score'
// column since we renamed the lr.scoreCol parameter previously.
// Note that model2.transform() outputs a 'myProbability' column instead of the usual
// 'probability' column since we renamed the lr.probabilityCol parameter previously.
model2.transform(test)
.select("features", "label", "probability", "prediction")
.select('features, 'label, 'myProbability, 'prediction)
.collect()
.foreach { case Row(features: Vector, label: Double, prob: Double, prediction: Double) =>
println("(" + features + ", " + label + ") -> prob=" + prob + ", prediction=" + prediction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.feature.{HashingTF, Tokenizer}
import org.apache.spark.mllib.linalg.Vector
import org.apache.spark.sql.{Row, SQLContext}

@BeanInfo
Expand Down Expand Up @@ -79,10 +80,10 @@ object SimpleTextClassificationPipeline {

// Make predictions on test documents.
model.transform(test)
.select("id", "text", "score", "prediction")
.select('id, 'text, 'probability, 'prediction)
.collect()
.foreach { case Row(id: Long, text: String, score: Double, prediction: Double) =>
println("(" + id + ", " + text + ") --> score=" + score + ", prediction=" + prediction)
.foreach { case Row(id: Long, text: String, prob: Vector, prediction: Double) =>
println("(" + id + ", " + text + ") --> prob=" + prob + ", prediction=" + prediction)
}

sc.stop()
Expand Down
52 changes: 0 additions & 52 deletions mllib/src/main/scala/org/apache/spark/ml/LabeledPoint.scala

This file was deleted.

Loading

0 comments on commit 1c61723

Please sign in to comment.