3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
using System ;
6
- using System . Collections . Generic ;
7
6
using System . IO ;
8
- using System . Linq ;
9
7
using Microsoft . Data . DataView ;
10
8
using Microsoft . ML ;
11
9
using Microsoft . ML . Auto ;
@@ -19,40 +17,40 @@ static class AutoTrainRegression
19
17
private static string TrainDataPath = $ "{ BaseDatasetsLocation } /taxi-fare-train.csv";
20
18
private static string TestDataPath = $ "{ BaseDatasetsLocation } /taxi-fare-test.csv";
21
19
private static string ModelPath = $ "{ BaseDatasetsLocation } /TaxiFareModel.zip";
22
- private static string LabelColumnName = "fare_amount" ;
20
+ private static string LabelColumn = "fare_amount" ;
23
21
24
22
public static void Run ( )
25
23
{
26
24
MLContext mlContext = new MLContext ( ) ;
27
25
28
- // STEP 1: Common data loading configuration
29
- var columnInference = mlContext . AutoInference ( ) . InferColumns ( TrainDataPath , LabelColumnName ) ;
26
+ // STEP 1: Infer columns
27
+ var columnInference = mlContext . AutoInference ( ) . InferColumns ( TrainDataPath , LabelColumn ) ;
30
28
31
29
// STEP 2: Load data
32
30
TextLoader textLoader = mlContext . Data . CreateTextLoader ( columnInference . TextLoaderArgs ) ;
33
31
IDataView trainDataView = textLoader . Read ( TrainDataPath ) ;
34
32
IDataView testDataView = textLoader . Read ( TestDataPath ) ;
35
33
36
- // STEP 3: Auto featurize, auto train and auto hyperparameter tuning
37
- Console . WriteLine ( $ "Invoking Regression.AutoFit ") ;
38
- var autoFitResults = mlContext . AutoInference ( )
34
+ // STEP 3: Auto featurize, auto train and auto hyperparameter tune
35
+ Console . WriteLine ( $ "Invoking new AutoML regression experiment... ") ;
36
+ var runResults = mlContext . AutoInference ( )
39
37
. CreateRegressionExperiment ( 0 )
40
- . Execute ( trainDataView , LabelColumnName ) ;
38
+ . Execute ( trainDataView , LabelColumn ) ;
41
39
42
- // STEP 4: Compare and print actual value vs predicted value for top 5 rows from validation data
43
- var best = autoFitResults . Best ( ) ;
40
+ // STEP 4: Print metric from best model
41
+ var best = runResults . Best ( ) ;
44
42
Console . WriteLine ( $ "RSquared of best model from validation data: { best . Metrics . RSquared } ") ;
45
43
46
44
// STEP 5: Evaluate test data
47
45
IDataView testDataViewWithBestScore = best . Model . Transform ( testDataView ) ;
48
- var testMetrics = mlContext . Regression . Evaluate ( testDataViewWithBestScore , label : LabelColumnName , DefaultColumnNames . Score ) ;
49
- Console . WriteLine ( $ "RSquared of best model from test data: { best . Metrics . RSquared } ") ;
46
+ var testMetrics = mlContext . Regression . Evaluate ( testDataViewWithBestScore , label : LabelColumn ) ;
47
+ Console . WriteLine ( $ "RSquared of best model on test data: { best . Metrics . RSquared } ") ;
50
48
51
49
// STEP 6: Save the best model for later deployment and inferencing
52
50
using ( var fs = File . Create ( ModelPath ) )
53
51
best . Model . SaveTo ( mlContext , fs ) ;
54
52
55
- Console . WriteLine ( "Press any key to continue.." ) ;
53
+ Console . WriteLine ( "Press any key to continue... " ) ;
56
54
Console . ReadLine ( ) ;
57
55
}
58
56
}
0 commit comments