Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.ExtLibs/HeuristicLab.LibSVM/1.6.3/LibSVM-1.6.3/Problem.cs @ 6409

Last change on this file since 6409 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.0 KB
Line 
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
20using System;
21using System.Collections.Generic;
22using System.IO;
23
24namespace 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
35    /// <summary>
36    /// Constructor.
37    /// </summary>
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    }
97
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]);
122        }
123        if (m > 0)
124          max_index = Math.Max(max_index, x[m - 1].Index);
125        vx.Add(x);
126      }
127
128      TemporaryCulture.Stop();
129
130      return new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index);
131    }
132
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();
140
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();
149
150      TemporaryCulture.Stop();
151    }
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    }
167
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      }
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.