Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClusteringModel.cs @ 13782

Last change on this file since 13782 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  /// <summary>
31  /// Represents a k-Means clustering model.
32  /// </summary>
33  [StorableClass]
34  [Item("KMeansClusteringModel", "Represents a k-Means clustering model.")]
35  public sealed class KMeansClusteringModel : NamedItem, IClusteringModel {
36
37
38    [Storable]
39    private string[] allowedInputVariables;
40    public IEnumerable<string> AllowedInputVariables
41    {
42      get { return allowedInputVariables; }
43    }
44    [Storable]
45    private List<double[]> centers;
46    public IEnumerable<double[]> Centers
47    {
48      get
49      {
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(IDataset dataset, IEnumerable<int> rows) {
84      return KMeansClusteringUtil.FindClosestCenters(centers, dataset, allowedInputVariables, rows);
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.