-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update helloworld examples to be simple #351
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think
../
prefixes would work once we deploy to https://docs.transmogrif.aiwere the links broken? how did you test them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The links are broken on the website, but only for that paragraph. I just imitated the style of the links that work in the other paragraphs. How do I link a page that will work when deployed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try running the docs server locally as described here - https://github.com/salesforce/TransmogrifAI/tree/master/docs#docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you're right. We actually have them as
../
elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They work.