Changeset 2415 for trunk/sources/LibSVM/GaussianTransform.cs
- Timestamp:
- 10/07/09 11:58:21 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/LibSVM/GaussianTransform.cs
r1819 r2415 21 21 using System.Collections.Generic; 22 22 using System.IO; 23 using System.Globalization; 24 using System.Threading; 23 25 24 26 namespace SVM 25 27 { 26 /// < remarks>28 /// <summary> 27 29 /// A transform which learns the mean and variance of a sample set and uses these to transform new data 28 30 /// so that it has zero mean and unit variance. 29 /// </ remarks>31 /// </summary> 30 32 public class GaussianTransform : IRangeTransform 31 33 { 32 private List<Node[]> _samples;33 private int _maxIndex;34 35 34 private double[] _means; 36 35 private double[] _stddevs; 37 36 38 37 /// <summary> 38 /// Determines the Gaussian transform for the provided problem. 39 /// </summary> 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) 43 { 44 int[] counts = new int[prob.MaxIndex]; 45 double[] means = new double[prob.MaxIndex]; 46 foreach (Node[] sample in prob.X) 47 { 48 for (int i = 0; i < sample.Length; i++) 49 { 50 means[sample[i].Index-1] += sample[i].Value; 51 counts[sample[i].Index-1]++; 52 } 53 } 54 for (int i = 0; i < prob.MaxIndex; i++) 55 { 56 if (counts[i] == 0) 57 counts[i] = 2; 58 means[i] /= counts[i]; 59 } 60 61 double[] stddevs = new double[prob.MaxIndex]; 62 foreach (Node[] sample in prob.X) 63 { 64 for (int i = 0; i < sample.Length; i++) 65 { 66 double diff = sample[i].Value - means[sample[i].Index - 1]; 67 stddevs[sample[i].Index - 1] += diff * diff; 68 } 69 } 70 for (int i = 0; i < prob.MaxIndex; i++) 71 { 72 if (stddevs[i] == 0) 73 continue; 74 stddevs[i] /= (counts[i] - 1); 75 stddevs[i] = Math.Sqrt(stddevs[i]); 76 } 77 78 return new GaussianTransform(means, stddevs); 79 } 80 81 /// <summary> 39 82 /// Constructor. 40 83 /// </summary> 41 /// <param name="maxIndex">The maximum index of the vectors to be transformed</param> 42 public GaussianTransform(int maxIndex) 43 { 44 _samples = new List<Node[]>(); 45 } 46 private GaussianTransform(double[] means, double[] stddevs, int maxIndex) 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) 47 87 { 48 88 _means = means; 49 89 _stddevs = stddevs; 50 _maxIndex = maxIndex;51 }52 53 /// <summary>54 /// Adds a sample to the data. No computation is performed. The maximum index of the55 /// sample must be less than MaxIndex.56 /// </summary>57 /// <param name="sample">The sample to add</param>58 public void Add(Node[] sample)59 {60 _samples.Add(sample);61 }62 63 /// <summary>64 /// Computes the statistics for the samples which have been obtained so far.65 /// </summary>66 public void ComputeStatistics()67 {68 int[] counts = new int[_maxIndex];69 _means = new double[_maxIndex];70 foreach(Node[] sample in _samples)71 {72 for (int i = 0; i < sample.Length; i++)73 {74 _means[sample[i].Index] += sample[i].Value;75 counts[sample[i].Index]++;76 }77 }78 for (int i = 0; i < _maxIndex; i++)79 {80 if (counts[i] == 0)81 counts[i] = 2;82 _means[i] /= counts[i];83 }84 85 _stddevs = new double[_maxIndex];86 foreach(Node[] sample in _samples)87 {88 for (int i = 0; i < sample.Length; i++)89 {90 double diff = sample[i].Value - _means[sample[i].Index];91 _stddevs[sample[i].Index] += diff * diff;92 }93 }94 for (int i = 0; i < _maxIndex; i++)95 {96 if (_stddevs[i] == 0)97 continue;98 _stddevs[i] /= (counts[i]-1);99 _stddevs[i] = Math.Sqrt(_stddevs[i]);100 }101 90 } 102 91 … … 109 98 public static void Write(Stream stream, GaussianTransform transform) 110 99 { 100 TemporaryCulture.Start(); 101 111 102 StreamWriter output = new StreamWriter(stream); 112 output.WriteLine(transform._m axIndex);113 for (int i = 0; i < transform._m axIndex; i++)103 output.WriteLine(transform._means.Length); 104 for (int i = 0; i < transform._means.Length; i++) 114 105 output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]); 115 106 output.Flush(); 107 108 TemporaryCulture.Stop(); 116 109 } 117 110 … … 123 116 public static GaussianTransform Read(Stream stream) 124 117 { 118 TemporaryCulture.Start(); 119 125 120 StreamReader input = new StreamReader(stream); 126 int length = int.Parse(input.ReadLine() );121 int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture); 127 122 double[] means = new double[length]; 128 123 double[] stddevs = new double[length]; … … 130 125 { 131 126 string[] parts = input.ReadLine().Split(); 132 means[i] = double.Parse(parts[0]); 133 stddevs[i] = double.Parse(parts[1]); 134 } 135 return new GaussianTransform(means, stddevs, length); 127 means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture); 128 stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture); 129 } 130 131 TemporaryCulture.Stop(); 132 133 return new GaussianTransform(means, stddevs); 136 134 } 137 135 … … 177 175 /// <summary> 178 176 /// Transform the input value using the transform stored for the provided index. 179 /// <see cref="ComputeStatistics"/> must be called first, or the transform must180 /// have been read from the disk.181 177 /// </summary> 182 178 /// <param name="input">Input value</param> … … 185 181 public double Transform(double input, int index) 186 182 { 183 index--; 187 184 if (_stddevs[index] == 0) 188 185 return 0; … … 192 189 } 193 190 /// <summary> 194 /// Transforms the input array. <see cref="ComputeStatistics"/> must be called 195 /// first, or the transform must have been read from the disk. 191 /// Transforms the input array. 196 192 /// </summary> 197 193 /// <param name="input">The array to transform</param>
Note: See TracChangeset
for help on using the changeset viewer.