source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ZNormalizationTransformation.cs @ 15846

Last change on this file since 15846 was 15846, checked in by pfleck, 15 months ago

#2906 First concept of simple transformation (single target transformation)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [Item("Z-Score Normalization", "Z-Score normalization transformation to standardize (target_mu = 0, target_sigma = 1) the values")]
32  [StorableClass]
33  public class ZNormalizationTransformation : Transformation<double> {
34    #region Parameters
35    private IFixedValueParameter<DoubleValue> TargetMeanParameter {
36      get { return (IFixedValueParameter<DoubleValue>)Parameters["Target Mean"]; }
37    }
38    private IFixedValueParameter<DoubleValue> TargetStandardDeviationParameter {
39      get { return (IFixedValueParameter<DoubleValue>)Parameters["Target Standard Deviation"]; }
40    }
41    private IFixedValueParameter<DoubleValue> OriginalMeanParameter {
42      get { return (IFixedValueParameter<DoubleValue>)Parameters["Original Mean"]; }
43    }
44    private IFixedValueParameter<DoubleValue> OriginalStandardDeviationParameter {
45      get { return (IFixedValueParameter<DoubleValue>)Parameters["Original Standard Deviation"]; }
46    }
47    #endregion
48
49    #region Properties
50    public double TargetMean {
51      get { return TargetMeanParameter.Value.Value; }
52      set { TargetMeanParameter.Value.Value = value; }
53    }
54    public double TargetStandardDeviation {
55      get { return TargetStandardDeviationParameter.Value.Value; }
56      set { TargetStandardDeviationParameter.Value.Value = value; }
57    }
58    public double OriginalMean {
59      get { return OriginalMeanParameter.Value.Value; }
60      set { OriginalMeanParameter.Value.Value = value; }
61    }
62    public double OriginalStandardDeviation {
63      get { return OriginalStandardDeviationParameter.Value.Value; }
64      set { OriginalStandardDeviationParameter.Value.Value = value; }
65    }
66    #endregion
67
68    #region Constructor, Cloning & Persistence
69    public ZNormalizationTransformation()
70      : base() {
71      Parameters.Add(new FixedValueParameter<DoubleValue>("Target Mean", new DoubleValue(0)));
72      Parameters.Add(new FixedValueParameter<DoubleValue>("Target Standard Deviation", new DoubleValue(1)));
73      Parameters.Add(new FixedValueParameter<DoubleValue>("Original Mean", new DoubleValue(double.NaN)) { Hidden = true });
74      Parameters.Add(new FixedValueParameter<DoubleValue>("Original Standard Deviation", new DoubleValue(double.NaN)) { Hidden = true });
75    }
76
77    protected ZNormalizationTransformation(ZNormalizationTransformation original, Cloner cloner)
78      : base(original, cloner) {
79    }
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new ZNormalizationTransformation(this, cloner);
82    }
83
84    [StorableConstructor]
85    protected ZNormalizationTransformation(bool deserializing)
86      : base(deserializing) {
87    }
88    #endregion
89
90    public override bool Check(IEnumerable<double> data, out string errorMessage) {
91      if (data.StandardDeviationPop().IsAlmost(0.0)) {
92        errorMessage = "Z-Score Normalization cannot be applied for data with a standard deviation of zero.";
93        return false;
94      }
95      return base.Check(data, out errorMessage);
96    }
97
98    public override void Configure(IEnumerable<double> data) {
99      OriginalMean = data.Average();
100      OriginalStandardDeviation = data.StandardDeviationPop();
101      base.Configure(data);
102    }
103
104    public override IEnumerable<double> Apply(IEnumerable<double> data) {
105      if (double.IsNaN(OriginalMean) || double.IsNaN(OriginalStandardDeviation))
106        Configure(data);
107
108      return Apply(data, TargetMean, TargetStandardDeviation, OriginalMean, OriginalStandardDeviation);
109    }
110
111    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
112      return InverseApply(data, TargetMean, TargetStandardDeviation, OriginalMean, OriginalStandardDeviation);
113    }
114
115    public static IEnumerable<double> Apply(IEnumerable<double> data, double targetMean, double targetStandardDeviation, double originalMean = double.NaN, double originalStandardDeviation = double.NaN) {
116      if (double.IsNaN(originalMean)) originalMean = data.Average();
117      if (double.IsNaN(originalStandardDeviation)) originalStandardDeviation = data.StandardDeviationPop();
118
119      return data
120        .Select(x => (x - originalMean) / originalStandardDeviation) // standardize (0, 1)
121        .Select(x => x * targetStandardDeviation + targetMean);
122    }
123
124    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double targetMean, double targetStandardDeviation, double originalMean, double originalStandardDeviation) {
125      return Apply(data, originalMean, originalStandardDeviation, targetMean, targetStandardDeviation);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.