-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
105 lines (86 loc) · 3.36 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace RBF_PNN
{
class Program
{
static void Main(string[] args)
{
// read the data
string line;
StreamReader dataFile = new StreamReader("E:\\documents\\com\\pasca\\patternrec\\RBF_PNN\\wdbc-normal.data");
List<Vector> data = new List<Vector>();
List<int> classLabel = new List<int>();
while ((line = dataFile.ReadLine()) != null)
{
if (line.IndexOf('?') == -1)
{
string[] dataLine = line.Split(',');
double[] dataLineNum = new double[30];
for (int i = 0; i < 30; i++) dataLineNum[i] = double.Parse(dataLine[i + 2]);
data.Add(new Vector(dataLineNum));
// 1 = Benign, 2 = Malignant
int c = (dataLine[1] == "B") ? 1 : 2;
classLabel.Add(c);
}
}
dataFile.Close();
// K-means for centroid calculation
int k = 2;
KMeans kmeans = new KMeans(k);
for (int i = 0; i < data.Count; i++)
{
kmeans.AddData(data[i]);
}
kmeans.DoClustering(100, 0.001);
// build the RBF
RBF rbf = new RBF(data.Count, 30, k, 1);
for (int i = 0; i < data.Count; i++)
{
rbf.AddTrainingData(classLabel[i], data[i]);
}
// center calculated using K-means
Matrix rbfCenter = new Matrix(k, 30);
for (int i = 0; i < k; i++)
{
rbfCenter.SetRow(i, kmeans.GetCentroid()[i].GetValueArray());
}
rbf.SetCenter(rbfCenter);
rbf.Train();
// testing
List<int> rbfActualClass = classLabel;
List<int> rbfPredictedClass = new List<int>();
for (int i = 0; i < data.Count; i++)
{
rbfPredictedClass.Add(rbf.Simulate(data[i]));
}
Console.WriteLine("RBF:\n");
ConfusionMatrix rbfConfusionMat = new ConfusionMatrix(rbfPredictedClass, rbfActualClass);
rbfConfusionMat.Compute();
rbfConfusionMat.Print();
Console.WriteLine("Accuracy: " + rbfConfusionMat.GetAccuracy());
// build the PNN
PNN pnn = new PNN(2, new int[] { 1, 2 });
for (int i = 0; i < data.Count; i++)
{
pnn.AddTrainingData(classLabel[i], data[i]);
}
pnn.Train();
// testing
List<int> pnnActualClass = classLabel;
List<int> pnnPredictedClass = new List<int>();
for (int i = 0; i < data.Count; i++)
{
pnnPredictedClass.Add(pnn.Simulate(data[i], 1));
}
Console.WriteLine("\n\nPNN:\n");
ConfusionMatrix pnnConfusionMat = new ConfusionMatrix(pnnPredictedClass, pnnActualClass);
pnnConfusionMat.Compute();
pnnConfusionMat.Print();
Console.WriteLine("Accuracy: " + pnnConfusionMat.GetAccuracy());
Console.Read();
}
}
}