Changeset 2415 for trunk/sources/LibSVM/RangeTransform.cs
- Timestamp:
- 10/07/09 11:58:21 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/LibSVM/RangeTransform.cs
r1819 r2415 20 20 using System; 21 21 using System.IO; 22 using System.Threading; 23 using System.Globalization; 22 24 23 25 namespace SVM 24 26 { 25 /// < remarks>27 /// <summary> 26 28 /// Class which encapsulates a range transformation. 27 /// </ remarks>29 /// </summary> 28 30 public class RangeTransform : IRangeTransform 29 31 { 32 /// <summary> 33 /// Default lower bound for scaling (-1). 34 /// </summary> 35 public const int DEFAULT_LOWER_BOUND = -1; 36 /// <summary> 37 /// Default upper bound for scaling (1). 38 /// </summary> 39 public const int DEFAULT_UPPER_BOUND = 1; 40 41 /// <summary> 42 /// Determines the Range transform for the provided problem. Uses the default lower and upper bounds. 43 /// </summary> 44 /// <param name="prob">The Problem to analyze</param> 45 /// <returns>The Range transform for the problem</returns> 46 public static RangeTransform Compute(Problem prob) 47 { 48 return Compute(prob, DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND); 49 } 50 /// <summary> 51 /// Determines the Range transform for the provided problem. 52 /// </summary> 53 /// <param name="prob">The Problem to analyze</param> 54 /// <param name="lowerBound">The lower bound for scaling</param> 55 /// <param name="upperBound">The upper bound for scaling</param> 56 /// <returns>The Range transform for the problem</returns> 57 public static RangeTransform Compute(Problem prob, double lowerBound, double upperBound) 58 { 59 double[] minVals = new double[prob.MaxIndex]; 60 double[] maxVals = new double[prob.MaxIndex]; 61 for (int i = 0; i < prob.MaxIndex; i++) 62 { 63 minVals[i] = double.MaxValue; 64 maxVals[i] = double.MinValue; 65 } 66 for (int i = 0; i < prob.Count; i++) 67 { 68 for (int j = 0; j < prob.X[i].Length; j++) 69 { 70 int index = prob.X[i][j].Index - 1; 71 double value = prob.X[i][j].Value; 72 minVals[index] = Math.Min(minVals[index], value); 73 maxVals[index] = Math.Max(maxVals[index], value); 74 } 75 } 76 for (int i = 0; i < prob.MaxIndex; i++) 77 { 78 if (minVals[i] == double.MaxValue || maxVals[i] == double.MinValue) 79 { 80 minVals[i] = 0; 81 maxVals[i] = 0; 82 } 83 } 84 return new RangeTransform(minVals, maxVals, lowerBound, upperBound); 85 } 86 30 87 private double[] _inputStart; 31 88 private double[] _inputScale; … … 72 129 { 73 130 Node[] output = new Node[input.Length]; 74 for (int i = 0; i < _length; i++)131 for (int i = 0; i < output.Length; i++) 75 132 { 76 133 int index = input[i].Index; … … 104 161 public static void Write(Stream stream, RangeTransform r) 105 162 { 163 TemporaryCulture.Start(); 164 106 165 StreamWriter output = new StreamWriter(stream); 107 166 output.WriteLine(r._length); … … 116 175 output.WriteLine("{0} {1}", r._outputStart, r._outputScale); 117 176 output.Flush(); 177 178 TemporaryCulture.Stop(); 118 179 } 119 180 … … 161 222 public static RangeTransform Read(Stream stream) 162 223 { 224 TemporaryCulture.Start(); 225 163 226 StreamReader input = new StreamReader(stream); 164 227 int length = int.Parse(input.ReadLine()); … … 174 237 double outputStart = double.Parse(parts[0]); 175 238 double outputScale = double.Parse(parts[1]); 239 240 TemporaryCulture.Stop(); 241 176 242 return new RangeTransform(inputStart, inputScale, outputStart, outputScale, length); 177 243 }
Note: See TracChangeset
for help on using the changeset viewer.