Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/GaussianTransform.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/GaussianTransform.cs
r2645 r4068 19 19 20 20 using System; 21 using System. Collections.Generic;21 using System.Globalization; 22 22 using System.IO; 23 using System.Globalization;24 using System.Threading;25 23 26 namespace SVM 27 { 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 28 33 /// <summary> 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. 34 /// Determines the Gaussian transform for the provided problem. 31 35 /// </summary> 32 public class GaussianTransform : IRangeTransform 33 { 34 private double[] _means; 35 private double[] _stddevs; 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]++; 45 } 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 } 36 52 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 } 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; 58 } 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 } 60 66 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 } 67 return new GaussianTransform(means, stddevs); 68 } 77 69 78 return new GaussianTransform(means, stddevs); 79 } 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 } 80 79 81 /// <summary> 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 } 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(); 91 88 92 /// <summary> 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 { 100 TemporaryCulture.Start(); 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(); 101 94 102 StreamWriter output = new StreamWriter(stream); 103 output.WriteLine(transform._means.Length); 104 for (int i = 0; i < transform._means.Length; i++) 105 output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]); 106 output.Flush(); 95 TemporaryCulture.Stop(); 96 } 107 97 108 TemporaryCulture.Stop(); 109 } 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(); 110 105 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 { 118 TemporaryCulture.Start(); 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 } 119 115 120 StreamReader input = new StreamReader(stream); 121 int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture); 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(); 127 means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture); 128 stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture); 129 } 116 TemporaryCulture.Stop(); 130 117 131 TemporaryCulture.Stop(); 118 return new GaussianTransform(means, stddevs); 119 } 132 120 133 return new GaussianTransform(means, stddevs); 134 } 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 } 135 136 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 } 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 } 154 151 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 } 152 #region IRangeTransform Members 172 153 173 #region IRangeTransform Members 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; 167 } 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 } 174 182 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 { 183 index--; 184 if (_stddevs[index] == 0) 185 return 0; 186 double diff = input - _means[index]; 187 diff /= _stddevs[index]; 188 return diff; 189 } 190 /// <summary> 191 /// Transforms the input array. 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 } 183 #endregion 184 } 209 185 }
Note: See TracChangeset
for help on using the changeset viewer.