Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RBFRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClusteringModel.cs @ 14888

Last change on this file since 14888 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 3.4 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Represents a k-Means clustering model.
33  /// </summary>
34  [StorableClass]
35  [Item("KMeansClusteringModel", "Represents a k-Means clustering model.")]
36  public sealed class KMeansClusteringModel : NamedItem, IClusteringModel {
37    public static new Image StaticItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
39    }
40
41    public IEnumerable<string> VariablesUsedForPrediction {
42      get { return allowedInputVariables; }
43    }
44
45    [Storable]
46    private string[] allowedInputVariables;
47    public IEnumerable<string> AllowedInputVariables {
48      get { return allowedInputVariables; }
49    }
50    [Storable]
51    private List<double[]> centers;
52    public IEnumerable<double[]> Centers {
53      get {
54        return centers.Select(x => (double[])x.Clone());
55      }
56    }
57    [StorableConstructor]
58    private KMeansClusteringModel(bool deserializing) : base(deserializing) { }
59    private KMeansClusteringModel(KMeansClusteringModel original, Cloner cloner)
60      : base(original, cloner) {
61      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
62      this.centers = new List<double[]>(original.Centers);
63    }
64    public KMeansClusteringModel(double[,] centers, IEnumerable<string> allowedInputVariables)
65      : base() {
66      this.name = ItemName;
67      this.description = ItemDescription;
68      // disect center matrix into list of double[]
69      // centers are given as double matrix where number of rows = dimensions and number of columns = clusters
70      // each column is a cluster center
71      this.centers = new List<double[]>();
72      for (int i = 0; i < centers.GetLength(1); i++) {
73        double[] c = new double[centers.GetLength(0)];
74        for (int j = 0; j < c.Length; j++) {
75          c[j] = centers[j, i];
76        }
77        this.centers.Add(c);
78      }
79      this.allowedInputVariables = allowedInputVariables.ToArray();
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new KMeansClusteringModel(this, cloner);
84    }
85
86
87    public IEnumerable<int> GetClusterValues(IDataset dataset, IEnumerable<int> rows) {
88      return KMeansClusteringUtil.FindClosestCenters(centers, dataset, allowedInputVariables, rows);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.