|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace Microsoft.ML.Tests.Scenarios.Api |
| 6 | +{ |
| 7 | + public class AspirationalExamples |
| 8 | + { |
| 9 | + public class IrisPrediction |
| 10 | + { |
| 11 | + public string PredictedLabel; |
| 12 | + } |
| 13 | + |
| 14 | + public class IrisData |
| 15 | + { |
| 16 | + public float[] Features; |
| 17 | + } |
| 18 | + |
| 19 | + public void FirstExperienceWithML() |
| 20 | + { |
| 21 | + // This is the 'getting started with ML' example, how we see it in our new API. |
| 22 | + // It currently doesn't compile, let alone work, but we still can discuss and improve the syntax. |
| 23 | + |
| 24 | + // Load the data into the system. |
| 25 | + string dataPath = "iris-data.txt"; |
| 26 | + var data = TextReader.ReadFile(dataPath, c => (Label: c.LoadString(0), Features: c.LoadFloat(1, 4))); |
| 27 | + |
| 28 | + // Assign numeric values to text in the "Label" column, because only |
| 29 | + // numbers can be processed during model training. |
| 30 | + var transformer = data.MakeTransformer(row => (Label: row.Label.Dictionarize(), row.Features)); |
| 31 | + var trainingData = transformer.Transform(data); |
| 32 | + |
| 33 | + // Train a multiclass linear classifier. |
| 34 | + var learner = new StochasticDualCoordinateAscentClassifier(); |
| 35 | + var classifier = learner.Train(trainingData); |
| 36 | + |
| 37 | + // Obtain some predictions. |
| 38 | + var predictionEngine = new PredictionEngine<float[], string>(classifier, inputColumn: "Features", outputColumn: "PredictedLabel"); |
| 39 | + string prediction = predictionEngine.Predict(new[] { 3.3f, 1.6f, 0.2f, 5.1f }); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments