Changeset 2418
 Timestamp:
 10/07/09 16:19:21 (14 years ago)
 Location:
 trunk/sources
 Files:

 4 edited
Legend:
 Unmodified
 Added
 Removed

trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs
r2416 r2418 159 159 StreamWriter writer = new StreamWriter(s); 160 160 writer.Write("Targetvariable: "); writer.WriteLine(p.targetVariable); 161 writer.Write("LowerPredictionLimit: "); writer.WriteLine(p.LowerPredictionLimit.ToString("r" ));162 writer.Write("UpperPredictionLimit: "); writer.WriteLine(p.UpperPredictionLimit.ToString("r" ));161 writer.Write("LowerPredictionLimit: "); writer.WriteLine(p.LowerPredictionLimit.ToString("r", CultureInfo.InvariantCulture.NumberFormat)); 162 writer.Write("UpperPredictionLimit: "); writer.WriteLine(p.UpperPredictionLimit.ToString("r", CultureInfo.InvariantCulture.NumberFormat)); 163 163 writer.Write("MaxTimeOffset: "); writer.WriteLine(p.MaxTimeOffset.ToString()); 164 164 writer.Write("MinTimeOffset: "); writer.WriteLine(p.MinTimeOffset.ToString()); … … 176 176 } 177 177 178 public static Predictor Import( Stream s) {178 public static Predictor Import(TextReader reader) { 179 179 Predictor p = new Predictor(); 180 StreamReader reader = new StreamReader(s);181 180 string[] targetVariableLine = reader.ReadLine().Split(':'); 182 181 string[] lowerPredictionLimitLine = reader.ReadLine().Split(':'); … … 187 186 188 187 p.targetVariable = targetVariableLine[1].Trim(); 189 p.LowerPredictionLimit = double.Parse(lowerPredictionLimitLine[1] );190 p.UpperPredictionLimit = double.Parse(upperPredictionLimitLine[1] );188 p.LowerPredictionLimit = double.Parse(lowerPredictionLimitLine[1], CultureInfo.InvariantCulture.NumberFormat); 189 p.UpperPredictionLimit = double.Parse(upperPredictionLimitLine[1], CultureInfo.InvariantCulture.NumberFormat); 191 190 p.maxTimeOffset = int.Parse(maxTimeOffsetLine[1]); 192 191 p.minTimeOffset = int.Parse(minTimeOffsetLine[1]); … … 195 194 p.variableNames[inputVariable.Trim()] = i++; 196 195 } 197 p.svmModel = SVMModel.Import( s);196 p.svmModel = SVMModel.Import(reader); 198 197 return p; 199 198 } 
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMModel.cs
r2415 r2418 120 120 StreamWriter writer = new StreamWriter(s); 121 121 writer.WriteLine("RangeTransform:"); 122 writer.Flush(); 122 123 using (MemoryStream memStream = new MemoryStream()) { 123 124 SVM.RangeTransform.Write(memStream, model.RangeTransform); … … 126 127 } 127 128 writer.WriteLine("Model:"); 128 129 writer.Flush(); 129 130 using (MemoryStream memStream = new MemoryStream()) { 130 131 SVM.Model.Write(memStream, model.Model); … … 135 136 } 136 137 137 public static SVMModel Import( Stream s) {138 public static SVMModel Import(TextReader reader) { 138 139 SVMModel model = new SVMModel(); 139 StreamReader reader = new StreamReader(s);140 140 while (reader.ReadLine().Trim() != "RangeTransform:") ; // read until line "RangeTransform"; 141 model.RangeTransform = SVM.RangeTransform.Read(s); 142 141 model.RangeTransform = SVM.RangeTransform.Read(reader); 143 142 // read until "Model:" 144 143 while (reader.ReadLine().Trim() != "Model:") ; 145 model.Model = SVM.Model.Read( s);144 model.Model = SVM.Model.Read(reader); 146 145 return model; 147 146 } 
trunk/sources/LibSVM/Model.cs
r2416 r2418 24 24 using System.Globalization; 25 25 26 namespace SVM 27 { 28 /// <summary> 29 /// Encapsulates an SVM Model. 30 /// </summary> 31 [Serializable] 32 public class Model 33 { 34 private Parameter _parameter; 35 private int _numberOfClasses; 36 private int _supportVectorCount; 37 private Node[][] _supportVectors; 38 private double[][] _supportVectorCoefficients; 39 private double[] _rho; 40 private double[] _pairwiseProbabilityA; 41 private double[] _pairwiseProbabilityB; 42 43 private int[] _classLabels; 44 private int[] _numberOfSVPerClass; 45 46 internal Model() 47 { 26 namespace SVM { 27 /// <summary> 28 /// Encapsulates an SVM Model. 29 /// </summary> 30 [Serializable] 31 public class Model { 32 private Parameter _parameter; 33 private int _numberOfClasses; 34 private int _supportVectorCount; 35 private Node[][] _supportVectors; 36 private double[][] _supportVectorCoefficients; 37 private double[] _rho; 38 private double[] _pairwiseProbabilityA; 39 private double[] _pairwiseProbabilityB; 40 41 private int[] _classLabels; 42 private int[] _numberOfSVPerClass; 43 44 internal Model() { 45 } 46 47 /// <summary> 48 /// Parameter object. 49 /// </summary> 50 public Parameter Parameter { 51 get { 52 return _parameter; 53 } 54 set { 55 _parameter = value; 56 } 57 } 58 59 /// <summary> 60 /// Number of classes in the model. 61 /// </summary> 62 public int NumberOfClasses { 63 get { 64 return _numberOfClasses; 65 } 66 set { 67 _numberOfClasses = value; 68 } 69 } 70 71 /// <summary> 72 /// Total number of support vectors. 73 /// </summary> 74 public int SupportVectorCount { 75 get { 76 return _supportVectorCount; 77 } 78 set { 79 _supportVectorCount = value; 80 } 81 } 82 83 /// <summary> 84 /// The support vectors. 85 /// </summary> 86 public Node[][] SupportVectors { 87 get { 88 return _supportVectors; 89 } 90 set { 91 _supportVectors = value; 92 } 93 } 94 95 /// <summary> 96 /// The coefficients for the support vectors. 97 /// </summary> 98 public double[][] SupportVectorCoefficients { 99 get { 100 return _supportVectorCoefficients; 101 } 102 set { 103 _supportVectorCoefficients = value; 104 } 105 } 106 107 /// <summary> 108 /// Rho values. 109 /// </summary> 110 public double[] Rho { 111 get { 112 return _rho; 113 } 114 set { 115 _rho = value; 116 } 117 } 118 119 /// <summary> 120 /// First pairwise probability. 121 /// </summary> 122 public double[] PairwiseProbabilityA { 123 get { 124 return _pairwiseProbabilityA; 125 } 126 set { 127 _pairwiseProbabilityA = value; 128 } 129 } 130 131 /// <summary> 132 /// Second pairwise probability. 133 /// </summary> 134 public double[] PairwiseProbabilityB { 135 get { 136 return _pairwiseProbabilityB; 137 } 138 set { 139 _pairwiseProbabilityB = value; 140 } 141 } 142 143 // for classification only 144 145 /// <summary> 146 /// Class labels. 147 /// </summary> 148 public int[] ClassLabels { 149 get { 150 return _classLabels; 151 } 152 set { 153 _classLabels = value; 154 } 155 } 156 157 /// <summary> 158 /// Number of support vectors per class. 159 /// </summary> 160 public int[] NumberOfSVPerClass { 161 get { 162 return _numberOfSVPerClass; 163 } 164 set { 165 _numberOfSVPerClass = value; 166 } 167 } 168 169 /// <summary> 170 /// Reads a Model from the provided file. 171 /// </summary> 172 /// <param name="filename">The name of the file containing the Model</param> 173 /// <returns>the Model</returns> 174 public static Model Read(string filename) { 175 FileStream input = File.OpenRead(filename); 176 try { 177 return Read(input); 178 } 179 finally { 180 input.Close(); 181 } 182 } 183 184 public static Model Read(Stream stream) { 185 return Read(new StreamReader(stream)); 186 } 187 188 /// <summary> 189 /// Reads a Model from the provided stream. 190 /// </summary> 191 /// <param name="stream">The stream from which to read the Model.</param> 192 /// <returns>the Model</returns> 193 public static Model Read(TextReader input) { 194 TemporaryCulture.Start(); 195 196 // read parameters 197 198 Model model = new Model(); 199 Parameter param = new Parameter(); 200 model.Parameter = param; 201 model.Rho = null; 202 model.PairwiseProbabilityA = null; 203 model.PairwiseProbabilityB = null; 204 model.ClassLabels = null; 205 model.NumberOfSVPerClass = null; 206 207 bool headerFinished = false; 208 while (!headerFinished) { 209 string line = input.ReadLine(); 210 string cmd, arg; 211 int splitIndex = line.IndexOf(' '); 212 if (splitIndex >= 0) { 213 cmd = line.Substring(0, splitIndex); 214 arg = line.Substring(splitIndex + 1); 215 } else { 216 cmd = line; 217 arg = ""; 48 218 } 49 50 /// <summary> 51 /// Parameter object. 52 /// </summary> 53 public Parameter Parameter 54 { 55 get 219 // arg = arg.ToLower(); (transforms double NaN or Infinity values to incorrect format [gkronber]) 220 221 int i, n; 222 switch (cmd) { 223 case "svm_type": 224 param.SvmType = (SvmType)Enum.Parse(typeof(SvmType), arg.ToUpper()); 225 break; 226 227 case "kernel_type": 228 param.KernelType = (KernelType)Enum.Parse(typeof(KernelType), arg.ToUpper()); 229 break; 230 231 case "degree": 232 param.Degree = int.Parse(arg); 233 break; 234 235 case "gamma": 236 param.Gamma = double.Parse(arg); 237 break; 238 239 case "coef0": 240 param.Coefficient0 = double.Parse(arg); 241 break; 242 243 case "nr_class": 244 model.NumberOfClasses = int.Parse(arg); 245 break; 246 247 case "total_sv": 248 model.SupportVectorCount = int.Parse(arg); 249 break; 250 251 case "rho": 252 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 253 model.Rho = new double[n]; 254 string[] rhoParts = arg.Split(); 255 for (i = 0; i < n; i++) 256 model.Rho[i] = double.Parse(rhoParts[i]); 257 break; 258 259 case "label": 260 n = model.NumberOfClasses; 261 model.ClassLabels = new int[n]; 262 string[] labelParts = arg.Split(); 263 for (i = 0; i < n; i++) 264 model.ClassLabels[i] = int.Parse(labelParts[i]); 265 break; 266 267 case "probA": 268 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 269 model.PairwiseProbabilityA = new double[n]; 270 string[] probAParts = arg.Split(); 271 for (i = 0; i < n; i++) 272 model.PairwiseProbabilityA[i] = double.Parse(probAParts[i]); 273 break; 274 275 case "probB": 276 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 277 model.PairwiseProbabilityB = new double[n]; 278 string[] probBParts = arg.Split(); 279 for (i = 0; i < n; i++) 280 model.PairwiseProbabilityB[i] = double.Parse(probBParts[i]); 281 break; 282 283 case "nr_sv": 284 n = model.NumberOfClasses; 285 model.NumberOfSVPerClass = new int[n]; 286 string[] nrsvParts = arg.Split(); 287 for (i = 0; i < n; i++) 288 model.NumberOfSVPerClass[i] = int.Parse(nrsvParts[i]); 289 break; 290 291 case "SV": 292 headerFinished = true; 293 break; 294 295 default: 296 throw new Exception("Unknown text in model file"); 297 } 298 } 299 300 // read sv_coef and SV 301 302 int m = model.NumberOfClasses  1; 303 int l = model.SupportVectorCount; 304 model.SupportVectorCoefficients = new double[m][]; 305 for (int i = 0; i < m; i++) { 306 model.SupportVectorCoefficients[i] = new double[l]; 307 } 308 model.SupportVectors = new Node[l][]; 309 310 for (int i = 0; i < l; i++) { 311 string[] parts = input.ReadLine().Trim().Split(); 312 313 for (int k = 0; k < m; k++) 314 model.SupportVectorCoefficients[k][i] = double.Parse(parts[k]); 315 int n = parts.Length  m; 316 model.SupportVectors[i] = new Node[n]; 317 for (int j = 0; j < n; j++) { 318 string[] nodeParts = parts[m + j].Split(':'); 319 model.SupportVectors[i][j] = new Node(); 320 model.SupportVectors[i][j].Index = int.Parse(nodeParts[0]); 321 model.SupportVectors[i][j].Value = double.Parse(nodeParts[1]); 322 } 323 } 324 325 TemporaryCulture.Stop(); 326 327 return model; 328 } 329 330 /// <summary> 331 /// Writes a model to the provided filename. This will overwrite any previous data in the file. 332 /// </summary> 333 /// <param name="filename">The desired file</param> 334 /// <param name="model">The Model to write</param> 335 public static void Write(string filename, Model model) { 336 FileStream stream = File.Open(filename, FileMode.Create); 337 try { 338 Write(stream, model); 339 } 340 finally { 341 stream.Close(); 342 } 343 } 344 345 /// <summary> 346 /// Writes a model to the provided stream. 347 /// </summary> 348 /// <param name="stream">The output stream</param> 349 /// <param name="model">The model to write</param> 350 public static void Write(Stream stream, Model model) { 351 TemporaryCulture.Start(); 352 353 StreamWriter output = new StreamWriter(stream); 354 355 Parameter param = model.Parameter; 356 357 output.Write("svm_type " + param.SvmType + Environment.NewLine); 358 output.Write("kernel_type " + param.KernelType + Environment.NewLine); 359 360 if (param.KernelType == KernelType.POLY) 361 output.Write("degree " + param.Degree.ToString("r") + Environment.NewLine); 362 363 if (param.KernelType == KernelType.POLY  param.KernelType == KernelType.RBF  param.KernelType == KernelType.SIGMOID) 364 output.Write("gamma " + param.Gamma.ToString("r") + Environment.NewLine); 365 366 if (param.KernelType == KernelType.POLY  param.KernelType == KernelType.SIGMOID) 367 output.Write("coef0 " + param.Coefficient0.ToString("r") + Environment.NewLine); 368 369 int nr_class = model.NumberOfClasses; 370 int l = model.SupportVectorCount; 371 output.Write("nr_class " + nr_class + Environment.NewLine); 372 output.Write("total_sv " + l + Environment.NewLine); 373 374 { 375 output.Write("rho"); 376 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 377 output.Write(" " + model.Rho[i].ToString("r")); 378 output.Write(Environment.NewLine); 379 } 380 381 if (model.ClassLabels != null) { 382 output.Write("label"); 383 for (int i = 0; i < nr_class; i++) 384 output.Write(" " + model.ClassLabels[i]); 385 output.Write(Environment.NewLine); 386 } 387 388 if (model.PairwiseProbabilityA != null) 389 // regression has probA only 56 390 { 57 return _parameter; 58 } 59 set 60 { 61 _parameter = value; 62 } 391 output.Write("probA"); 392 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 393 output.Write(" " + model.PairwiseProbabilityA[i].ToString("r")); 394 output.Write(Environment.NewLine); 395 } 396 if (model.PairwiseProbabilityB != null) { 397 output.Write("probB"); 398 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 399 output.Write(" " + model.PairwiseProbabilityB[i].ToString("r")); 400 output.Write(Environment.NewLine); 401 } 402 403 if (model.NumberOfSVPerClass != null) { 404 output.Write("nr_sv"); 405 for (int i = 0; i < nr_class; i++) 406 output.Write(" " + model.NumberOfSVPerClass[i].ToString("r")); 407 output.Write(Environment.NewLine); 408 } 409 410 output.WriteLine("SV"); 411 double[][] sv_coef = model.SupportVectorCoefficients; 412 Node[][] SV = model.SupportVectors; 413 414 for (int i = 0; i < l; i++) { 415 for (int j = 0; j < nr_class  1; j++) 416 output.Write(sv_coef[j][i].ToString("r") + " "); 417 418 Node[] p = SV[i]; 419 if (p.Length == 0) { 420 output.WriteLine(); 421 continue; 63 422 } 64 65 /// <summary> 66 /// Number of classes in the model. 67 /// </summary> 68 public int NumberOfClasses 69 { 70 get 71 { 72 return _numberOfClasses; 73 } 74 set 75 { 76 _numberOfClasses = value; 77 } 423 if (param.KernelType == KernelType.PRECOMPUTED) 424 output.Write("0:{0}", (int)p[0].Value); 425 else { 426 output.Write("{0}:{1}", p[0].Index, p[0].Value.ToString("r")); 427 for (int j = 1; j < p.Length; j++) 428 output.Write(" {0}:{1}", p[j].Index, p[j].Value.ToString("r")); 78 429 } 79 80 /// <summary> 81 /// Total number of support vectors. 82 /// </summary> 83 public int SupportVectorCount 84 { 85 get 86 { 87 return _supportVectorCount; 88 } 89 set 90 { 91 _supportVectorCount = value; 92 } 93 } 94 95 /// <summary> 96 /// The support vectors. 97 /// </summary> 98 public Node[][] SupportVectors 99 { 100 get 101 { 102 return _supportVectors; 103 } 104 set 105 { 106 _supportVectors = value; 107 } 108 } 109 110 /// <summary> 111 /// The coefficients for the support vectors. 112 /// </summary> 113 public double[][] SupportVectorCoefficients 114 { 115 get 116 { 117 return _supportVectorCoefficients; 118 } 119 set 120 { 121 _supportVectorCoefficients = value; 122 } 123 } 124 125 /// <summary> 126 /// Rho values. 127 /// </summary> 128 public double[] Rho 129 { 130 get 131 { 132 return _rho; 133 } 134 set 135 { 136 _rho = value; 137 } 138 } 139 140 /// <summary> 141 /// First pairwise probability. 142 /// </summary> 143 public double[] PairwiseProbabilityA 144 { 145 get 146 { 147 return _pairwiseProbabilityA; 148 } 149 set 150 { 151 _pairwiseProbabilityA = value; 152 } 153 } 154 155 /// <summary> 156 /// Second pairwise probability. 157 /// </summary> 158 public double[] PairwiseProbabilityB 159 { 160 get 161 { 162 return _pairwiseProbabilityB; 163 } 164 set 165 { 166 _pairwiseProbabilityB = value; 167 } 168 } 169 170 // for classification only 171 172 /// <summary> 173 /// Class labels. 174 /// </summary> 175 public int[] ClassLabels 176 { 177 get 178 { 179 return _classLabels; 180 } 181 set 182 { 183 _classLabels = value; 184 } 185 } 186 187 /// <summary> 188 /// Number of support vectors per class. 189 /// </summary> 190 public int[] NumberOfSVPerClass 191 { 192 get 193 { 194 return _numberOfSVPerClass; 195 } 196 set 197 { 198 _numberOfSVPerClass = value; 199 } 200 } 201 202 /// <summary> 203 /// Reads a Model from the provided file. 204 /// </summary> 205 /// <param name="filename">The name of the file containing the Model</param> 206 /// <returns>the Model</returns> 207 public static Model Read(string filename) 208 { 209 FileStream input = File.OpenRead(filename); 210 try 211 { 212 return Read(input); 213 } 214 finally 215 { 216 input.Close(); 217 } 218 } 219 220 /// <summary> 221 /// Reads a Model from the provided stream. 222 /// </summary> 223 /// <param name="stream">The stream from which to read the Model.</param> 224 /// <returns>the Model</returns> 225 public static Model Read(Stream stream) 226 { 227 TemporaryCulture.Start(); 228 229 StreamReader input = new StreamReader(stream); 230 231 // read parameters 232 233 Model model = new Model(); 234 Parameter param = new Parameter(); 235 model.Parameter = param; 236 model.Rho = null; 237 model.PairwiseProbabilityA = null; 238 model.PairwiseProbabilityB = null; 239 model.ClassLabels = null; 240 model.NumberOfSVPerClass = null; 241 242 bool headerFinished = false; 243 while (!headerFinished) 244 { 245 string line = input.ReadLine(); 246 string cmd, arg; 247 int splitIndex = line.IndexOf(' '); 248 if (splitIndex >= 0) 249 { 250 cmd = line.Substring(0, splitIndex); 251 arg = line.Substring(splitIndex + 1); 252 } 253 else 254 { 255 cmd = line; 256 arg = ""; 257 } 258 // arg = arg.ToLower(); (transforms double NaN or Infinity values to incorrect format [gkronber]) 259 260 int i,n; 261 switch(cmd){ 262 case "svm_type": 263 param.SvmType = (SvmType)Enum.Parse(typeof(SvmType), arg.ToUpper()); 264 break; 265 266 case "kernel_type": 267 param.KernelType = (KernelType)Enum.Parse(typeof(KernelType), arg.ToUpper()); 268 break; 269 270 case "degree": 271 param.Degree = int.Parse(arg); 272 break; 273 274 case "gamma": 275 param.Gamma = double.Parse(arg); 276 break; 277 278 case "coef0": 279 param.Coefficient0 = double.Parse(arg); 280 break; 281 282 case "nr_class": 283 model.NumberOfClasses = int.Parse(arg); 284 break; 285 286 case "total_sv": 287 model.SupportVectorCount = int.Parse(arg); 288 break; 289 290 case "rho": 291 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 292 model.Rho = new double[n]; 293 string[] rhoParts = arg.Split(); 294 for(i=0; i<n; i++) 295 model.Rho[i] = double.Parse(rhoParts[i]); 296 break; 297 298 case "label": 299 n = model.NumberOfClasses; 300 model.ClassLabels = new int[n]; 301 string[] labelParts = arg.Split(); 302 for (i = 0; i < n; i++) 303 model.ClassLabels[i] = int.Parse(labelParts[i]); 304 break; 305 306 case "probA": 307 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 308 model.PairwiseProbabilityA = new double[n]; 309 string[] probAParts = arg.Split(); 310 for (i = 0; i < n; i++) 311 model.PairwiseProbabilityA[i] = double.Parse(probAParts[i]); 312 break; 313 314 case "probB": 315 n = model.NumberOfClasses * (model.NumberOfClasses  1) / 2; 316 model.PairwiseProbabilityB = new double[n]; 317 string[] probBParts = arg.Split(); 318 for (i = 0; i < n; i++) 319 model.PairwiseProbabilityB[i] = double.Parse(probBParts[i]); 320 break; 321 322 case "nr_sv": 323 n = model.NumberOfClasses; 324 model.NumberOfSVPerClass = new int[n]; 325 string[] nrsvParts = arg.Split(); 326 for (i = 0; i < n; i++) 327 model.NumberOfSVPerClass[i] = int.Parse(nrsvParts[i]); 328 break; 329 330 case "SV": 331 headerFinished = true; 332 break; 333 334 default: 335 throw new Exception("Unknown text in model file"); 336 } 337 } 338 339 // read sv_coef and SV 340 341 int m = model.NumberOfClasses  1; 342 int l = model.SupportVectorCount; 343 model.SupportVectorCoefficients = new double[m][]; 344 for (int i = 0; i < m; i++) 345 { 346 model.SupportVectorCoefficients[i] = new double[l]; 347 } 348 model.SupportVectors = new Node[l][]; 349 350 for (int i = 0; i < l; i++) 351 { 352 string[] parts = input.ReadLine().Trim().Split(); 353 354 for (int k = 0; k < m; k++) 355 model.SupportVectorCoefficients[k][i] = double.Parse(parts[k]); 356 int n = parts.Lengthm; 357 model.SupportVectors[i] = new Node[n]; 358 for (int j = 0; j < n; j++) 359 { 360 string[] nodeParts = parts[m + j].Split(':'); 361 model.SupportVectors[i][j] = new Node(); 362 model.SupportVectors[i][j].Index = int.Parse(nodeParts[0]); 363 model.SupportVectors[i][j].Value = double.Parse(nodeParts[1]); 364 } 365 } 366 367 TemporaryCulture.Stop(); 368 369 return model; 370 } 371 372 /// <summary> 373 /// Writes a model to the provided filename. This will overwrite any previous data in the file. 374 /// </summary> 375 /// <param name="filename">The desired file</param> 376 /// <param name="model">The Model to write</param> 377 public static void Write(string filename, Model model) 378 { 379 FileStream stream = File.Open(filename, FileMode.Create); 380 try 381 { 382 Write(stream, model); 383 } 384 finally 385 { 386 stream.Close(); 387 } 388 } 389 390 /// <summary> 391 /// Writes a model to the provided stream. 392 /// </summary> 393 /// <param name="stream">The output stream</param> 394 /// <param name="model">The model to write</param> 395 public static void Write(Stream stream, Model model) 396 { 397 TemporaryCulture.Start(); 398 399 StreamWriter output = new StreamWriter(stream); 400 401 Parameter param = model.Parameter; 402 403 output.Write("svm_type " + param.SvmType + Environment.NewLine); 404 output.Write("kernel_type " + param.KernelType + Environment.NewLine); 405 406 if (param.KernelType == KernelType.POLY) 407 output.Write("degree " + param.Degree.ToString("r") + Environment.NewLine); 408 409 if (param.KernelType == KernelType.POLY  param.KernelType == KernelType.RBF  param.KernelType == KernelType.SIGMOID) 410 output.Write("gamma " + param.Gamma.ToString("r") + Environment.NewLine); 411 412 if (param.KernelType == KernelType.POLY  param.KernelType == KernelType.SIGMOID) 413 output.Write("coef0 " + param.Coefficient0.ToString("r") + Environment.NewLine); 414 415 int nr_class = model.NumberOfClasses; 416 int l = model.SupportVectorCount; 417 output.Write("nr_class " + nr_class + Environment.NewLine); 418 output.Write("total_sv " + l + Environment.NewLine); 419 420 { 421 output.Write("rho"); 422 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 423 output.Write(" " + model.Rho[i].ToString("r")); 424 output.Write(Environment.NewLine); 425 } 426 427 if (model.ClassLabels != null) 428 { 429 output.Write("label"); 430 for (int i = 0; i < nr_class; i++) 431 output.Write(" " + model.ClassLabels[i]); 432 output.Write(Environment.NewLine); 433 } 434 435 if (model.PairwiseProbabilityA != null) 436 // regression has probA only 437 { 438 output.Write("probA"); 439 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 440 output.Write(" " + model.PairwiseProbabilityA[i].ToString("r")); 441 output.Write(Environment.NewLine); 442 } 443 if (model.PairwiseProbabilityB != null) 444 { 445 output.Write("probB"); 446 for (int i = 0; i < nr_class * (nr_class  1) / 2; i++) 447 output.Write(" " + model.PairwiseProbabilityB[i].ToString("r")); 448 output.Write(Environment.NewLine); 449 } 450 451 if (model.NumberOfSVPerClass != null) 452 { 453 output.Write("nr_sv"); 454 for (int i = 0; i < nr_class; i++) 455 output.Write(" " + model.NumberOfSVPerClass[i].ToString("r")); 456 output.Write(Environment.NewLine); 457 } 458 459 output.Write("SV\n"); 460 double[][] sv_coef = model.SupportVectorCoefficients; 461 Node[][] SV = model.SupportVectors; 462 463 for (int i = 0; i < l; i++) 464 { 465 for (int j = 0; j < nr_class  1; j++) 466 output.Write(sv_coef[j][i].ToString("r") + " "); 467 468 Node[] p = SV[i]; 469 if (p.Length == 0) 470 { 471 output.WriteLine(); 472 continue; 473 } 474 if (param.KernelType == KernelType.PRECOMPUTED) 475 output.Write("0:{0}", (int)p[0].Value); 476 else 477 { 478 output.Write("{0}:{1}", p[0].Index, p[0].Value.ToString("r")); 479 for (int j = 1; j < p.Length; j++) 480 output.Write(" {0}:{1}", p[j].Index, p[j].Value.ToString("r")); 481 } 482 output.WriteLine(); 483 } 484 485 output.Flush(); 486 487 TemporaryCulture.Stop(); 488 } 489 } 430 output.WriteLine(); 431 } 432 433 output.Flush(); 434 435 TemporaryCulture.Stop(); 436 } 437 } 490 438 } 
trunk/sources/LibSVM/RangeTransform.cs
r2416 r2418 199 199 /// <returns>The Range transform</returns> 200 200 public static RangeTransform Read(Stream stream) { 201 return Read(new StreamReader(stream)); 202 } 203 204 public static RangeTransform Read(TextReader input) { 201 205 TemporaryCulture.Start(); 202 206 203 StreamReader input = new StreamReader(stream);204 207 int length = int.Parse(input.ReadLine()); 205 208 double[] inputStart = new double[length];
Note: See TracChangeset
for help on using the changeset viewer.