Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/LibSVM/Problem.cs @ 3810

Last change on this file since 3810 was 2415, checked in by gkronber, 15 years ago

Updated LibSVM project to latest version. #774

File size: 6.1 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.IO;
22using System.Collections.Generic;
23using System.Threading;
24using System.Globalization;
25
26namespace SVM
27{
28    /// <summary>
29    /// Encapsulates a problem, or set of vectors which must be classified.
30    /// </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
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;
52        }
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        }
115
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();
124
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;
129
130            while (input.Peek() > -1)
131            {
132                string[] parts = input.ReadLine().Trim().Split();
133
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            }
148
149            TemporaryCulture.Stop();
150
151            return new Problem(vy.Count, vy.ToArray(), vx.ToArray(), max_index);
152        }
153
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        }
211    }
212}
Note: See TracBrowser for help on using the repository browser.