Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/09 11:58:21 (15 years ago)
Author:
gkronber
Message:

Updated LibSVM project to latest version. #774

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/LibSVM/RangeTransform.cs

    r1819 r2415  
    2020using System;
    2121using System.IO;
     22using System.Threading;
     23using System.Globalization;
    2224
    2325namespace SVM
    2426{
    25     /// <remarks>
     27    /// <summary>
    2628    /// Class which encapsulates a range transformation.
    27     /// </remarks>
     29    /// </summary>
    2830    public class RangeTransform : IRangeTransform
    2931    {
     32        /// <summary>
     33        /// Default lower bound for scaling (-1).
     34        /// </summary>
     35        public const int DEFAULT_LOWER_BOUND = -1;
     36        /// <summary>
     37        /// Default upper bound for scaling (1).
     38        /// </summary>
     39        public const int DEFAULT_UPPER_BOUND = 1;
     40
     41        /// <summary>
     42        /// Determines the Range transform for the provided problem.  Uses the default lower and upper bounds.
     43        /// </summary>
     44        /// <param name="prob">The Problem to analyze</param>
     45        /// <returns>The Range transform for the problem</returns>
     46        public static RangeTransform Compute(Problem prob)
     47        {
     48            return Compute(prob, DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND);
     49        }
     50        /// <summary>
     51        /// Determines the Range transform for the provided problem.
     52        /// </summary>
     53        /// <param name="prob">The Problem to analyze</param>
     54        /// <param name="lowerBound">The lower bound for scaling</param>
     55        /// <param name="upperBound">The upper bound for scaling</param>
     56        /// <returns>The Range transform for the problem</returns>
     57        public static RangeTransform Compute(Problem prob, double lowerBound, double upperBound)
     58        {
     59            double[] minVals = new double[prob.MaxIndex];
     60            double[] maxVals = new double[prob.MaxIndex];
     61            for (int i = 0; i < prob.MaxIndex; i++)
     62            {
     63                minVals[i] = double.MaxValue;
     64                maxVals[i] = double.MinValue;
     65            }
     66            for (int i = 0; i < prob.Count; i++)
     67            {
     68                for (int j = 0; j < prob.X[i].Length; j++)
     69                {
     70                    int index = prob.X[i][j].Index - 1;
     71                    double value = prob.X[i][j].Value;
     72                    minVals[index] = Math.Min(minVals[index], value);
     73                    maxVals[index] = Math.Max(maxVals[index], value);
     74                }
     75            }
     76            for (int i = 0; i < prob.MaxIndex; i++)
     77            {
     78                if (minVals[i] == double.MaxValue || maxVals[i] == double.MinValue)
     79                {
     80                    minVals[i] = 0;
     81                    maxVals[i] = 0;
     82                }
     83            }
     84            return new RangeTransform(minVals, maxVals, lowerBound, upperBound);
     85        }
     86
    3087        private double[] _inputStart;
    3188        private double[] _inputScale;
     
    72129        {
    73130            Node[] output = new Node[input.Length];
    74             for (int i = 0; i < _length; i++)
     131            for (int i = 0; i < output.Length; i++)
    75132            {
    76133                int index = input[i].Index;
     
    104161        public static void Write(Stream stream, RangeTransform r)
    105162        {
     163            TemporaryCulture.Start();
     164
    106165            StreamWriter output = new StreamWriter(stream);
    107166            output.WriteLine(r._length);
     
    116175            output.WriteLine("{0} {1}", r._outputStart, r._outputScale);
    117176            output.Flush();
     177
     178            TemporaryCulture.Stop();
    118179        }
    119180
     
    161222        public static RangeTransform Read(Stream stream)
    162223        {
     224            TemporaryCulture.Start();
     225
    163226            StreamReader input = new StreamReader(stream);
    164227            int length = int.Parse(input.ReadLine());
     
    174237            double outputStart = double.Parse(parts[0]);
    175238            double outputScale = double.Parse(parts[1]);
     239
     240            TemporaryCulture.Stop();
     241
    176242            return new RangeTransform(inputStart, inputScale, outputStart, outputScale, length);
    177243        }
Note: See TracChangeset for help on using the changeset viewer.