[2645] | 1 | /*
|
---|
| 2 | * SVM.NET Library
|
---|
| 3 | * Copyright (C) 2008 Matthew Johnson
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | using System;
|
---|
[4068] | 21 | using System.Globalization;
|
---|
[2645] | 22 | using System.IO;
|
---|
| 23 |
|
---|
[4068] | 24 | namespace SVM {
|
---|
| 25 | /// <summary>
|
---|
| 26 | /// A transform which learns the mean and variance of a sample set and uses these to transform new data
|
---|
| 27 | /// so that it has zero mean and unit variance.
|
---|
| 28 | /// </summary>
|
---|
| 29 | public class GaussianTransform : IRangeTransform {
|
---|
| 30 | private double[] _means;
|
---|
| 31 | private double[] _stddevs;
|
---|
| 32 |
|
---|
[2645] | 33 | /// <summary>
|
---|
[4068] | 34 | /// Determines the Gaussian transform for the provided problem.
|
---|
[2645] | 35 | /// </summary>
|
---|
[4068] | 36 | /// <param name="prob">The Problem to analyze</param>
|
---|
| 37 | /// <returns>The Gaussian transform for the problem</returns>
|
---|
| 38 | public static GaussianTransform Compute(Problem prob) {
|
---|
| 39 | int[] counts = new int[prob.MaxIndex];
|
---|
| 40 | double[] means = new double[prob.MaxIndex];
|
---|
| 41 | foreach (Node[] sample in prob.X) {
|
---|
| 42 | for (int i = 0; i < sample.Length; i++) {
|
---|
| 43 | means[sample[i].Index - 1] += sample[i].Value;
|
---|
| 44 | counts[sample[i].Index - 1]++;
|
---|
[2645] | 45 | }
|
---|
[4068] | 46 | }
|
---|
| 47 | for (int i = 0; i < prob.MaxIndex; i++) {
|
---|
| 48 | if (counts[i] == 0)
|
---|
| 49 | counts[i] = 2;
|
---|
| 50 | means[i] /= counts[i];
|
---|
| 51 | }
|
---|
[2645] | 52 |
|
---|
[4068] | 53 | double[] stddevs = new double[prob.MaxIndex];
|
---|
| 54 | foreach (Node[] sample in prob.X) {
|
---|
| 55 | for (int i = 0; i < sample.Length; i++) {
|
---|
| 56 | double diff = sample[i].Value - means[sample[i].Index - 1];
|
---|
| 57 | stddevs[sample[i].Index - 1] += diff * diff;
|
---|
[2645] | 58 | }
|
---|
[4068] | 59 | }
|
---|
| 60 | for (int i = 0; i < prob.MaxIndex; i++) {
|
---|
| 61 | if (stddevs[i] == 0)
|
---|
| 62 | continue;
|
---|
| 63 | stddevs[i] /= (counts[i] - 1);
|
---|
| 64 | stddevs[i] = Math.Sqrt(stddevs[i]);
|
---|
| 65 | }
|
---|
[2645] | 66 |
|
---|
[4068] | 67 | return new GaussianTransform(means, stddevs);
|
---|
| 68 | }
|
---|
[2645] | 69 |
|
---|
[4068] | 70 | /// <summary>
|
---|
| 71 | /// Constructor.
|
---|
| 72 | /// </summary>
|
---|
| 73 | /// <param name="means">Means in each dimension</param>
|
---|
| 74 | /// <param name="stddevs">Standard deviation in each dimension</param>
|
---|
| 75 | public GaussianTransform(double[] means, double[] stddevs) {
|
---|
| 76 | _means = means;
|
---|
| 77 | _stddevs = stddevs;
|
---|
| 78 | }
|
---|
[2645] | 79 |
|
---|
[4068] | 80 | /// <summary>
|
---|
| 81 | /// Saves the transform to the disk. The samples are not stored, only the
|
---|
| 82 | /// statistics.
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <param name="stream">The destination stream</param>
|
---|
| 85 | /// <param name="transform">The transform</param>
|
---|
| 86 | public static void Write(Stream stream, GaussianTransform transform) {
|
---|
| 87 | TemporaryCulture.Start();
|
---|
[2645] | 88 |
|
---|
[4068] | 89 | StreamWriter output = new StreamWriter(stream);
|
---|
| 90 | output.WriteLine(transform._means.Length);
|
---|
| 91 | for (int i = 0; i < transform._means.Length; i++)
|
---|
| 92 | output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]);
|
---|
| 93 | output.Flush();
|
---|
[2645] | 94 |
|
---|
[4068] | 95 | TemporaryCulture.Stop();
|
---|
| 96 | }
|
---|
[2645] | 97 |
|
---|
[4068] | 98 | /// <summary>
|
---|
| 99 | /// Reads a GaussianTransform from the provided stream.
|
---|
| 100 | /// </summary>
|
---|
| 101 | /// <param name="stream">The source stream</param>
|
---|
| 102 | /// <returns>The transform</returns>
|
---|
| 103 | public static GaussianTransform Read(Stream stream) {
|
---|
| 104 | TemporaryCulture.Start();
|
---|
[2645] | 105 |
|
---|
[4068] | 106 | StreamReader input = new StreamReader(stream);
|
---|
| 107 | int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture);
|
---|
| 108 | double[] means = new double[length];
|
---|
| 109 | double[] stddevs = new double[length];
|
---|
| 110 | for (int i = 0; i < length; i++) {
|
---|
| 111 | string[] parts = input.ReadLine().Split();
|
---|
| 112 | means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture);
|
---|
| 113 | stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture);
|
---|
| 114 | }
|
---|
[2645] | 115 |
|
---|
[4068] | 116 | TemporaryCulture.Stop();
|
---|
[2645] | 117 |
|
---|
[4068] | 118 | return new GaussianTransform(means, stddevs);
|
---|
| 119 | }
|
---|
[2645] | 120 |
|
---|
[4068] | 121 | /// <summary>
|
---|
| 122 | /// Saves the transform to the disk. The samples are not stored, only the
|
---|
| 123 | /// statistics.
|
---|
| 124 | /// </summary>
|
---|
| 125 | /// <param name="filename">The destination filename</param>
|
---|
| 126 | /// <param name="transform">The transform</param>
|
---|
| 127 | public static void Write(string filename, GaussianTransform transform) {
|
---|
| 128 | FileStream output = File.Open(filename, FileMode.Create);
|
---|
| 129 | try {
|
---|
| 130 | Write(output, transform);
|
---|
| 131 | }
|
---|
| 132 | finally {
|
---|
| 133 | output.Close();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[2645] | 136 |
|
---|
[4068] | 137 | /// <summary>
|
---|
| 138 | /// Reads a GaussianTransform from the provided stream.
|
---|
| 139 | /// </summary>
|
---|
| 140 | /// <param name="filename">The source filename</param>
|
---|
| 141 | /// <returns>The transform</returns>
|
---|
| 142 | public static GaussianTransform Read(string filename) {
|
---|
| 143 | FileStream input = File.Open(filename, FileMode.Open);
|
---|
| 144 | try {
|
---|
| 145 | return Read(input);
|
---|
| 146 | }
|
---|
| 147 | finally {
|
---|
| 148 | input.Close();
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[2645] | 151 |
|
---|
[4068] | 152 | #region IRangeTransform Members
|
---|
| 153 |
|
---|
| 154 | /// <summary>
|
---|
| 155 | /// Transform the input value using the transform stored for the provided index.
|
---|
| 156 | /// </summary>
|
---|
| 157 | /// <param name="input">Input value</param>
|
---|
| 158 | /// <param name="index">Index of the transform to use</param>
|
---|
| 159 | /// <returns>The transformed value</returns>
|
---|
| 160 | public double Transform(double input, int index) {
|
---|
| 161 | index--;
|
---|
| 162 | if (_stddevs[index] == 0)
|
---|
| 163 | return 0;
|
---|
| 164 | double diff = input - _means[index];
|
---|
| 165 | diff /= _stddevs[index];
|
---|
| 166 | return diff;
|
---|
[2645] | 167 | }
|
---|
[4068] | 168 | /// <summary>
|
---|
| 169 | /// Transforms the input array.
|
---|
| 170 | /// </summary>
|
---|
| 171 | /// <param name="input">The array to transform</param>
|
---|
| 172 | /// <returns>The transformed array</returns>
|
---|
| 173 | public Node[] Transform(Node[] input) {
|
---|
| 174 | Node[] output = new Node[input.Length];
|
---|
| 175 | for (int i = 0; i < output.Length; i++) {
|
---|
| 176 | int index = input[i].Index;
|
---|
| 177 | double value = input[i].Value;
|
---|
| 178 | output[i] = new Node(index, Transform(value, index));
|
---|
| 179 | }
|
---|
| 180 | return output;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | #endregion
|
---|
| 184 | }
|
---|
[2645] | 185 | }
|
---|