Skip to content

Commit 1e90463

Browse files
author
Pete Luferenko
committed
Updated for PR comments
1 parent 96661bb commit 1e90463

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,49 @@ public class IrisPrediction
1313

1414
public class IrisExample
1515
{
16-
public float[] Features;
16+
public float SepalWidth { get; set; }
17+
public float SepalLength { get; set; }
18+
public float PetalWidth { get; set; }
19+
public float PetalLength { get; set; }
1720
}
1821

1922
public void FirstExperienceWithML()
2023
{
2124
// This is the 'getting started with ML' example, how we see it in our new API.
2225
// It currently doesn't compile, let alone work, but we still can discuss and improve the syntax.
2326

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)));
30-
31-
// Convert string label to integer for training.
32-
var preprocess = data.MakeEstimator(row => (Label: row.Label.Dictionarize(), row.Features));
33-
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));
37-
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-
}
27+
// Load the data into the system.
28+
string dataPath = "iris-data.txt";
29+
var data = TextReader.FitAndRead(env, dataPath, row => (
30+
Label: row.ReadString(0),
31+
SepalWidth: row.ReadFloat(1),
32+
SepalLength: row.ReadFloat(2),
33+
PetalWidth: row.ReadFloat(3),
34+
PetalLength: row.ReadFloat(4)));
35+
36+
37+
var preprocess = data.Schema.MakeEstimator(row => (
38+
// Convert string label to key.
39+
Label: row.Label.DictionarizeLabel(),
40+
// Concatenate all features into a vector.
41+
Features: row.SepalWidth.ConcatWith(row.SepalLength, row.PetalWidth, row.PetalLength)));
42+
43+
// Create a learner and train it.
44+
var learner = preprocess.AppendEstimator(row => row.Label.SdcaPredict(row.Features, l1Coefficient: 0.1));
45+
var classifier = learner.Fit(data);
46+
47+
// Add another transformer that converts the integer prediction into string.
48+
var finalTransformer = classifier.AppendTransformer(row => row.PredictedLabel.KeyToValue());
49+
50+
// Make a prediction engine and predict.
51+
engine = finalTransformer.MakePredictionEngine<IrisExample, IrisPrediction>();
52+
IrisPrediction prediction = engine.Predict(new IrisExample
53+
{
54+
SepalWidth = 3.3f,
55+
SepalLength = 1.6f,
56+
PetalWidth = 0.2f,
57+
PetalLength = 5.1f
58+
});
4559
}
4660
}
4761
}

0 commit comments

Comments
 (0)