Repository navigation
Expand file tree
/
Copy pathGaussianTransform.cs
More file actions
209 lines (190 loc) · 7.06 KB
/
Copy pathGaussianTransform.cs
File metadata and controls
209 lines (190 loc) · 7.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* SVM.NET Library
* Copyright (C) 2008 Matthew Johnson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Threading;
namespace Bigtree.Algorithm.SVM
{
/// <summary>
/// A transform which learns the mean and variance of a sample set and uses these to transform new data
/// so that it has zero mean and unit variance.
/// </summary>
public class GaussianTransform : IRangeTransform
{
private double[] _means;
private double[] _stddevs;
/// <summary>
/// Determines the Gaussian transform for the provided problem.
/// </summary>
/// <param name="prob">The Problem to analyze</param>
/// <returns>The Gaussian transform for the problem</returns>
public static GaussianTransform Compute(Problem prob)
{
int[] counts = new int[prob.MaxIndex];
double[] means = new double[prob.MaxIndex];
foreach (Node[] sample in prob.X)
{
for (int i = 0; i < sample.Length; i++)
{
means[sample[i].Index-1] += sample[i].Value;
counts[sample[i].Index-1]++;
}
}
for (int i = 0; i < prob.MaxIndex; i++)
{
if (counts[i] == 0)
counts[i] = 2;
means[i] /= counts[i];
}
double[] stddevs = new double[prob.MaxIndex];
foreach (Node[] sample in prob.X)
{
for (int i = 0; i < sample.Length; i++)
{
double diff = sample[i].Value - means[sample[i].Index - 1];
stddevs[sample[i].Index - 1] += diff * diff;
}
}
for (int i = 0; i < prob.MaxIndex; i++)
{
if (stddevs[i] == 0)
continue;
stddevs[i] /= (counts[i] - 1);
stddevs[i] = Math.Sqrt(stddevs[i]);
}
return new GaussianTransform(means, stddevs);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="means">Means in each dimension</param>
/// <param name="stddevs">Standard deviation in each dimension</param>
public GaussianTransform(double[] means, double[] stddevs)
{
_means = means;
_stddevs = stddevs;
}
/// <summary>
/// Saves the transform to the disk. The samples are not stored, only the
/// statistics.
/// </summary>
/// <param name="stream">The destination stream</param>
/// <param name="transform">The transform</param>
public static void Write(Stream stream, GaussianTransform transform)
{
TemporaryCulture.Start();
StreamWriter output = new StreamWriter(stream);
output.WriteLine(transform._means.Length);
for (int i = 0; i < transform._means.Length; i++)
output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]);
output.Flush();
TemporaryCulture.Stop();
}
/// <summary>
/// Reads a GaussianTransform from the provided stream.
/// </summary>
/// <param name="stream">The source stream</param>
/// <returns>The transform</returns>
public static GaussianTransform Read(Stream stream)
{
TemporaryCulture.Start();
StreamReader input = new StreamReader(stream);
int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture);
double[] means = new double[length];
double[] stddevs = new double[length];
for (int i = 0; i < length; i++)
{
string[] parts = input.ReadLine().Split();
means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture);
stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture);
}
TemporaryCulture.Stop();
return new GaussianTransform(means, stddevs);
}
/// <summary>
/// Saves the transform to the disk. The samples are not stored, only the
/// statistics.
/// </summary>
/// <param name="filename">The destination filename</param>
/// <param name="transform">The transform</param>
public static void Write(string filename, GaussianTransform transform)
{
FileStream output = File.Open(filename, FileMode.Create);
try
{
Write(output, transform);
}
finally
{
output.Close();
}
}
/// <summary>
/// Reads a GaussianTransform from the provided stream.
/// </summary>
/// <param name="filename">The source filename</param>
/// <returns>The transform</returns>
public static GaussianTransform Read(string filename)
{
FileStream input = File.Open(filename, FileMode.Open);
try
{
return Read(input);
}
finally
{
input.Close();
}
}
#region IRangeTransform Members
/// <summary>
/// Transform the input value using the transform stored for the provided index.
/// </summary>
/// <param name="input">Input value</param>
/// <param name="index">Index of the transform to use</param>
/// <returns>The transformed value</returns>
public double Transform(double input, int index)
{
index--;
if (_stddevs[index] == 0)
return 0;
double diff = input - _means[index];
diff /= _stddevs[index];
return diff;
}
/// <summary>
/// Transforms the input array.
/// </summary>
/// <param name="input">The array to transform</param>
/// <returns>The transformed array</returns>
public Node[] Transform(Node[] input)
{
Node[] output = new Node[input.Length];
for (int i = 0; i < output.Length; i++)
{
int index = input[i].Index;
double value = input[i].Value;
output[i] = new Node(index, Transform(value, index));
}
return output;
}
#endregion
}
}