-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSVMUtilities.cs
More file actions
115 lines (98 loc) · 4.11 KB
/
Copy pathSVMUtilities.cs
File metadata and controls
115 lines (98 loc) · 4.11 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
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
using Bigtree.Algorithm.SVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bigtree.Algorithm.SVMTest
{
public static class SVMUtilities
{
private const double SCALE = 100;
public const int TRAINING_SEED = 20080524;
public const int TESTING_SEED = 20140407;
public static Problem CreateTwoClassProblem(int count, bool isTraining = true)
{
Problem prob = new Problem();
prob.Count = count;
prob.MaxIndex = 2;
Random rand = new Random(isTraining ? TRAINING_SEED : TESTING_SEED);
// create points on either side of the vertical axis
int positive = count / 2;
List<double> labels = new List<double>();
List<Node[]> data = new List<Node[]>();
for (int i = 0; i < count; i++)
{
double x = rand.NextDouble() * SCALE + 10;
double y = rand.NextDouble() * SCALE - (SCALE * .5);
x = i < positive ? x : -x;
data.Add(new Node[] { new Node(1, x), new Node(2, y) });
labels.Add(i < positive ? 1 : -1);
}
prob.X = data.ToArray();
prob.Y = labels.ToArray();
return prob;
}
public static Problem CreateMulticlassProblem(int numberOfClasses, int count, bool isTraining = true)
{
if (numberOfClasses > 8)
throw new ArgumentException("Number of classes must be < 8");
Problem prob = new Problem();
prob.Count = count;
prob.MaxIndex = 3;
int[] samplesPerClass = new int[numberOfClasses];
double countPerClass = (double)count / numberOfClasses;
double current = countPerClass;
for (int i = 1; i < samplesPerClass.Length; i++)
{
samplesPerClass[i] = (int)current;
current += countPerClass;
samplesPerClass[i - 1] = samplesPerClass[i] - samplesPerClass[i - 1];
}
samplesPerClass[samplesPerClass.Length - 1] = count - samplesPerClass.Last();
int[] xSigns = new int[8] { -1, 1, 1, -1, -1, 1, 1, -1 };
int[] ySigns = new int[8] { 1, 1, -1, -1, 1, 1, -1, -1 };
int[] zSigns = new int[8] { 1, 1, 1, 1, -1, -1, -1, -1 };
Random rand = new Random(isTraining ? TRAINING_SEED : TESTING_SEED);
List<double> labels = new List<double>();
List<Node[]> data = new List<Node[]>();
for (int i = 0; i < numberOfClasses; i++)
{
for (int j = 0; j < samplesPerClass[i]; j++)
{
double x = rand.NextDouble() * SCALE + 10;
double y = rand.NextDouble() * SCALE + 10;
double z = rand.NextDouble() * SCALE + 10;
x *= xSigns[i];
y *= ySigns[i];
z *= zSigns[i];
data.Add(new Node[] { new Node(1, x), new Node(2, y), new Node(3, z) });
labels.Add(i);
}
}
prob.X = data.ToArray();
prob.Y = labels.ToArray();
return prob;
}
public static Problem CreateRegressionProblem(int count, bool isTraining = true)
{
Problem prob = new Problem();
prob.Count = count;
prob.MaxIndex = 2;
Random rand = new Random(isTraining ? TRAINING_SEED : TESTING_SEED);
List<double> labels = new List<double>();
List<Node[]> data = new List<Node[]>();
for (int i = 0; i < count; i++)
{
double y = rand.NextDouble() * 10 - 5;
double z = rand.NextDouble() * 10 - 5;
double x = 2 * y + z;
data.Add(new Node[] { new Node(1, y), new Node(2, z) });
labels.Add(x);
}
prob.X = data.ToArray();
prob.Y = labels.ToArray();
return prob;
}
}
}