Skip to content

Commit be90030

Browse files
author
Pete Luferenko
committed
Merged with examples
2 parents 9fd480e + 96661bb commit be90030

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

test/Microsoft.ML.Tests/Scenarios/Api/AspirationalExamples.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class IrisPrediction
1111
public string PredictedLabel;
1212
}
1313

14-
public class IrisData
14+
public class IrisExample
1515
{
1616
public float[] Features;
1717
}
@@ -21,22 +21,27 @@ public void FirstExperienceWithML()
2121
// This is the 'getting started with ML' example, how we see it in our new API.
2222
// It currently doesn't compile, let alone work, but we still can discuss and improve the syntax.
2323

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)));
24+
// Initialize the environment.
25+
using (var env = new TlcEnvironment())
26+
{
27+
// Load the data into the system.
28+
string dataPath = "iris-data.txt";
29+
var data = TextReader.FitAndRead(env, dataPath, c => (Label: c.LoadString(0), Features: c.LoadFloat(1, 4)));
2730

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);
31+
// Convert string label to integer for training.
32+
var preprocess = data.MakeEstimator(row => (Label: row.Label.Dictionarize(), row.Features));
3233

33-
// Train a multiclass linear classifier.
34-
var learner = new StochasticDualCoordinateAscentClassifier();
35-
var classifier = learner.Train(trainingData);
34+
// Create a learner and train it.
35+
var learner = preprocess.MakeEstimator(row => row.Label.SdcaPredict(row.Features, l1Coefficient: 0.1));
36+
var classifier = learner.Fit(preprocess.FitAndTransform(data));
3637

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 });
38+
// Add another transformer that converts the integer prediction into string.
39+
var finalTransformer = classifier.AppendTransformer(row => row.PredictedLabel.KeyToValue());
40+
41+
// Make a prediction engine and predict.
42+
engine = bundle.MakePredictionEngine<IrisExample, IrisPrediction>();
43+
IrisPrediction prediction = engine.Predict(new IrisExample { Features = new[] { 3.3f, 1.6f, 0.2f, 5.1f } });
44+
}
4045
}
4146
}
4247
}

0 commit comments

Comments
 (0)