Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/Scaling.cs @ 16683

Last change on this file since 16683 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HEAL.Attic;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [Obsolete("Use transformation classes in Problems.DataAnalysis instead")]
32  [StorableType("52EABC0F-B8D2-4ADD-ACC2-C825D3F1D6F3")]
33  [Item(Name = "Scaling", Description = "Contains information about scaling of variables for data-analysis algorithms.")]
34  public class Scaling : Item {
35    [Storable]
36    private Dictionary<string, Tuple<double, double>> scalingParameters = new Dictionary<string, Tuple<double, double>>();
37    [StorableConstructor]
38    protected Scaling(StorableConstructorFlag _) : base(_) { }
39    protected Scaling(Scaling original, Cloner cloner)
40      : base(original, cloner) {
41      foreach (var pair in original.scalingParameters)
42        scalingParameters.Add(pair.Key, Tuple.Create(pair.Value.Item1, pair.Value.Item2));
43    }
44    public Scaling(IDataset ds, IEnumerable<string> variables, IEnumerable<int> rows) {
45      foreach (var variable in variables) {
46        var values = ds.GetDoubleValues(variable, rows);
47        var min = values.Where(x => !double.IsNaN(x)).Min();
48        var max = values.Where(x => !double.IsNaN(x)).Max();
49        scalingParameters[variable] = Tuple.Create(min, max);
50      }
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new Scaling(this, cloner);
55    }
56
57    public IEnumerable<double> GetScaledValues(IDataset ds, string variable, IEnumerable<int> rows) {
58      double min = scalingParameters[variable].Item1;
59      double max = scalingParameters[variable].Item2;
60      if (min.IsAlmost(max)) return rows.Select(i => 0.0); // return enumerable of zeros
61      return ds.GetDoubleValues(variable, rows).Select(x => (x - min) / (max - min));  // scale to range [0..1]
62    }
63
64    public void GetScalingParameters(string variable, out double min, out double max) {
65      min = scalingParameters[variable].Item1;
66      max = scalingParameters[variable].Item2;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.