-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
71 lines (60 loc) · 2.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Microsoft.ML;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.StaticPipe;
using System;
using System.IO;
using System.Linq;
namespace ml_iris
{
class Program
{
public static string trainingFile = @"Data/iris_train.csv";
public static string TestFile = @"Data/iris_test.csv";
static void Main(string[] args)
{
Console.WriteLine("***** Welcome *****\n-Checking Files:");
Console.WriteLine($" - {trainingFile} : {File.Exists(trainingFile)}");
Console.WriteLine($" - {TestFile} : {File.Exists(TestFile)}");
if (File.Exists(trainingFile) && File.Exists(TestFile))
{
Console.WriteLine("Learning .....");
var mlContext = new MLContext();
var reader = mlContext.Data.TextReader(new TextLoader.Arguments()
{
Separator = ",",
HasHeader = true,
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 trainingData = reader.Read(new MultiFileSource(trainingFile));
var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
.Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
.Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumn: "Label", featureColumn: "Features"))
.Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));
var model = pipeline.Fit(trainingData);
var predictionFunction = model.MakePredictionFunction<IrisData, IrisPrediction>(mlContext);
Console.WriteLine("Test Data Results ....\n");
//Load the Test Data from CSV file
var testData = File.ReadLines(TestFile).Select(line => new IrisData(line)).ToList();
//Shuffel the List -- Not needed Just for Fun
testData = testData.OrderBy(a => Guid.NewGuid()).ToList();
foreach (var iris in testData)
{
Console.WriteLine($"Predicted : {predictionFunction.Predict(iris).PredictedLabels} \nActual : {iris.type}\n=================================");
}
Console.WriteLine("Completed .....");
}
else
{
Console.WriteLine("Error Missing Required File(s) !");
}
Console.ReadKey();
}
}
}