File: clustererFlame.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (189 lines) | stat: -rw-r--r-- 5,758 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*********************************************************************
FLAME Implementation in MLDemos
Copyright (C) Pierre-Antoine Sondag (pasondag@gmail.com) 2012

Based on the standard implementation of FLAME data clustering algorithm.
Copyright (C) 2007, Fu Limin (phoolimin@gmail.com).
All rights reserved.

MLDemos: A User-Friendly visualization toolkit for machine learning
Copyright (C) 2010  Basilio Noris
Contact: mldemos@b4silio.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License,
version 3 as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*********************************************************************/
#include <clustererFlame.h>
#include <iostream>
#include <math.h>
#include <ostream>
#include <public.h>
#include <QHash>
#include <string>
#include <sstream>
#include "flame.h"
#include "assert.h"

using namespace std;

void ClustererFlame::SetParams(
    int knnParameter,
    int knnMetric,
    int maxIterationsParameter,
    bool isSeveralClasses,
    float thresholdParameter) {
    this->knnParameter = knnParameter;
    this->knnMetric = knnMetric;
    this->maxIterationsParameter = maxIterationsParameter;
    this->isSeveralClasses = isSeveralClasses;
    this->thresholdParameter = thresholdParameter;
}

void ClustererFlame::PrintDone() {
    printf("done.\n");
    fflush(stdout);
}

void ClustererFlame::Train(std::vector<fvec> samples) {
    if(!samples.size()) {
        return;
    }

    resultMap.clear();

    int N = samples.size();
    int M = samples[0].size();

    // Preconditions. Fail-fast on errors.
    // All datapoints are of same dimention.
    for (int i = 0; i < N; i++) {
        assert(samples[i].size() == M);
    }

    printf("Making a defensive deepcopy of the data.");
    fflush(stdout);

    data = (float**) malloc( N * sizeof(float*) );

    for (int i = 0; i < N; i++) {
        data[i] = (float*) malloc( M * sizeof(float) );
        for (int j = 0; j < M; j++) {
            data[i][j] = samples[i][j];
        }
    }
    PrintDone();

    printf("Initialize Flame data structure: ");
    fflush(stdout);
    flameStructure = Flame_New();
    PrintDone();

    printf("Send data to Flame structure: ");
    fflush(stdout);
    Flame_SetDataMatrix(flameStructure, data, N, M, knnMetric);
    PrintDone();

    free(data);

    printf("Detecting Cluster Supporting Objects: ");
    fflush(stdout);
    Flame_DefineSupports(flameStructure, knnParameter, -2.0);
    nbClusters = flameStructure->cso_count + 1; //Adding 1 for outlier class.
    printf("done, found %i.\n", flameStructure->cso_count);

    printf("Propagating fuzzy memberships: ");
    fflush(stdout);
    Flame_LocalApproximation(flameStructure, maxIterationsParameter, 1e-6);
    PrintDone();

    printf("Defining clusters from fuzzy memberships: ");
    fflush(stdout);
    if (isSeveralClasses) {
        Flame_MakeClusters(flameStructure, thresholdParameter);
    } else {
        Flame_MakeClusters(flameStructure, -1);
    }
    PrintDone();

    printf( "Displaying results: " );

    for (int i = 0; i <= flameStructure->cso_count; i++) {
        if (i == flameStructure->cso_count) {
            printf("\nCluster outliers, with %6i members:\n",
                flameStructure->clusters[i].size);
        } else {
            printf("\nCluster %3i, with %6i members:\n",
                i+1,
                flameStructure->clusters[i].size );
        }

        for (int j = 0; j<flameStructure->clusters[i].size; j++) {
            printf( "%5i", flameStructure->clusters[i].array[j] );
            resultMap[(samples[flameStructure->clusters[i].array[j]])].push_back(i);
        }
        printf( "\n" );
    }

    for (int i = 0; i < flameStructure->N; i++) {
        if (flameStructure->obtypes[i] == OBT_SUPPORT) {
            supports.push_back(samples[i]);
            std::cout <<  i << ",";
        }
    }
    std::cout << "\n";
    fflush(stdout);
}

fvec ClustererFlame::Test( const fvec &sample) {
    if (resultMap.count(sample) < 1) {
        return fvec(1,0);
    }

    fvec res;
    res.resize(nbClusters, 0); // set everyone to 0
    for (int i = 0; i < resultMap[sample].size(); i++) {
        int index = resultMap[sample][i];
        // set to 1/(nb affected class) for classes to which sample belong
        res[index] = 1/resultMap[sample].size();
    }
    return res;
}

// Not always called properly by the main program :-(
const char *ClustererFlame::GetInfoString() {
    stringstream s;

    s << "Flame\n\n";
    s << "Support definition" << "\n";
    s << "KNN: " << knnParameter << "\n";

    s << "Cluster making" << "\n";
    s << "Iterations: " << flameStructure->steps << " over " << maxIterationsParameter << "\n";

    s << "# clusters/supports found: " << flameStructure->cso_count << " \n\n";

    for( int i=0; i<=flameStructure->cso_count; i++) {
        if( i == flameStructure->cso_count ) {
            s << "# outliers elements: " << flameStructure->clusters[i].size << "\n";
        } else {
            s << "# elements in cluster: " << (i + 1) << ": " << flameStructure->clusters[i].size << "\n";
        }
    }

    const char *result = s.str().c_str();
    return result;
}

vector<fvec> ClustererFlame::GetSupports() {
    return supports;
}