Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/LibSVM/Scaling.cs @ 6347

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

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

File size: 4.3 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;
21
22namespace SVM
23{
24    /// <remarks>
25    /// Deals with the scaling of Problems so they have uniform ranges across all dimensions in order to
26    /// result in better SVM performance.
27    /// </remarks>
28    public static class Scaling
29    {
30        /// <summary>
31        /// Default lower bound for scaling (-1).
32        /// </summary>
33        public const int DEFAULT_LOWER_BOUND = -1;
34        /// <summary>
35        /// Default upper bound for scaling (1).
36        /// </summary>
37        public const int DEFAULT_UPPER_BOUND = 1;
38
39        /// <summary>
40        /// Determines the Range transform for the provided problem.  Uses the default lower and upper bounds.
41        /// </summary>
42        /// <param name="prob">The Problem to analyze</param>
43        /// <returns>The Range transform for the problem</returns>
44        public static RangeTransform DetermineRange(Problem prob)
45        {
46            return DetermineRangeTransform(prob, DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
47        }
48        /// <summary>
49        /// Determines the Range transform for the provided problem.
50        /// </summary>
51        /// <param name="prob">The Problem to analyze</param>
52        /// <param name="lowerBound">The lower bound for scaling</param>
53        /// <param name="upperBound">The upper bound for scaling</param>
54        /// <returns>The Range transform for the problem</returns>
55        public static RangeTransform DetermineRangeTransform(Problem prob, double lowerBound, double upperBound)
56        {
57            double[] minVals = new double[prob.MaxIndex];
58            double[] maxVals = new double[prob.MaxIndex];
59            for (int i = 0; i < prob.MaxIndex; i++)
60            {
61                minVals[i] = double.MaxValue;
62                maxVals[i] = double.MinValue;
63            }
64            for (int i = 0; i < prob.Count; i++)
65            {
66                for (int j = 0; j < prob.X[i].Length; j++)
67                {
68                    int index = prob.X[i][j].Index-1;
69                    double value = prob.X[i][j].Value;
70                    minVals[index] = Math.Min(minVals[index], value);
71                    maxVals[index] = Math.Max(maxVals[index], value);
72                }
73            }
74            for (int i = 0; i < prob.MaxIndex; i++)
75            {
76                if (minVals[i] == double.MaxValue || maxVals[i] == double.MinValue)
77                {
78                    minVals[i] = 0;
79                    maxVals[i] = 0;
80                }
81            }
82            return new RangeTransform(minVals, maxVals, lowerBound, upperBound);
83        }
84        /// <summary>
85        /// Scales a problem using the provided range.  This will not affect the parameter.
86        /// </summary>
87        /// <param name="prob">The problem to scale</param>
88        /// <param name="range">The Range transform to use in scaling</param>
89        /// <returns>The Scaled problem</returns>
90        public static Problem Scale(Problem prob, IRangeTransform range)
91        {
92            Problem scaledProblem = new Problem(prob.Count, new double[prob.Count], new Node[prob.Count][], prob.MaxIndex);
93            for (int i = 0; i < scaledProblem.Count; i++)
94            {
95                scaledProblem.X[i] = new Node[prob.X[i].Length];
96                for (int j = 0; j < scaledProblem.X[i].Length; j++)
97                    scaledProblem.X[i][j] = new Node(prob.X[i][j].Index, range.Transform(prob.X[i][j].Value, prob.X[i][j].Index));
98                scaledProblem.Y[i] = prob.Y[i];
99            }
100            return scaledProblem;
101        }
102    }
103}
Note: See TracBrowser for help on using the repository browser.