-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathelimin.c
More file actions
169 lines (143 loc) · 4.93 KB
/
elimin.c
File metadata and controls
169 lines (143 loc) · 4.93 KB
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
/************************************************************************
* *
* Program package 'lvq_pak': *
* *
* elimin.c *
* - eliminates those entries that are incorrectly classified by knn *
* *
* Version 3.0 *
* Date: 1 Mar 1995 *
* *
* NOTE: This program package is copyrighted in the sense that it *
* may be used for scientific purposes. The package as a whole, or *
* parts thereof, cannot be included or used in any commercial *
* application without written permission granted by its producents. *
* No programs contained in this package may be copied for commercial *
* distribution. *
* *
* All comments concerning this program package may be sent to the *
* e-mail address 'lvq@cochlea.hut.fi'. *
* *
************************************************************************/
#include <stdio.h>
#include <float.h>
#include "lvq_pak.h"
#include "datafile.h"
#include "labels.h"
#include "lvq_rout.h"
#define KNN 10
static char *usage[] = {
"elimin - eliminates those entries that are incorrectly classified by knn\n",
"Required parameters:\n",
" -din filename input data\n",
" -cout filename output codebook filename\n",
"Optional parameters:\n",
" -knn N use N nearest neighbours (default: 5)\n",
NULL};
struct entries *eliminate_codes(int knn, struct entries *data, WINNER_FUNCTION *find_knn)
{
long j, noe, correct, incorrect;
int datalabel;
struct data_entry tmp, *loca, *prev;
struct entries *datac;
struct winner_info *win;
eptr p;
if (knn > KNN) {
fprintf(stderr, "Can use only %d neighbors", KNN);
knn = KNN;
}
if ((datac = copy_entries(data)) == NULL)
{
return NULL;
}
if ((win = calloc(knn, sizeof(struct winner_info))) == NULL)
{
close_entries(datac);
return NULL;
}
/* Count the number of entries in data set */
/* Those entries are removed from datac that are not
correctly classified using the knn-classification */
loca = rewind_entries(data, &p);
noe = data->flags.totlen_known ? data->num_entries : 0;
prev = &tmp;
tmp.next = NULL;
while (loca != NULL) {
correct = 0;
incorrect = 0;
if (find_knn(data, loca, win, knn) != knn)
{
/* did not find winners */
}
else
{
datalabel = get_entry_label(loca);
/* Count the correctly classified items against
the incorrectly classified ones */
for (j = 0; j < knn; j++) {
if (get_entry_label(win[j].winner) == datalabel)
correct++;
else
incorrect++;
}
/* The entry is saved only if there are more correct hits
than there are incorrect ones (one hit is for sure because
the sample is compared to itself also) */
if (correct > incorrect) {
prev->next = copy_entry(datac, loca);
prev = prev->next;
datac->num_entries++;
datac->num_loaded++;
}
}
/* Consider the next entry */
loca = next_entry(&p);
ifverbose(1)
if (noe)
mprint((long) --noe);
}
ifverbose(1)
{
mprint(0);
fprintf(stderr, "\n");
}
datac->entries = tmp.next;
return(datac);
}
int main(int argc, char **argv)
{
int knn;
char *in_data_file;
char *out_code_file;
struct entries *data, *codes;
global_options(argc, argv);
if (extract_parameter(argc, argv, "-help", OPTION2))
{
printhelp();
exit(0);
}
in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS);
out_code_file = extract_parameter(argc, argv, OUT_CODE_FILE, ALWAYS);
knn = (int) oatoi(extract_parameter(argc, argv, KNN_NEIGHBORS, OPTION), 5);
ifverbose(2)
fprintf(stderr, "Input entries are read from file %s\n", in_data_file);
if ((data = open_entries(in_data_file)) == NULL)
{
fprintf(stderr, "Can't open data file '%s'\n", in_data_file);
exit(1);
}
ifverbose(2)
fprintf(stderr, "Extra codes are eliminated\n");
codes = eliminate_codes(knn, data, find_winner_knn);
close_entries(data);
if (codes == NULL)
{
fprintf(stderr, "Elimination failed!\n");
exit(1);
}
ifverbose(2)
fprintf(stderr, "Codebook entries are saved to file %s\n", out_code_file);
save_entries(codes, out_code_file);
invalidate_alphafile(out_code_file);
return(0);
}