-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update helloworld examples to be simple (#351)
- Loading branch information
1 parent
ecc7f22
commit e6cc43f
Showing
8 changed files
with
935 additions
and
113 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
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 |
---|---|---|
@@ -1,72 +1,78 @@ | ||
# Iris MultiClass Classification | ||
|
||
The following code illustrates how TransmogrifAI can be used to do classify multiple classes over the Iris dataset. | ||
The code for this example can be found [here](https://github.com/salesforce/TransmogrifAI/tree/master/helloworld/src/main/scala/com/salesforce/hw/iris), and the data over [here](https://github.com/salesforce/op/tree/master/helloworld/src/main/resources/IrisDataset). | ||
The following code illustrates how TransmogrifAI can be used to do classify multiple classes over the Iris dataset. This example is very similar to the Titanic Binary Classification example, so you should look over that example first if you have not already. | ||
The code for this example can be found [here](https://github.com/salesforce/TransmogrifAI/tree/master/helloworld/src/main/scala/com/salesforce/hw/OpIrisSimple.scala), and the data over [here](https://github.com/salesforce/op/tree/master/helloworld/src/main/resources/IrisDataset/iris.csv). | ||
|
||
**Data Schema** | ||
|
||
```scala | ||
case class Iris | ||
( | ||
id: Int, | ||
sepalLength: Double, | ||
sepalWidth: Double, | ||
petalLength: Double, | ||
petalWidth: Double, | ||
irisClass: String | ||
) | ||
``` | ||
|
||
**Define Features** | ||
|
||
**Define features** | ||
```scala | ||
val id = FeatureBuilder.Integral[Iris].extract(_.getID.toIntegral).asPredictor | ||
val sepalLength = FeatureBuilder.Real[Iris].extract(_.getSepalLength.toReal).asPredictor | ||
val sepalWidth = FeatureBuilder.Real[Iris].extract(_.getSepalWidth.toReal).asPredictor | ||
val petalLength = FeatureBuilder.Real[Iris].extract(_.getPetalLength.toReal).asPredictor | ||
val petalWidth = FeatureBuilder.Real[Iris].extract(_.getPetalWidth.toReal).asPredictor | ||
val irisClass = FeatureBuilder.Text[Iris].extract(_.getClass$.toText).asResponse | ||
|
||
``` | ||
|
||
**Feature Engineering** | ||
|
||
```scala | ||
val labels = irisClass.indexed() | ||
val features = Seq(sepalLength, sepalWidth, petalLength, petalWidth).transmogrify() | ||
val label = irisClass.indexed() | ||
val checkedFeatures = label.sanityCheck(features, removeBadFeatures = true) | ||
``` | ||
|
||
**Modeling & Evaluation** | ||
|
||
In MultiClass Classification, we use the ```MultiClassificationModelSelector``` to select the model we want to run on, which is Logistic Regression in this case. You can find more information on model selection [here](../developer-guide#modelselector). | ||
|
||
```scala | ||
val pred = MultiClassificationModelSelector | ||
.withCrossValidation(splitter = Some(DataCutter(reserveTestFraction = 0.2, seed = randomSeed)), seed = randomSeed) | ||
.setInput(labels, features).getOutput() | ||
|
||
private val evaluator = Evaluators.MultiClassification.f1() | ||
.setLabelCol(labels) | ||
.setPredictionCol(pred) | ||
|
||
private val wf = new OpWorkflow().setResultFeatures(pred, labels) | ||
|
||
def runner(opParams: OpParams): OpWorkflowRunner = | ||
new OpWorkflowRunner( | ||
workflow = wf, | ||
trainingReader = irisReader, | ||
scoringReader = irisReader, | ||
evaluationReader = Option(irisReader), | ||
evaluator = Option(evaluator), | ||
featureToComputeUpTo = Option(features) | ||
) | ||
val prediction = MultiClassificationModelSelector | ||
.withTrainValidationSplit( | ||
modelTypesToUse = Seq(OpLogisticRegression)) | ||
.setInput(label, checkedFeatures).getOutput() | ||
|
||
val evaluator = Evaluators.MultiClassification() | ||
.setLabelCol(label) | ||
.setPredictionCol(prediction) | ||
|
||
val workflow = new OpWorkflow().setResultFeatures(prediction, label).setReader(dataReader) | ||
|
||
val model = workflow.train() | ||
``` | ||
You can run the code using the following commands for train, score and evaluate: | ||
|
||
**Results** | ||
|
||
We can still find the contributions of each feature for the model, but in MultiClass Classification, ```ModelInsights``` has a contribution of each feature to the prediction of each class. This code takes the max of all of these contributions as the overall contribution. | ||
|
||
```scala | ||
val modelInsights = model.modelInsights(prediction) | ||
val modelFeatures = modelInsights.features.flatMap( feature => feature.derivedFeatures) | ||
val featureContributions = modelFeatures.map( feature => (feature.derivedFeatureName, | ||
feature.contribution.map( contribution => math.abs(contribution)) | ||
.foldLeft(0.0) { (max, contribution) => math.max(max, contribution)})) | ||
val sortedContributions = featureContributions.sortBy( contribution => -contribution._2) | ||
|
||
val (scores, metrics) = model.scoreAndEvaluate(evaluator = evaluator) | ||
``` | ||
|
||
You can run the code using the following command: | ||
```bash | ||
cd helloworld | ||
./gradlew compileTestScala installDist | ||
``` | ||
**Train** | ||
```bash | ||
./gradlew -q sparkSubmit -Dmain=com.salesforce.hw.iris.OpIris -Dargs="\ | ||
--run-type=train \ | ||
--model-location=/tmp/iris-model \ | ||
--read-location Iris=`pwd`/src/main/resources/IrisDataset/iris.data" | ||
``` | ||
**Score** | ||
```bash | ||
./gradlew -q sparkSubmit -Dmain=com.salesforce.hw.iris.OpIris -Dargs="\ | ||
--run-type=score \ | ||
--model-location=/tmp/iris-model \ | ||
--read-location Iris=`pwd`/src/main/resources/IrisDataset/bezdekIris.data \ | ||
--write-location=/tmp/iris-scores" | ||
``` | ||
**Evaluate** | ||
```bash | ||
./gradlew -q sparkSubmit -Dmain=com.salesforce.hw.iris.OpIris -Dargs="\ | ||
--run-type=evaluate \ | ||
--model-location=/tmp/iris-model \ | ||
--metrics-location=/tmp/iris-metrics \ | ||
--read-location Iris=`pwd`/src/main/resources/IrisDataset/bezdekIris.data \ | ||
--write-location=/tmp/iris-eval" | ||
./gradlew -q sparkSubmit -Dmain=com.salesforce.hw.OpIrisSimple -Dargs="\ | ||
`pwd`/src/main/resources/IrisDataset/iris.csv" | ||
``` |
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
Oops, something went wrong.