[1806] | 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;
|
---|
| 21 | using System.Collections.Generic;
|
---|
| 22 | using System.IO;
|
---|
[2415] | 23 | using System.Globalization;
|
---|
| 24 | using System.Threading;
|
---|
[1806] | 25 |
|
---|
| 26 | namespace SVM
|
---|
| 27 | {
|
---|
[2415] | 28 | /// <summary>
|
---|
[1806] | 29 | /// A transform which learns the mean and variance of a sample set and uses these to transform new data
|
---|
| 30 | /// so that it has zero mean and unit variance.
|
---|
[2415] | 31 | /// </summary>
|
---|
[1806] | 32 | public class GaussianTransform : IRangeTransform
|
---|
| 33 | {
|
---|
| 34 | private double[] _means;
|
---|
| 35 | private double[] _stddevs;
|
---|
| 36 |
|
---|
| 37 | /// <summary>
|
---|
[2415] | 38 | /// Determines the Gaussian transform for the provided problem.
|
---|
[1806] | 39 | /// </summary>
|
---|
[2415] | 40 | /// <param name="prob">The Problem to analyze</param>
|
---|
| 41 | /// <returns>The Gaussian transform for the problem</returns>
|
---|
| 42 | public static GaussianTransform Compute(Problem prob)
|
---|
[1806] | 43 | {
|
---|
[2415] | 44 | int[] counts = new int[prob.MaxIndex];
|
---|
| 45 | double[] means = new double[prob.MaxIndex];
|
---|
| 46 | foreach (Node[] sample in prob.X)
|
---|
[1806] | 47 | {
|
---|
| 48 | for (int i = 0; i < sample.Length; i++)
|
---|
| 49 | {
|
---|
[2415] | 50 | means[sample[i].Index-1] += sample[i].Value;
|
---|
| 51 | counts[sample[i].Index-1]++;
|
---|
[1806] | 52 | }
|
---|
| 53 | }
|
---|
[2415] | 54 | for (int i = 0; i < prob.MaxIndex; i++)
|
---|
[1806] | 55 | {
|
---|
| 56 | if (counts[i] == 0)
|
---|
| 57 | counts[i] = 2;
|
---|
[2415] | 58 | means[i] /= counts[i];
|
---|
[1806] | 59 | }
|
---|
| 60 |
|
---|
[2415] | 61 | double[] stddevs = new double[prob.MaxIndex];
|
---|
| 62 | foreach (Node[] sample in prob.X)
|
---|
[1806] | 63 | {
|
---|
| 64 | for (int i = 0; i < sample.Length; i++)
|
---|
| 65 | {
|
---|
[2415] | 66 | double diff = sample[i].Value - means[sample[i].Index - 1];
|
---|
| 67 | stddevs[sample[i].Index - 1] += diff * diff;
|
---|
[1806] | 68 | }
|
---|
| 69 | }
|
---|
[2415] | 70 | for (int i = 0; i < prob.MaxIndex; i++)
|
---|
[1806] | 71 | {
|
---|
[2415] | 72 | if (stddevs[i] == 0)
|
---|
[1806] | 73 | continue;
|
---|
[2415] | 74 | stddevs[i] /= (counts[i] - 1);
|
---|
| 75 | stddevs[i] = Math.Sqrt(stddevs[i]);
|
---|
[1806] | 76 | }
|
---|
[2415] | 77 |
|
---|
| 78 | return new GaussianTransform(means, stddevs);
|
---|
[1806] | 79 | }
|
---|
| 80 |
|
---|
| 81 | /// <summary>
|
---|
[2415] | 82 | /// Constructor.
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <param name="means">Means in each dimension</param>
|
---|
| 85 | /// <param name="stddevs">Standard deviation in each dimension</param>
|
---|
| 86 | public GaussianTransform(double[] means, double[] stddevs)
|
---|
| 87 | {
|
---|
| 88 | _means = means;
|
---|
| 89 | _stddevs = stddevs;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /// <summary>
|
---|
[1806] | 93 | /// Saves the transform to the disk. The samples are not stored, only the
|
---|
| 94 | /// statistics.
|
---|
| 95 | /// </summary>
|
---|
| 96 | /// <param name="stream">The destination stream</param>
|
---|
| 97 | /// <param name="transform">The transform</param>
|
---|
| 98 | public static void Write(Stream stream, GaussianTransform transform)
|
---|
| 99 | {
|
---|
[2415] | 100 | TemporaryCulture.Start();
|
---|
| 101 |
|
---|
[1806] | 102 | StreamWriter output = new StreamWriter(stream);
|
---|
[2415] | 103 | output.WriteLine(transform._means.Length);
|
---|
| 104 | for (int i = 0; i < transform._means.Length; i++)
|
---|
[1806] | 105 | output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]);
|
---|
| 106 | output.Flush();
|
---|
[2415] | 107 |
|
---|
| 108 | TemporaryCulture.Stop();
|
---|
[1806] | 109 | }
|
---|
| 110 |
|
---|
| 111 | /// <summary>
|
---|
| 112 | /// Reads a GaussianTransform from the provided stream.
|
---|
| 113 | /// </summary>
|
---|
| 114 | /// <param name="stream">The source stream</param>
|
---|
| 115 | /// <returns>The transform</returns>
|
---|
| 116 | public static GaussianTransform Read(Stream stream)
|
---|
| 117 | {
|
---|
[2415] | 118 | TemporaryCulture.Start();
|
---|
| 119 |
|
---|
[1806] | 120 | StreamReader input = new StreamReader(stream);
|
---|
[2415] | 121 | int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture);
|
---|
[1806] | 122 | double[] means = new double[length];
|
---|
| 123 | double[] stddevs = new double[length];
|
---|
| 124 | for (int i = 0; i < length; i++)
|
---|
| 125 | {
|
---|
| 126 | string[] parts = input.ReadLine().Split();
|
---|
[2415] | 127 | means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture);
|
---|
| 128 | stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture);
|
---|
[1806] | 129 | }
|
---|
[2415] | 130 |
|
---|
| 131 | TemporaryCulture.Stop();
|
---|
| 132 |
|
---|
| 133 | return new GaussianTransform(means, stddevs);
|
---|
[1806] | 134 | }
|
---|
| 135 |
|
---|
| 136 | /// <summary>
|
---|
| 137 | /// Saves the transform to the disk. The samples are not stored, only the
|
---|
| 138 | /// statistics.
|
---|
| 139 | /// </summary>
|
---|
| 140 | /// <param name="filename">The destination filename</param>
|
---|
| 141 | /// <param name="transform">The transform</param>
|
---|
| 142 | public static void Write(string filename, GaussianTransform transform)
|
---|
| 143 | {
|
---|
| 144 | FileStream output = File.Open(filename, FileMode.Create);
|
---|
| 145 | try
|
---|
| 146 | {
|
---|
| 147 | Write(output, transform);
|
---|
| 148 | }
|
---|
| 149 | finally
|
---|
| 150 | {
|
---|
| 151 | output.Close();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /// <summary>
|
---|
| 156 | /// Reads a GaussianTransform from the provided stream.
|
---|
| 157 | /// </summary>
|
---|
| 158 | /// <param name="filename">The source filename</param>
|
---|
| 159 | /// <returns>The transform</returns>
|
---|
| 160 | public static GaussianTransform Read(string filename)
|
---|
| 161 | {
|
---|
| 162 | FileStream input = File.Open(filename, FileMode.Open);
|
---|
| 163 | try
|
---|
| 164 | {
|
---|
| 165 | return Read(input);
|
---|
| 166 | }
|
---|
| 167 | finally
|
---|
| 168 | {
|
---|
| 169 | input.Close();
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | #region IRangeTransform Members
|
---|
| 174 |
|
---|
| 175 | /// <summary>
|
---|
| 176 | /// Transform the input value using the transform stored for the provided index.
|
---|
| 177 | /// </summary>
|
---|
| 178 | /// <param name="input">Input value</param>
|
---|
| 179 | /// <param name="index">Index of the transform to use</param>
|
---|
| 180 | /// <returns>The transformed value</returns>
|
---|
| 181 | public double Transform(double input, int index)
|
---|
| 182 | {
|
---|
[2415] | 183 | index--;
|
---|
[1806] | 184 | if (_stddevs[index] == 0)
|
---|
| 185 | return 0;
|
---|
| 186 | double diff = input - _means[index];
|
---|
| 187 | diff /= _stddevs[index];
|
---|
| 188 | return diff;
|
---|
| 189 | }
|
---|
| 190 | /// <summary>
|
---|
[2415] | 191 | /// Transforms the input array.
|
---|
[1806] | 192 | /// </summary>
|
---|
| 193 | /// <param name="input">The array to transform</param>
|
---|
| 194 | /// <returns>The transformed array</returns>
|
---|
| 195 | public Node[] Transform(Node[] input)
|
---|
| 196 | {
|
---|
| 197 | Node[] output = new Node[input.Length];
|
---|
| 198 | for (int i = 0; i < output.Length; i++)
|
---|
| 199 | {
|
---|
| 200 | int index = input[i].Index;
|
---|
| 201 | double value = input[i].Value;
|
---|
| 202 | output[i] = new Node(index, Transform(value, index));
|
---|
| 203 | }
|
---|
| 204 | return output;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | #endregion
|
---|
| 208 | }
|
---|
| 209 | }
|
---|