This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Program.cs
117 lines (94 loc) · 3.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using SiaNet.Data;
using System;
using System.Linq;
using Deedle;
using SiaNet;
using SiaNet.Layers;
using SiaNet.Regularizers;
using SiaNet.Engine;
using SiaNet.Backend.ArrayFire;
using SiaNet.Events;
namespace BasicClassificationWithTitanicDataset
{
class Program
{
static void Main(string[] args)
{
//Setup Engine
Global.UseEngine(SiaNet.Backend.ArrayFire.SiaNetBackend.Instance, DeviceType.Default);
var train = LoadTrain(); //Load train data
var test = LoadTest(); //Load test data
var model = new Sequential();
model.EpochEnd += Model_EpochEnd;
model.Add(new Dense(128, ActType.ReLU));
model.Add(new Dense(64, ActType.ReLU));
model.Add(new Dense(1, ActType.Sigmoid));
//Compile with Optimizer, Loss and Metric
model.Compile(OptimizerType.Adam, LossType.BinaryCrossEntropy, MetricType.BinaryAccurary);
// Perform training with train and val dataset
model.Train(train, epochs: 100, batchSize: 200);
var prediction = model.Predict(test);
}
private static void Model_EpochEnd(object sender, EpochEndEventArgs e)
{
Console.WriteLine("Epoch: {0}, Loss: {1}, Acc: {2}", e.Epoch, e.Loss, e.Metric);
}
private static DataFrameIter LoadTrain()
{
//Using deedle which is similar to Pandas in python
var frame = Frame.ReadCsv("train.csv", true);
//Preprocess the data by handling missing values, converting string to numbers
frame = PreProcesData(frame);
//Load Deedle frame to Tensor frame
var data = frame.ToArray2D<float>().Cast<float>().ToArray();
DataFrame2D df = new DataFrame2D(frame.ColumnCount);
df.Load(data);
//Split X and Y
var x = df[0, 6];
var y = df[7];
return new DataFrameIter(x, y);
}
private static DataFrame LoadTest()
{
//Using deedle which is similar to Pandas in python
var frame = Frame.ReadCsv("test.csv", true);
//Preprocess the data by handling missing values, converting string to numbers
frame = PreProcesData(frame, true);
//Load Deedle frame to Tensor frame
var data = frame.ToArray2D<float>().Cast<float>().ToArray();
DataFrame2D df = new DataFrame2D(frame.ColumnCount);
df.Load(data);
return df;
}
private static Frame<int, string> PreProcesData(Frame<int, string> frame, bool isTest = false)
{
// Drop some colmuns which will not help in prediction
frame.DropColumn("PassengerId");
frame.DropColumn("Name");
frame.DropColumn("Ticket");
frame.DropColumn("Cabin");
//Fill missing data with nearest values
frame = frame.FillMissing(Direction.Forward);
// Convert male/female to 1/0
frame["Sex"] = frame.GetColumn<string>("Sex").SelectValues<double>((x) => {
return x == "male" ? 1 : 0;
});
// Convert S/C/Q -> 0/1/2
frame["Embarked"] = frame.GetColumn<string>("Embarked").SelectValues<double>((x) => {
if (x == "S")
return 0;
else if (x == "C")
return 1;
else if (x == "Q")
return 2;
return 0;
});
// Convert Survived to 1 or 0
if(!isTest)
frame["Survived"] = frame.GetColumn<bool>("Survived").SelectValues<double>((x) => {
return x ? 1 : 0;
});
return frame;
}
}
}