Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis/3.3/Clustering/ClusterHelper.cs @ 13794

Last change on this file since 13794 was 13794, checked in by abeham, 8 years ago

#2457: worked on recommendation algorithms (x-validation)

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
25
26namespace HeuristicLab.Analysis {
27  public class ClusteringHelper<T> {
28    private readonly int k;
29
30    private List<KeyValuePair<T, double>> instances;
31    private List<KeyValuePair<T, double>> excluded;
32    private int[] clusterValues;
33
34    private ClusteringHelper(int K) {
35      this.k = K;
36    }
37
38    /// <summary>
39    /// Helps in clustering data which is available as key-value pairs.
40    /// It is possible to specify an exclude function to omit certain points from clustering.
41    /// These points will be assigned cluster with id k. All other points will be assigned a
42    /// cluster id in the range [0;k-1].
43    /// </summary>
44    /// <param name="k">The maximum number of clusters that should be created.</param>
45    /// <param name="values">The data which links a certain item with a double value that is to be clustered.</param>
46    /// <param name="excludeFunc">The function that allows excluding certing data points which will receive cluster id k.</param>
47    /// <returns>A reference to the helper class to allow fluent calls.</returns>
48    public static ClusteringHelper<T> Cluster(int k, IEnumerable<KeyValuePair<T, double>> values, Func<KeyValuePair<T, double>, bool> excludeFunc = null) {
49      if (excludeFunc == null) excludeFunc = _ => false;
50      var helper = new ClusteringHelper<T>(k);
51      helper.Initialize(values, excludeFunc);
52      if (helper.instances.Count == 0)
53        helper.clusterValues = new int[0];
54      else CkMeans1D.Cluster(helper.instances.Select(x => x.Value).ToArray(), k, out helper.clusterValues);
55      return helper;
56    }
57
58    /// <summary>
59    /// Returns the clustered data by
60    /// </summary>
61    /// <returns></returns>
62    public IEnumerable<KeyValuePair<T, Tuple<double, int>>> GetByInstance() {
63      return GetClustered().Select(x => new KeyValuePair<T, Tuple<double, int>>(x.Item1.Key, Tuple.Create(x.Item1.Value, x.Item2)));
64    }
65
66    public IEnumerable<KeyValuePair<int, List<KeyValuePair<T, double>>>> GetByCluster() {
67      return GetClustered().GroupBy(x => x.Item2)
68        .Select(x => new KeyValuePair<int, List<KeyValuePair<T, double>>>(x.Key, x.Select(y => y.Item1).ToList()));
69    }
70
71    private void Initialize(IEnumerable<KeyValuePair<T, double>> values, Func<KeyValuePair<T, double>, bool> excludeFunc) {
72      instances = new List<KeyValuePair<T, double>>();
73      excluded = new List<KeyValuePair<T, double>>();
74      foreach (var v in values) {
75        if (!excludeFunc(v)) instances.Add(v);
76        else excluded.Add(v);
77      }
78    }
79
80    private IEnumerable<Tuple<KeyValuePair<T, double>, int>> GetClustered() {
81      for (var i = 0; i < clusterValues.Length; i++) {
82        yield return Tuple.Create(instances[i], clusterValues[i]);
83      }
84      foreach (var ex in excluded)
85        yield return Tuple.Create(ex, k);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.