-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
111 lines (82 loc) · 3.07 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using W_KNN;
public class Main
{
string[] entry;
List<List<string>> data = new List<List<string>>();
List<List<string>> testData = new List<List<string>>();
List<List<string>> dummyData = new List<List<string>>();
HashSet<string> classes = new HashSet<string>();
int fold, k;
public Main()
{
Console.WriteLine("Enter fold count & Nearest Neighbour count: ");
fold = Convert.ToInt32(Console.ReadLine());
k = Convert.ToInt32(Console.ReadLine());
arrangeData();
run();
new Predictor(data, testData, k, classes);
}
public void arrangeData()
{
string path = @"C:\Codebase\.NET\W_knn\W_KNN\ecoli.data", line;
StreamReader reader = new StreamReader(path);
while ((line = reader.ReadLine()) != null)
{
line = Regex.Replace(line, @"\s+", " ");
entry = line.Split(null);
classes.Add(entry[8]);
data.Add(entry.ToList());
Array.Clear(entry, 0, entry.Length);
}
}
public void printMe() {
foreach (List<string> s in data)
{
foreach (string j in s)
{
Console.Write(j + " ");
}
Console.WriteLine();
}
Console.WriteLine();
for (int i = 0; i < testData.Count; i++)
{
for (int j = 0; j < testData[i].Count; j++)
{
Console.Write(testData[i][j]);
}
Console.WriteLine();
}
}
public void run() {
Random rnd=new Random();
int ind, foldElem = data.Count/fold, round=1;
double myAccuracy = 0;
for (int i = 0; i < data.Count; i++) {
ind = rnd.Next(1, data.Count - 1);
data.Insert(0,data[ind]);
data.RemoveAt(ind + 1);
}
for (int run = 0; run < fold; run++) {
foreach (List<string> str in data) dummyData.Add(str);
for (int i = run * fold; i < (run * fold) + foldElem; i++) {
testData.Add(dummyData[i]);
dummyData.RemoveAt(i);
}
Predictor p=new Predictor(dummyData, testData, k, classes);
Console.WriteLine("\n"+round++ +":");
p.beginTest();
testData.Clear();
dummyData.Clear();
}
Console.WriteLine("\nNet Accuracy: {0}%", Math.Round(validationMetrices.accuracy/fold,5)*100);
Console.WriteLine("\nNet Precision: {0}%", Math.Round(validationMetrices.precision / fold, 5) * 100);
Console.WriteLine("\nNet Recall: {0}%", Math.Round(validationMetrices.recall / fold, 5) * 100);
Console.WriteLine("\nNet f-measure: {0}%", Math.Round(validationMetrices.f1 / fold, 5) * 100);
}
}