-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
99 lines (59 loc) · 4.03 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
using Accord.Statistics.Kernels;
using liver_disease_prediction.dataModels;
using liver_disease_prediction.MachineLearningModels;
using liver_disease_prediction.utility;
using System.Collections.Generic;
using System;
internal class Program
{
private static void Main(string[] args)
{
// ---------------------------------------DATA PREPARATION-----------------------------------------------------
List<LiverPatientRecord> records = DataUtility.LoadDataFromCsv("indian_liver_patient.csv");
(List<LiverPatientRecord> trainSet, List<LiverPatientRecord> testSet) = DataUtility.SplitData(records);
List<List<LiverPatientRecord>> folds = DataUtility.SplitDataIntoFolds(trainSet);
// ---------------------------------------LOGISTIC REGRESSION-----------------------------------------------------
Console.WriteLine("\n---------LOGISTIC REGRESSION---------\n");
LogisticRegressionModel logisticRegressionModel = new LogisticRegressionModel();
Dictionary<string, double[]> logRegParameterRanges = new Dictionary<string, double[]>
{
{ "Regularization", new double[] {1e-1, 1e-4, 1e-7} },
{ "Intercept", new double[] { 0.0, 1.0, 2.0 } }
};
(double bestRegularization, double bestIntercept, double[] logRegMetrics) = logisticRegressionModel.CrossValidation(folds, logRegParameterRanges);
logisticRegressionModel.Train(trainSet, bestRegularization, bestIntercept);
int[] logRegPredictions = logisticRegressionModel.Predict(testSet);
(double accuracy, double precision, double recall, double f1Score) = MachineLearningModel.ComputeMetrics(testSet, logRegPredictions);
Console.WriteLine("\nTest set Metrics for best parameters:\n");
Console.WriteLine($"Accuracy: {accuracy} , Precision: {precision}");
Console.WriteLine($"Recall: {recall}, F1 score: {f1Score}");
// ---------------------------------------DECISION TREE-----------------------------------------------------
Console.WriteLine("\n---------DECISION TREE---------\n");
DecisionTreeModel treeModel = new DecisionTreeModel();
Dictionary<string, double[]> treeParameterRanges = new Dictionary<string, double[]>
{
{ "Join", new double[] { 1.0, 5.0, 10.0, 15.0} },
{ "MaxHeight", new double[] { 10.0, 20.0, 40.0 } }
};
(double bestJoin, double bestMaxHeight, double[] treeMetrics) = treeModel.CrossValidation(folds, treeParameterRanges);
treeModel.Train(trainSet, bestJoin, bestMaxHeight);
int[] treePredictions = treeModel.Predict(testSet);
(double treeaccuracy, double treeprecision, double treerecall, double treef1Score) = MachineLearningModel.ComputeMetrics(testSet, treePredictions);
Console.WriteLine("\nTest set Metrics for best parameters:\n");
Console.WriteLine($"Accuracy: {treeaccuracy} , Precision: {treeprecision}");
Console.WriteLine($"Recall: {treerecall}, F1 score: {treef1Score}");
// ---------------------------------------SUPPORT VECTOR MACHINES-----------------------------------------------------
Console.WriteLine("\n---------SUPPORT VECTOR MACHINES---------\n");
SVMModel svm = new SVMModel();
IKernel[] kernels = new IKernel[] { new Gaussian(), new Linear(), new ChiSquare()};
double[] complexities = new double[] {1e-10, 1e-7, 1e-4};
(IKernel bestKernel, double bestComplexity, double[] svmMetrics) = svm.CrossValidation(folds, kernels, complexities);
svm.Train(trainSet, bestKernel, bestComplexity);
int[] svmPredictions = svm.Predict(testSet);
(double svmaccuracy, double svmprecision, double svmrecall, double svmf1Score) = MachineLearningModel.ComputeMetrics(testSet, svmPredictions);
Console.WriteLine("\nTest set Metrics for best parameters:\n");
Console.WriteLine($"Accuracy: {svmaccuracy} , Precision: {svmprecision}");
Console.WriteLine($"Recall: {svmrecall}, F1 score: {svmf1Score}");
}
}