-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (50 loc) · 2.55 KB
/
Copy pathProgram.cs
File metadata and controls
64 lines (50 loc) · 2.55 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
using Microsoft.ML;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.Transforms;
using System;
using System.Linq;
namespace BinaryClassification
{
class Program
{
static void Main(string[] args)
{
var context = new MLContext();
var textLoader = context.Data.TextReader(new TextLoader.Arguments()
{
Separator = ",",
HasHeader = true,
Column = new[]
{
new TextLoader.Column("PassengerId", DataKind.R4, 0),
new TextLoader.Column("Label", DataKind.Bool, 1),
new TextLoader.Column("Pclass", DataKind.R4, 2),
new TextLoader.Column("Name", DataKind.Text, 3),
new TextLoader.Column("Sex", DataKind.Text, 4),
new TextLoader.Column("Age", DataKind.R4, 5),
new TextLoader.Column("SibSp", DataKind.R4, 6),
new TextLoader.Column("Parch", DataKind.R4, 7),
new TextLoader.Column("Ticket", DataKind.Text, 8),
new TextLoader.Column("Fare", DataKind.R4, 9),
new TextLoader.Column("Cabin", DataKind.Text, 10),
new TextLoader.Column("Embarked", DataKind.Text, 11)
}
});
IDataView data = textLoader.Read("titanic.csv");
var (trainData, testData) = context.BinaryClassification.TrainTestSplit(data, testFraction: 0.2);
var pipeline = context.Transforms.Concatenate("Text", "Name", "Sex", "Embarked")
.Append(context.Transforms.Text.FeaturizeText("Text", "TextFeatures"))
.Append(context.Transforms.Concatenate("Features", "TextFeatures", "Pclass", "Age", "Fare", "SibSp", "Parch"))
.Append(context.BinaryClassification.Trainers.LogisticRegression("Label", "Features"));
Console.WriteLine("Cross validating...");
var crossValidateResults = context.BinaryClassification.CrossValidate(testData, pipeline);
var averageAuc = crossValidateResults.Average(i => i.metrics.Auc);
Console.WriteLine($"Average AUC - {averageAuc}");
var model = pipeline.Fit(trainData);
var predictionFunction = model.MakePredictionFunction<TitanicData, TitanicPrediction>(context);
var prediction = predictionFunction.Predict(new TitanicData { Sex = "F" });
Console.WriteLine($"Prediction - {prediction.Prediction}");
Console.ReadLine();
}
}
}