Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis.DecisionTrees/3.4/Splitting/CorrelationImpurityCalculator.cs @ 17912

Last change on this file since 17912 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  /// <summary>
29  /// Helper class for incremental split calculation.
30  /// Used while moving a potential splitter along the ordered training instances
31  /// </summary>
32  internal class CorreleationImpurityCalculator {
33    #region state
34    //Data
35    private readonly List<double> attributeValues;
36    private readonly List<double> targetValues;
37    private readonly double order;
38    private readonly UnivariateOnlineLR left;
39    private readonly UnivariateOnlineLR right;
40    #endregion
41
42    #region Properties
43    public double Impurity { get; private set; }
44    public double SplitValue {
45      get {
46        if (left.Size <= 0) return double.NegativeInfinity;
47        if (left.Size >= attributeValues.Count) return double.PositiveInfinity;
48        return (attributeValues[left.Size - 1] + attributeValues[left.Size]) / 2;
49      }
50    }
51    public bool ValidPosition {
52      get { return !attributeValues[left.Size - 1].IsAlmost(attributeValues[left.Size]); }
53    }
54    public int LeftSize {
55      get { return left.Size; }
56    }
57    #endregion
58
59    #region Constructors
60    public CorreleationImpurityCalculator(int partition, IEnumerable<double> atts, IEnumerable<double> targets, double order) {
61      if (order <= 0) throw new ArgumentException("Splitter order must be larger than 0");
62      this.order = order;
63      attributeValues = atts.ToList();
64      targetValues = targets.ToList();
65      left = new UnivariateOnlineLR(attributeValues.Take(partition).ToList(), targetValues.Take(partition).ToList());
66      right = new UnivariateOnlineLR(attributeValues.Skip(partition).ToList(), targetValues.Skip(partition).ToList());
67      UpdateImpurity();
68    }
69    #endregion
70
71    #region IImpurityCalculator
72    public void Increment() {
73      var target = targetValues[left.Size];
74      var att = attributeValues[left.Size];
75      left.Add(att, target);
76      right.Remove(att, target);
77      UpdateImpurity();
78    }
79    #endregion
80
81    private void UpdateImpurity() {
82      var yl = Math.Pow(left.Ssr, 1.0 / order);
83      var yr = Math.Pow(right.Ssr, 1.0 / order);
84      if (left.Size > 1 && right.Size > 1) Impurity = -yl - yr;
85      else Impurity = double.MinValue;
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.