1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 |
|
---|
30 | namespace 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 | [Storable]
|
---|
42 | private string[] allowedInputVariables;
|
---|
43 | public IEnumerable<string> AllowedInputVariables {
|
---|
44 | get { return allowedInputVariables; }
|
---|
45 | }
|
---|
46 | [Storable]
|
---|
47 | private List<double[]> centers;
|
---|
48 | public IEnumerable<double[]> Centers {
|
---|
49 | get {
|
---|
50 | return centers.Select(x => (double[])x.Clone());
|
---|
51 | }
|
---|
52 | }
|
---|
53 | [StorableConstructor]
|
---|
54 | private KMeansClusteringModel(bool deserializing) : base(deserializing) { }
|
---|
55 | private KMeansClusteringModel(KMeansClusteringModel original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
58 | this.centers = new List<double[]>(original.Centers);
|
---|
59 | }
|
---|
60 | public KMeansClusteringModel(double[,] centers, IEnumerable<string> allowedInputVariables)
|
---|
61 | : base() {
|
---|
62 | this.name = ItemName;
|
---|
63 | this.description = ItemDescription;
|
---|
64 | // disect center matrix into list of double[]
|
---|
65 | // centers are given as double matrix where number of rows = dimensions and number of columns = clusters
|
---|
66 | // each column is a cluster center
|
---|
67 | this.centers = new List<double[]>();
|
---|
68 | for (int i = 0; i < centers.GetLength(1); i++) {
|
---|
69 | double[] c = new double[centers.GetLength(0)];
|
---|
70 | for (int j = 0; j < c.Length; j++) {
|
---|
71 | c[j] = centers[j, i];
|
---|
72 | }
|
---|
73 | this.centers.Add(c);
|
---|
74 | }
|
---|
75 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
79 | return new KMeansClusteringModel(this, cloner);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | public IEnumerable<int> GetClusterValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
84 | return KMeansClusteringUtil.FindClosestCenters(centers, dataset, allowedInputVariables, rows);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|