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/GaussianTransform.cs

    r1819 r2415  
    2121using System.Collections.Generic;
    2222using System.IO;
     23using System.Globalization;
     24using System.Threading;
    2325
    2426namespace SVM
    2527{
    26     /// <remarks>
     28    /// <summary>
    2729    /// A transform which learns the mean and variance of a sample set and uses these to transform new data
    2830    /// so that it has zero mean and unit variance.
    29     /// </remarks>
     31    /// </summary>
    3032    public class GaussianTransform : IRangeTransform
    3133    {
    32         private List<Node[]> _samples;
    33         private int _maxIndex;
    34 
    3534        private double[] _means;
    3635        private double[] _stddevs;
    3736
    3837        /// <summary>
     38        /// Determines the Gaussian transform for the provided problem.
     39        /// </summary>
     40        /// <param name="prob">The Problem to analyze</param>
     41        /// <returns>The Gaussian transform for the problem</returns>
     42        public static GaussianTransform Compute(Problem prob)
     43        {
     44            int[] counts = new int[prob.MaxIndex];
     45            double[] means = new double[prob.MaxIndex];
     46            foreach (Node[] sample in prob.X)
     47            {
     48                for (int i = 0; i < sample.Length; i++)
     49                {
     50                    means[sample[i].Index-1] += sample[i].Value;
     51                    counts[sample[i].Index-1]++;
     52                }
     53            }
     54            for (int i = 0; i < prob.MaxIndex; i++)
     55            {
     56                if (counts[i] == 0)
     57                    counts[i] = 2;
     58                means[i] /= counts[i];
     59            }
     60
     61            double[] stddevs = new double[prob.MaxIndex];
     62            foreach (Node[] sample in prob.X)
     63            {
     64                for (int i = 0; i < sample.Length; i++)
     65                {
     66                    double diff = sample[i].Value - means[sample[i].Index - 1];
     67                    stddevs[sample[i].Index - 1] += diff * diff;
     68                }
     69            }
     70            for (int i = 0; i < prob.MaxIndex; i++)
     71            {
     72                if (stddevs[i] == 0)
     73                    continue;
     74                stddevs[i] /= (counts[i] - 1);
     75                stddevs[i] = Math.Sqrt(stddevs[i]);
     76            }
     77
     78            return new GaussianTransform(means, stddevs);
     79        }
     80
     81        /// <summary>
    3982        /// Constructor.
    4083        /// </summary>
    41         /// <param name="maxIndex">The maximum index of the vectors to be transformed</param>
    42         public GaussianTransform(int maxIndex)
    43         {
    44             _samples = new List<Node[]>();
    45         }
    46         private GaussianTransform(double[] means, double[] stddevs, int maxIndex)
     84        /// <param name="means">Means in each dimension</param>
     85        /// <param name="stddevs">Standard deviation in each dimension</param>
     86        public GaussianTransform(double[] means, double[] stddevs)
    4787        {
    4888            _means = means;
    4989            _stddevs = stddevs;
    50             _maxIndex = maxIndex;
    51         }
    52 
    53         /// <summary>
    54         /// Adds a sample to the data.  No computation is performed.  The maximum index of the
    55         /// sample must be less than MaxIndex.
    56         /// </summary>
    57         /// <param name="sample">The sample to add</param>
    58         public void Add(Node[] sample)
    59         {
    60             _samples.Add(sample);
    61         }
    62 
    63         /// <summary>
    64         /// Computes the statistics for the samples which have been obtained so far.
    65         /// </summary>
    66         public void ComputeStatistics()
    67         {
    68             int[] counts = new int[_maxIndex];
    69             _means = new double[_maxIndex];
    70             foreach(Node[] sample in _samples)
    71             {
    72                 for (int i = 0; i < sample.Length; i++)
    73                 {
    74                     _means[sample[i].Index] += sample[i].Value;
    75                     counts[sample[i].Index]++;
    76                 }
    77             }
    78             for (int i = 0; i < _maxIndex; i++)
    79             {
    80                 if (counts[i] == 0)
    81                     counts[i] = 2;
    82                 _means[i] /= counts[i];
    83             }
    84 
    85             _stddevs = new double[_maxIndex];
    86             foreach(Node[] sample in _samples)
    87             {
    88                 for (int i = 0; i < sample.Length; i++)
    89                 {
    90                     double diff = sample[i].Value - _means[sample[i].Index];
    91                     _stddevs[sample[i].Index] += diff * diff;
    92                 }
    93             }
    94             for (int i = 0; i < _maxIndex; i++)
    95             {
    96                 if (_stddevs[i] == 0)
    97                     continue;
    98                 _stddevs[i] /= (counts[i]-1);
    99                 _stddevs[i] = Math.Sqrt(_stddevs[i]);
    100             }
    10190        }
    10291
     
    10998        public static void Write(Stream stream, GaussianTransform transform)
    11099        {
     100            TemporaryCulture.Start();
     101
    111102            StreamWriter output = new StreamWriter(stream);
    112             output.WriteLine(transform._maxIndex);
    113             for (int i = 0; i < transform._maxIndex; i++)
     103            output.WriteLine(transform._means.Length);
     104            for (int i = 0; i < transform._means.Length; i++)
    114105                output.WriteLine("{0} {1}", transform._means[i], transform._stddevs[i]);
    115106            output.Flush();
     107
     108            TemporaryCulture.Stop();
    116109        }
    117110
     
    123116        public static GaussianTransform Read(Stream stream)
    124117        {
     118            TemporaryCulture.Start();
     119
    125120            StreamReader input = new StreamReader(stream);
    126             int length = int.Parse(input.ReadLine());
     121            int length = int.Parse(input.ReadLine(), CultureInfo.InvariantCulture);
    127122            double[] means = new double[length];
    128123            double[] stddevs = new double[length];
     
    130125            {
    131126                string[] parts = input.ReadLine().Split();
    132                 means[i] = double.Parse(parts[0]);
    133                 stddevs[i] = double.Parse(parts[1]);
    134             }
    135             return new GaussianTransform(means, stddevs, length);
     127                means[i] = double.Parse(parts[0], CultureInfo.InvariantCulture);
     128                stddevs[i] = double.Parse(parts[1], CultureInfo.InvariantCulture);
     129            }
     130
     131            TemporaryCulture.Stop();
     132
     133            return new GaussianTransform(means, stddevs);
    136134        }
    137135
     
    177175        /// <summary>
    178176        /// Transform the input value using the transform stored for the provided index.
    179         /// <see cref="ComputeStatistics"/> must be called first, or the transform must
    180         /// have been read from the disk.
    181177        /// </summary>
    182178        /// <param name="input">Input value</param>
     
    185181        public double Transform(double input, int index)
    186182        {
     183            index--;
    187184            if (_stddevs[index] == 0)
    188185                return 0;
     
    192189        }
    193190        /// <summary>
    194         /// Transforms the input array.  <see cref="ComputeStatistics"/> must be called
    195         /// first, or the transform must have been read from the disk.
     191        /// Transforms the input array.
    196192        /// </summary>
    197193        /// <param name="input">The array to transform</param>
Note: See TracChangeset for help on using the changeset viewer.