@@ -11,7 +11,7 @@ public class IrisPrediction
11
11
public string PredictedLabel ;
12
12
}
13
13
14
- public class IrisData
14
+ public class IrisExample
15
15
{
16
16
public float [ ] Features ;
17
17
}
@@ -21,22 +21,27 @@ public void FirstExperienceWithML()
21
21
// This is the 'getting started with ML' example, how we see it in our new API.
22
22
// It currently doesn't compile, let alone work, but we still can discuss and improve the syntax.
23
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 ) ) ) ;
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 ) ) ) ;
27
30
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 ) ) ;
32
33
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 ) ) ;
36
37
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
+ }
40
45
}
41
46
}
42
47
}
0 commit comments