Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClusteringModel.cs @ 17578

Last change on this file since 17578 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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