Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Problem.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/Problem.cs
r2645 r4068 19 19 20 20 using System; 21 using System.Collections.Generic; 21 22 using System.IO; 22 using System.Collections.Generic;23 using System.Threading;24 using System.Globalization;25 23 26 namespace SVM 27 { 24 namespace SVM { 25 /// <summary> 26 /// Encapsulates a problem, or set of vectors which must be classified. 27 /// </summary> 28 [Serializable] 29 public class Problem { 30 private int _count; 31 private double[] _Y; 32 private Node[][] _X; 33 private int _maxIndex; 34 28 35 /// <summary> 29 /// Encapsulates a problem, or set of vectors which must be classified.36 /// Constructor. 30 37 /// </summary> 31 [Serializable] 32 public class Problem 33 { 34 private int _count; 35 private double[] _Y; 36 private Node[][] _X; 37 private int _maxIndex; 38 /// <param name="count">Number of vectors</param> 39 /// <param name="y">The class labels</param> 40 /// <param name="x">Vector data.</param> 41 /// <param name="maxIndex">Maximum index for a vector</param> 42 public Problem(int count, double[] y, Node[][] x, int maxIndex) { 43 _count = count; 44 _Y = y; 45 _X = x; 46 _maxIndex = maxIndex; 47 } 48 /// <summary> 49 /// Empty Constructor. Nothing is initialized. 50 /// </summary> 51 public Problem() { 52 } 53 /// <summary> 54 /// Number of vectors. 55 /// </summary> 56 public int Count { 57 get { 58 return _count; 59 } 60 set { 61 _count = value; 62 } 63 } 64 /// <summary> 65 /// Class labels. 66 /// </summary> 67 public double[] Y { 68 get { 69 return _Y; 70 } 71 set { 72 _Y = value; 73 } 74 } 75 /// <summary> 76 /// Vector data. 77 /// </summary> 78 public Node[][] X { 79 get { 80 return _X; 81 } 82 set { 83 _X = value; 84 } 85 } 86 /// <summary> 87 /// Maximum index for a vector. 88 /// </summary> 89 public int MaxIndex { 90 get { 91 return _maxIndex; 92 } 93 set { 94 _maxIndex = value; 95 } 96 } 38 97 39 /// <summary> 40 /// Constructor. 41 /// </summary> 42 /// <param name="count">Number of vectors</param> 43 /// <param name="y">The class labels</param> 44 /// <param name="x">Vector data.</param> 45 /// <param name="maxIndex">Maximum index for a vector</param> 46 public Problem(int count, double[] y, Node[][] x, int maxIndex) 47 { 48 _count = count; 49 _Y = y; 50 _X = x; 51 _maxIndex = maxIndex; 98 /// <summary> 99 /// Reads a problem from a stream. 100 /// </summary> 101 /// <param name="stream">Stream to read from</param> 102 /// <returns>The problem</returns> 103 public static Problem Read(Stream stream) { 104 TemporaryCulture.Start(); 105 106 StreamReader input = new StreamReader(stream); 107 List<double> vy = new List<double>(); 108 List<Node[]> vx = new List<Node[]>(); 109 int max_index = 0; 110 111 while (input.Peek() > -1) { 112 string[] parts = input.ReadLine().Trim().Split(); 113 114 vy.Add(double.Parse(parts[0])); 115 int m = parts.Length - 1; 116 Node[] x = new Node[m]; 117 for (int j = 0; j < m; j++) { 118 x[j] = new Node(); 119 string[] nodeParts = parts[j + 1].Split(':'); 120 x[j].Index = int.Parse(nodeParts[0]); 121 x[j].Value = double.Parse(nodeParts[1]); 52 122 } 53 /// <summary> 54 /// Empty Constructor. Nothing is initialized. 55 /// </summary> 56 public Problem() 57 { 58 } 59 /// <summary> 60 /// Number of vectors. 61 /// </summary> 62 public int Count 63 { 64 get 65 { 66 return _count; 67 } 68 set 69 { 70 _count = value; 71 } 72 } 73 /// <summary> 74 /// Class labels. 75 /// </summary> 76 public double[] Y 77 { 78 get 79 { 80 return _Y; 81 } 82 set 83 { 84 _Y = value; 85 } 86 } 87 /// <summary> 88 /// Vector data. 89 /// </summary> 90 public Node[][] X 91 { 92 get 93 { 94 return _X; 95 } 96 set 97 { 98 _X = value; 99 } 100 } 101 /// <summary> 102 /// Maximum index for a vector. 103 /// </summary> 104 public int MaxIndex 105 { 106 get 107 { 108 return _maxIndex; 109 } 110 set 111 { 112 _maxIndex = value; 113 } 114 } 123 if (m > 0) 124 max_index = Math.Max(max_index, x[m - 1].Index); 125 vx.Add(x); 126 } 115 127 116 /// <summary> 117 /// Reads a problem from a stream. 118 /// </summary> 119 /// <param name="stream">Stream to read from</param> 120 /// <returns>The problem</returns> 121 public static Problem Read(Stream stream) 122 { 123 TemporaryCulture.Start(); 128 TemporaryCulture.Stop(); 124 129 125 StreamReader input = new StreamReader(stream); 126 List<double> vy = new List<double>(); 127 List<Node[]> vx = new List<Node[]>(); 128 int max_index = 0; 130 return new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index); 131 } 129 132 130 while (input.Peek() > -1) 131 { 132 string[] parts = input.ReadLine().Trim().Split(); 133 /// <summary> 134 /// Writes a problem to a stream. 135 /// </summary> 136 /// <param name="stream">The stream to write the problem to.</param> 137 /// <param name="problem">The problem to write.</param> 138 public static void Write(Stream stream, Problem problem) { 139 TemporaryCulture.Start(); 133 140 134 vy.Add(double.Parse(parts[0])); 135 int m = parts.Length - 1; 136 Node[] x = new Node[m]; 137 for (int j = 0; j < m; j++) 138 { 139 x[j] = new Node(); 140 string[] nodeParts = parts[j + 1].Split(':'); 141 x[j].Index = int.Parse(nodeParts[0]); 142 x[j].Value = double.Parse(nodeParts[1]); 143 } 144 if (m > 0) 145 max_index = Math.Max(max_index, x[m - 1].Index); 146 vx.Add(x); 147 } 141 StreamWriter output = new StreamWriter(stream); 142 for (int i = 0; i < problem.Count; i++) { 143 output.Write(problem.Y[i]); 144 for (int j = 0; j < problem.X[i].Length; j++) 145 output.Write(" {0}:{1}", problem.X[i][j].Index, problem.X[i][j].Value); 146 output.WriteLine(); 147 } 148 output.Flush(); 148 149 149 TemporaryCulture.Stop(); 150 TemporaryCulture.Stop(); 151 } 150 152 151 return new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index); 152 } 153 /// <summary> 154 /// Reads a Problem from a file. 155 /// </summary> 156 /// <param name="filename">The file to read from.</param> 157 /// <returns>the Probem</returns> 158 public static Problem Read(string filename) { 159 FileStream input = File.OpenRead(filename); 160 try { 161 return Read(input); 162 } 163 finally { 164 input.Close(); 165 } 166 } 153 167 154 /// <summary> 155 /// Writes a problem to a stream. 156 /// </summary> 157 /// <param name="stream">The stream to write the problem to.</param> 158 /// <param name="problem">The problem to write.</param> 159 public static void Write(Stream stream, Problem problem) 160 { 161 TemporaryCulture.Start(); 162 163 StreamWriter output = new StreamWriter(stream); 164 for (int i = 0; i < problem.Count; i++) 165 { 166 output.Write(problem.Y[i]); 167 for (int j = 0; j < problem.X[i].Length; j++) 168 output.Write(" {0}:{1}", problem.X[i][j].Index, problem.X[i][j].Value); 169 output.WriteLine(); 170 } 171 output.Flush(); 172 173 TemporaryCulture.Stop(); 174 } 175 176 /// <summary> 177 /// Reads a Problem from a file. 178 /// </summary> 179 /// <param name="filename">The file to read from.</param> 180 /// <returns>the Probem</returns> 181 public static Problem Read(string filename) 182 { 183 FileStream input = File.OpenRead(filename); 184 try 185 { 186 return Read(input); 187 } 188 finally 189 { 190 input.Close(); 191 } 192 } 193 194 /// <summary> 195 /// Writes a problem to a file. This will overwrite any previous data in the file. 196 /// </summary> 197 /// <param name="filename">The file to write to</param> 198 /// <param name="problem">The problem to write</param> 199 public static void Write(string filename, Problem problem) 200 { 201 FileStream output = File.Open(filename, FileMode.Create); 202 try 203 { 204 Write(output, problem); 205 } 206 finally 207 { 208 output.Close(); 209 } 210 } 168 /// <summary> 169 /// Writes a problem to a file. This will overwrite any previous data in the file. 170 /// </summary> 171 /// <param name="filename">The file to write to</param> 172 /// <param name="problem">The problem to write</param> 173 public static void Write(string filename, Problem problem) { 174 FileStream output = File.Open(filename, FileMode.Create); 175 try { 176 Write(output, problem); 177 } 178 finally { 179 output.Close(); 180 } 211 181 } 182 } 212 183 }
Note: See TracChangeset
for help on using the changeset viewer.