@@ -13,35 +13,49 @@ public class IrisPrediction
13
13
14
14
public class IrisExample
15
15
{
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 ; }
17
20
}
18
21
19
22
public void FirstExperienceWithML ( )
20
23
{
21
24
// This is the 'getting started with ML' example, how we see it in our new API.
22
25
// It currently doesn't compile, let alone work, but we still can discuss and improve the syntax.
23
26
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
+ } ) ;
45
59
}
46
60
}
47
61
}
0 commit comments