Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/LibSVM/Problem.cs @ 3684

Last change on this file since 3684 was 1819, checked in by mkommend, 16 years ago

created new project for LibSVM source files (ticket #619)

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