Repository navigation
Add a binary logistic sample #2256
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1b0eed0
Add binary logistic sample
jwood803 4b25d3e
Merge branch 'master' into binary-logistic-sample
jwood803 8e80670
Pipeline changes
jwood803 a34532f
Merge branch 'master' into binary-logistic-sample
jwood803 efae1dd
updates
jwood803 433d7e9
Merge branch 'master' into binary-logistic-sample
jwood803 ea5837d
Get sample working correctly
jwood803 ac423e4
Don't change sample to run in program
jwood803 4f8886b
Update class name
jwood803 0bde82d
Merge branch 'master' into binary-logistic-sample
jwood803 4e6eb7d
Add score comments
jwood803 0f749c3
Add sample reference to declaration
jwood803 5bf2e4e
Merge branch 'master' into binary-logistic-sample
jwood803 37edf07
Move example reference
jwood803 9b16dd1
Switch parameters
jwood803 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
87 changes: 87 additions & 0 deletions
87
docs/samples/Microsoft.ML.Samples/Dynamic/LogisticRegression.cs
This file contains hidden or 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,87 @@ | ||
| using System; | ||
| using Microsoft.Data.DataView; | ||
| using Microsoft.ML.Data; | ||
|
|
||
| namespace Microsoft.ML.Samples.Dynamic | ||
| { | ||
| public class LogisticRegressionExample | ||
| { | ||
| public static void LogisticRegression() | ||
| { | ||
| var ml = new MLContext(); | ||
|
|
||
| // Downloading a classification dataset from github.com/dotnet/machinelearning. | ||
| // It will be stored in the same path as the executable | ||
| var dataFilePath = SamplesUtils.DatasetUtils.DownloadAdultDataset(); | ||
|
|
||
| // Data Preview | ||
| // 1. Column: age (numeric) | ||
| // 2. Column: workclass (text/categorical) | ||
| // 3. Column: fnlwgt (numeric) | ||
| // 4. Column: education (text/categorical) | ||
| // 5. Column: education-num (numeric) | ||
| // 6. Column: marital-status (text/categorical) | ||
| // 7. Column: occupation (text/categorical) | ||
| // 8. Column: relationship (text/categorical) | ||
| // 9. Column: ethnicity (text/categorical) | ||
| // 10. Column: sex (text/categorical) | ||
| // 11. Column: capital-gain (numeric) | ||
| // 12. Column: capital-loss (numeric) | ||
| // 13. Column: hours-per-week (numeric) | ||
| // 14. Column: native-country (text/categorical) | ||
| // 15. Column: Column [Label]: IsOver50K (boolean) | ||
|
|
||
| var reader = ml.Data.CreateTextLoader(new TextLoader.Arguments | ||
| { | ||
| Separators = new[] { ',' }, | ||
| HasHeader = true, | ||
| Column = new[] | ||
| { | ||
| new TextLoader.Column("age", DataKind.R4, 0), | ||
| new TextLoader.Column("workclass", DataKind.Text, 1), | ||
| new TextLoader.Column("fnlwgt", DataKind.R4, 2), | ||
| new TextLoader.Column("education", DataKind.Text, 3), | ||
| new TextLoader.Column("education-num", DataKind.R4, 4), | ||
| new TextLoader.Column("marital-status", DataKind.Text, 5), | ||
| new TextLoader.Column("occupation", DataKind.Text, 6), | ||
| new TextLoader.Column("relationship", DataKind.Text, 7), | ||
| new TextLoader.Column("ethnicity", DataKind.Text, 8), | ||
| new TextLoader.Column("sex", DataKind.Text, 9), | ||
| new TextLoader.Column("capital-gain", DataKind.R4, 10), | ||
| new TextLoader.Column("capital-loss", DataKind.R4, 11), | ||
| new TextLoader.Column("hours-per-week", DataKind.R4, 12), | ||
| new TextLoader.Column("native-country", DataKind.Text, 13), | ||
| new TextLoader.Column("Label", DataKind.Bool, 14) | ||
| } | ||
| }); | ||
|
|
||
| IDataView data = reader.Read(dataFilePath); | ||
|
|
||
| var (trainData, testData) = ml.BinaryClassification.TrainTestSplit(data, testFraction: 0.2); | ||
|
|
||
| var pipeline = ml.Transforms.Concatenate("Text", "workclass", "education", "marital-status", | ||
| "relationship", "ethnicity", "sex", "native-country") | ||
| .Append(ml.Transforms.Text.FeaturizeText("TextFeatures", "Text")) | ||
| .Append(ml.Transforms.Concatenate("Features", "TextFeatures", "age", "fnlwgt", | ||
| "education-num", "capital-gain", "capital-loss", "hours-per-week")) | ||
| .Append(ml.BinaryClassification.Trainers.LogisticRegression()); | ||
|
|
||
| var model = pipeline.Fit(trainData); | ||
|
|
||
| var dataWithPredictions = model.Transform(testData); | ||
|
|
||
| var metrics = ml.BinaryClassification.Evaluate(dataWithPredictions); | ||
|
|
||
| Console.WriteLine($"Accuracy: {metrics.Accuracy}"); // 0.80 | ||
| Console.WriteLine($"AUC: {metrics.Auc}"); // 0.64 | ||
| Console.WriteLine($"F1 Score: {metrics.F1Score}"); // 0.39 | ||
|
|
||
| Console.WriteLine($"Negative Precision: {metrics.NegativePrecision}"); // 0.81 | ||
| Console.WriteLine($"Negative Recall: {metrics.NegativeRecall}"); // 0.96 | ||
| Console.WriteLine($"Positive Precision: {metrics.PositivePrecision}"); // 0.68 | ||
| Console.WriteLine($"Positive Recall: {metrics.PositiveRecall}"); // 0.27 | ||
|
|
||
| Console.ReadLine(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Would be nice to put this example next to
LogisticRegressiondeclarationSomething like this:
machinelearning/src/Microsoft.ML.Transforms/Text/TextCatalog.cs
Line 24 in 2f0c475
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.
Hopefully, I put this in the right place. 🙂
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.
Sorry for been cryptic.
I meant this place:
machinelearning/src/Microsoft.ML.StandardLearners/StandardLearnersCatalog.cs
Line 306 in b4c1066
You put it on top of internal class which doesn't show up in documentation.
In reply to: 251782735 [](ancestors = 251782735)
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.
Apologies, I should have noticed the
internalon the class. Should be good now, though.