Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7034 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.IO;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using SVM;
31using HeuristicLab.Problems.DataAnalysis;
32using System.Drawing;
33
34namespace HeuristicLab.Algorithms.DataAnalysis {
35  /// <summary>
36  /// Represents a k-Means clustering model.
37  /// </summary>
38  [StorableClass]
39  [Item("KMeansClusteringModel", "Represents a k-Means clustering model.")]
40  public sealed class KMeansClusteringModel : NamedItem, IClusteringModel {
41    public override Image ItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
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(Dataset 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.