[5651] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5651] | 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;
|
---|
[7201] | 23 | using System.Drawing;
|
---|
[5651] | 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 {
|
---|
[7201] | 37 | public static new Image StaticItemImage {
|
---|
[5651] | 38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[13921] | 41 | public IEnumerable<string> VariablesUsedForPrediction {
|
---|
| 42 | get { return allowedInputVariables; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[5651] | 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() {
|
---|
[5658] | 66 | this.name = ItemName;
|
---|
| 67 | this.description = ItemDescription;
|
---|
[5651] | 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 |
|
---|
[12509] | 87 | public IEnumerable<int> GetClusterValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[5651] | 88 | return KMeansClusteringUtil.FindClosestCenters(centers, dataset, allowedInputVariables, rows);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|