-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
72 lines (59 loc) · 2.34 KB
/
Program.cs
File metadata and controls
72 lines (59 loc) · 2.34 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.ML;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Runtime.Learners;
using Microsoft.ML.Transforms.Conversions;
using System;
namespace IrisML
{
public class IrisData
{
public float SepalLength;
public float SepalWidth;
public float PetalLength;
public float PetalWidth;
public string Label;
}
public class IrisPrediction
{
[ColumnName("PredictedLabel")]
public string PredictedLabels;
}
class Program
{
const string FILE_DATA_PATH = "irisdata.txt";
static void Main(string[] args)
{
var context = new MLContext();
var reader = context.Data.TextReader(new TextLoader.Arguments()
{
Separator = ",",
HasHeader = false,
Column = new[]
{
new TextLoader.Column("SepalLength", DataKind.R4, 0),
new TextLoader.Column("SepalWidth", DataKind.R4, 1),
new TextLoader.Column("PetalLength", DataKind.R4, 2),
new TextLoader.Column("PetalWidth", DataKind.R4, 3),
new TextLoader.Column("Label", DataKind.Text, 4)
}
});
IDataView trainingDataView = reader.Read(new MultiFileSource(FILE_DATA_PATH));
var pipeline = context.Transforms.Conversion.MapValueToKey("Label")
.Append(context.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
.Append(context.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: "Label", featureColumn: "Features"))
.Append(context.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
var model = pipeline.Fit(trainingDataView);
var prediction = model.MakePredictionFunction<IrisData, IrisPrediction>(context).Predict(
new IrisData()
{
SepalLength = 3.3f,
SepalWidth = 1.6f,
PetalLength = 0.2f,
PetalWidth = 5.1f,
});
Console.WriteLine($"Predicted flower type is: {prediction.PredictedLabels}");
Console.ReadKey();
}
}
}