Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/LibSVM/RangeTransform.cs @ 2388

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

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

File size: 6.8 KB
RevLine 
[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
20using System;
21using System.IO;
22
23namespace SVM
24{
25    /// <remarks>
26    /// Class which encapsulates a range transformation.
27    /// </remarks>
28    public class RangeTransform : IRangeTransform
29    {
30        private double[] _inputStart;
31        private double[] _inputScale;
32        private double _outputStart;
33        private double _outputScale;
34        private int _length;
35
36        /// <summary>
37        /// Constructor.
38        /// </summary>
39        /// <param name="minValues">The minimum values in each dimension.</param>
40        /// <param name="maxValues">The maximum values in each dimension.</param>
41        /// <param name="lowerBound">The desired lower bound for all dimensions.</param>
42        /// <param name="upperBound">The desired upper bound for all dimensions.</param>
43        public RangeTransform(double[] minValues, double[] maxValues, double lowerBound, double upperBound)
44        {
45            _length = minValues.Length;
46            if(maxValues.Length != _length)
47                throw new Exception("Number of max and min values must be equal.");
48            _inputStart = new double[_length];
49            _inputScale = new double[_length];
50            for (int i = 0; i < _length; i++)
51            {
52                _inputStart[i] = minValues[i];
53                _inputScale[i] = maxValues[i] - minValues[i];
54            }
55            _outputStart = lowerBound;
56            _outputScale = upperBound - lowerBound;
57        }
58        private RangeTransform(double[] inputStart, double[] inputScale, double outputStart, double outputScale, int length)
59        {
60            _inputStart = inputStart;
61            _inputScale = inputScale;
62            _outputStart = outputStart;
63            _outputScale = outputScale;
64            _length = length;
65        }
66        /// <summary>
67        /// Transforms the input array based upon the values provided.
68        /// </summary>
69        /// <param name="input">The input array</param>
70        /// <returns>A scaled array</returns>
71        public Node[] Transform(Node[] input)
72        {
73            Node[] output = new Node[input.Length];
74            for (int i = 0; i < _length; i++)
75            {
76                int index = input[i].Index;
77                double value = input[i].Value;
78                output[i] = new Node(index, Transform(value, index));
79            }
80            return output;
81        }
82
83        /// <summary>
84        /// Transforms this an input value using the scaling transform for the provided dimension.
85        /// </summary>
86        /// <param name="input">The input value to transform</param>
87        /// <param name="index">The dimension whose scaling transform should be used</param>
88        /// <returns>The scaled value</returns>
89        public double Transform(double input, int index)
90        {
91            index--;
92            double tmp = input - _inputStart[index];
93            if (_inputScale[index] == 0)
94                return 0;
95            tmp /= _inputScale[index];
96            tmp *= _outputScale;
97            return tmp + _outputStart;
98        }
99        /// <summary>
100        /// Writes this Range transform to a stream.
101        /// </summary>
102        /// <param name="stream">The stream to write to</param>
103        /// <param name="r">The range to write</param>
104        public static void Write(Stream stream, RangeTransform r)
105        {
106            StreamWriter output = new StreamWriter(stream);
107            output.WriteLine(r._length);
108            output.Write(r._inputStart[0]);
109            for(int i=1; i<r._inputStart.Length; i++)
110                output.Write(" " + r._inputStart[i]);
111            output.WriteLine();
112            output.Write(r._inputScale[0]);
113            for (int i = 1; i < r._inputScale.Length; i++)
114                output.Write(" " + r._inputScale[i]);
115            output.WriteLine();
116            output.WriteLine("{0} {1}", r._outputStart, r._outputScale);
117            output.Flush();
118        }
119
120        /// <summary>
121        /// Writes this Range transform to a file.    This will overwrite any previous data in the file.
122        /// </summary>
123        /// <param name="outputFile">The file to write to</param>
124        /// <param name="r">The Range to write</param>
125        public static void Write(string outputFile, RangeTransform r)
126        {
127            FileStream s = File.Open(outputFile, FileMode.Create);
128            try
129            {
130                Write(s, r);
131            }
132            finally
133            {
134                s.Close();
135            }
136        }
137
138        /// <summary>
139        /// Reads a Range transform from a file.
140        /// </summary>
141        /// <param name="inputFile">The file to read from</param>
142        /// <returns>The Range transform</returns>
143        public static RangeTransform Read(string inputFile)
144        {
145            FileStream s = File.OpenRead(inputFile);
146            try
147            {
148                return Read(s);
149            }
150            finally
151            {
152                s.Close();
153            }
154        }
155
156        /// <summary>
157        /// Reads a Range transform from a stream.
158        /// </summary>
159        /// <param name="stream">The stream to read from</param>
160        /// <returns>The Range transform</returns>
161        public static RangeTransform Read(Stream stream)
162        {
163            StreamReader input = new StreamReader(stream);
164            int length = int.Parse(input.ReadLine());
165            double[] inputStart = new double[length];
166            double[] inputScale = new double[length];
167            string[] parts = input.ReadLine().Split();
168            for (int i = 0; i < length; i++)
169                inputStart[i] = double.Parse(parts[i]);
170            parts = input.ReadLine().Split();
171            for (int i = 0; i < length; i++)
172                inputScale[i] = double.Parse(parts[i]);
173            parts = input.ReadLine().Split();
174            double outputStart = double.Parse(parts[0]);
175            double outputScale = double.Parse(parts[1]);
176            return new RangeTransform(inputStart, inputScale, outputStart, outputScale, length);
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.