[1806] | 1 | /*
|
---|
| 2 | * SVM.NET Library
|
---|
| 3 | * Copyright (C) 2008 Matthew Johnson
|
---|
| 4 | *
|
---|
| 5 | * This program is free software: you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | using System;
|
---|
| 22 | using System.IO;
|
---|
[2415] | 23 | using System.Threading;
|
---|
| 24 | using System.Globalization;
|
---|
[1806] | 25 |
|
---|
[2418] | 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 |
|
---|
[2415] | 47 | /// <summary>
|
---|
[2418] | 48 | /// Parameter object.
|
---|
[2415] | 49 | /// </summary>
|
---|
[2418] | 50 | public Parameter Parameter {
|
---|
| 51 | get {
|
---|
| 52 | return _parameter;
|
---|
| 53 | }
|
---|
| 54 | set {
|
---|
| 55 | _parameter = value;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[1806] | 58 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 70 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 82 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 94 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 106 |
|
---|
[2418] | 107 | /// <summary>
|
---|
| 108 | /// Rho values.
|
---|
| 109 | /// </summary>
|
---|
| 110 | public double[] Rho {
|
---|
| 111 | get {
|
---|
| 112 | return _rho;
|
---|
| 113 | }
|
---|
| 114 | set {
|
---|
| 115 | _rho = value;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[1806] | 118 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 130 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 142 |
|
---|
[2418] | 143 | // for classification only
|
---|
[1806] | 144 |
|
---|
[2418] | 145 | /// <summary>
|
---|
| 146 | /// Class labels.
|
---|
| 147 | /// </summary>
|
---|
| 148 | public int[] ClassLabels {
|
---|
| 149 | get {
|
---|
| 150 | return _classLabels;
|
---|
| 151 | }
|
---|
| 152 | set {
|
---|
| 153 | _classLabels = value;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[1806] | 156 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 168 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 183 |
|
---|
[2418] | 184 | public static Model Read(Stream stream) {
|
---|
| 185 | return Read(new StreamReader(stream));
|
---|
| 186 | }
|
---|
[1806] | 187 |
|
---|
[2418] | 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();
|
---|
[1806] | 195 |
|
---|
[2418] | 196 | // read parameters
|
---|
[1806] | 197 |
|
---|
[2418] | 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;
|
---|
[1806] | 206 |
|
---|
[2418] | 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 = "";
|
---|
| 218 | }
|
---|
| 219 | // arg = arg.ToLower(); (transforms double NaN or Infinity values to incorrect format [gkronber])
|
---|
[1806] | 220 |
|
---|
[2418] | 221 | int i, n;
|
---|
| 222 | switch (cmd) {
|
---|
| 223 | case "svm_type":
|
---|
| 224 | param.SvmType = (SvmType)Enum.Parse(typeof(SvmType), arg.ToUpper());
|
---|
| 225 | break;
|
---|
[1806] | 226 |
|
---|
[2418] | 227 | case "kernel_type":
|
---|
| 228 | param.KernelType = (KernelType)Enum.Parse(typeof(KernelType), arg.ToUpper());
|
---|
| 229 | break;
|
---|
[1806] | 230 |
|
---|
[2418] | 231 | case "degree":
|
---|
| 232 | param.Degree = int.Parse(arg);
|
---|
| 233 | break;
|
---|
[1806] | 234 |
|
---|
[2418] | 235 | case "gamma":
|
---|
| 236 | param.Gamma = double.Parse(arg);
|
---|
| 237 | break;
|
---|
[1806] | 238 |
|
---|
[2418] | 239 | case "coef0":
|
---|
| 240 | param.Coefficient0 = double.Parse(arg);
|
---|
| 241 | break;
|
---|
[1806] | 242 |
|
---|
[2418] | 243 | case "nr_class":
|
---|
| 244 | model.NumberOfClasses = int.Parse(arg);
|
---|
| 245 | break;
|
---|
[1806] | 246 |
|
---|
[2418] | 247 | case "total_sv":
|
---|
| 248 | model.SupportVectorCount = int.Parse(arg);
|
---|
| 249 | break;
|
---|
[1806] | 250 |
|
---|
[2418] | 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;
|
---|
[1806] | 258 |
|
---|
[2418] | 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;
|
---|
[1806] | 266 |
|
---|
[2418] | 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;
|
---|
[1806] | 274 |
|
---|
[2418] | 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;
|
---|
[1806] | 282 |
|
---|
[2418] | 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;
|
---|
[1806] | 290 |
|
---|
[2418] | 291 | case "SV":
|
---|
| 292 | headerFinished = true;
|
---|
| 293 | break;
|
---|
[1806] | 294 |
|
---|
[2418] | 295 | default:
|
---|
| 296 | throw new Exception("Unknown text in model file");
|
---|
| 297 | }
|
---|
| 298 | }
|
---|
[1806] | 299 |
|
---|
[2418] | 300 | // read sv_coef and SV
|
---|
[1806] | 301 |
|
---|
[2418] | 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][];
|
---|
[1806] | 309 |
|
---|
[2418] | 310 | for (int i = 0; i < l; i++) {
|
---|
| 311 | string[] parts = input.ReadLine().Trim().Split();
|
---|
[1806] | 312 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 324 |
|
---|
[2418] | 325 | TemporaryCulture.Stop();
|
---|
[1806] | 326 |
|
---|
[2418] | 327 | return model;
|
---|
| 328 | }
|
---|
[2415] | 329 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 344 |
|
---|
[2418] | 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();
|
---|
[1806] | 352 |
|
---|
[2418] | 353 | StreamWriter output = new StreamWriter(stream);
|
---|
[1806] | 354 |
|
---|
[2418] | 355 | Parameter param = model.Parameter;
|
---|
[1806] | 356 |
|
---|
[2418] | 357 | output.Write("svm_type " + param.SvmType + Environment.NewLine);
|
---|
| 358 | output.Write("kernel_type " + param.KernelType + Environment.NewLine);
|
---|
[1806] | 359 |
|
---|
[2418] | 360 | if (param.KernelType == KernelType.POLY)
|
---|
| 361 | output.Write("degree " + param.Degree.ToString("r") + Environment.NewLine);
|
---|
[1806] | 362 |
|
---|
[2418] | 363 | if (param.KernelType == KernelType.POLY || param.KernelType == KernelType.RBF || param.KernelType == KernelType.SIGMOID)
|
---|
| 364 | output.Write("gamma " + param.Gamma.ToString("r") + Environment.NewLine);
|
---|
[1806] | 365 |
|
---|
[2418] | 366 | if (param.KernelType == KernelType.POLY || param.KernelType == KernelType.SIGMOID)
|
---|
| 367 | output.Write("coef0 " + param.Coefficient0.ToString("r") + Environment.NewLine);
|
---|
[1806] | 368 |
|
---|
[2418] | 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);
|
---|
[1806] | 373 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 380 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 387 |
|
---|
[2418] | 388 | if (model.PairwiseProbabilityA != null)
|
---|
| 389 | // regression has probA only
|
---|
[2415] | 390 | {
|
---|
[2418] | 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 | }
|
---|
[2411] | 402 |
|
---|
[2418] | 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 | }
|
---|
[1806] | 409 |
|
---|
[2418] | 410 | output.WriteLine("SV");
|
---|
| 411 | double[][] sv_coef = model.SupportVectorCoefficients;
|
---|
| 412 | Node[][] SV = model.SupportVectors;
|
---|
[1806] | 413 |
|
---|
[2418] | 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") + " ");
|
---|
[1806] | 417 |
|
---|
[2418] | 418 | Node[] p = SV[i];
|
---|
| 419 | if (p.Length == 0) {
|
---|
| 420 | output.WriteLine();
|
---|
| 421 | continue;
|
---|
| 422 | }
|
---|
| 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"));
|
---|
| 429 | }
|
---|
| 430 | output.WriteLine();
|
---|
| 431 | }
|
---|
[1806] | 432 |
|
---|
[2418] | 433 | output.Flush();
|
---|
[2415] | 434 |
|
---|
[2418] | 435 | TemporaryCulture.Stop();
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
[1806] | 438 | } |
---|