-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathProgram.cs
53 lines (41 loc) · 1.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Initialize MLContext
using Microsoft.ML;
using Microsoft.ML.AutoML;
using Microsoft.ML.Data;
using static Microsoft.ML.DataOperationsCatalog;
// Initialize MLContext
MLContext ctx = new MLContext();
// Define data path
var dataPath = Path.GetFullPath(@"..\..\..\..\Data\taxi-fare-train.csv");
// Infer column information
ColumnInferenceResults columnInference =
ctx.Auto().InferColumns(dataPath, labelColumnName: "fare_amount", groupColumns: false);
// Create text loader
TextLoader loader = ctx.Data.CreateTextLoader(columnInference.TextLoaderOptions);
// Load data into IDataView
IDataView data = loader.Load(dataPath);
// Split into train (80%), validation (20%) sets
TrainTestData trainValidationData = ctx.Data.TrainTestSplit(data, testFraction: 0.2);
//Define pipeline
SweepablePipeline pipeline =
ctx.Auto().Featurizer(data, columnInformation: columnInference.ColumnInformation)
.Append(ctx.Auto().Regression(labelColumnName: columnInference.ColumnInformation.LabelColumnName));
// Create AutoML experiment
AutoMLExperiment experiment = ctx.Auto().CreateExperiment();
// Configure experiment
experiment
.SetPipeline(pipeline)
.SetRegressionMetric(RegressionMetric.RSquared, labelColumn: columnInference.ColumnInformation.LabelColumnName)
.SetTrainingTimeInSeconds(60)
.SetDataset(trainValidationData);
// Log experiment trials
ctx.Log += (_, e) => {
if (e.Source.Equals("AutoMLExperiment"))
{
Console.WriteLine(e.RawMessage);
}
};
// Run experiment
TrialResult experimentResults = await experiment.RunAsync();
// Get best model
var model = experimentResults.Model;