[8323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8323] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[8323] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[14843] | 31 | [Obsolete("Use transformation classes in Problems.DataAnalysis instead")]
|
---|
[16565] | 32 | [StorableType("52EABC0F-B8D2-4ADD-ACC2-C825D3F1D6F3")]
|
---|
[8416] | 33 | [Item(Name = "Scaling", Description = "Contains information about scaling of variables for data-analysis algorithms.")]
|
---|
[8323] | 34 | public class Scaling : Item {
|
---|
| 35 | [Storable]
|
---|
| 36 | private Dictionary<string, Tuple<double, double>> scalingParameters = new Dictionary<string, Tuple<double, double>>();
|
---|
| 37 | [StorableConstructor]
|
---|
[16565] | 38 | protected Scaling(StorableConstructorFlag _) : base(_) { }
|
---|
[8323] | 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 | }
|
---|
[12509] | 44 | public Scaling(IDataset ds, IEnumerable<string> variables, IEnumerable<int> rows) {
|
---|
[8323] | 45 | foreach (var variable in variables) {
|
---|
| 46 | var values = ds.GetDoubleValues(variable, rows);
|
---|
[8829] | 47 | var min = values.Where(x => !double.IsNaN(x)).Min();
|
---|
| 48 | var max = values.Where(x => !double.IsNaN(x)).Max();
|
---|
[8323] | 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 |
|
---|
[12509] | 57 | public IEnumerable<double> GetScaledValues(IDataset ds, string variable, IEnumerable<int> rows) {
|
---|
[8323] | 58 | double min = scalingParameters[variable].Item1;
|
---|
| 59 | double max = scalingParameters[variable].Item2;
|
---|
[8829] | 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]
|
---|
[8323] | 62 | }
|
---|
[8463] | 63 |
|
---|
| 64 | public void GetScalingParameters(string variable, out double min, out double max) {
|
---|
| 65 | min = scalingParameters[variable].Item1;
|
---|
| 66 | max = scalingParameters[variable].Item2;
|
---|
| 67 | }
|
---|
[8323] | 68 | }
|
---|
| 69 | }
|
---|