📄 accuracy.cpp
字号:
/************************************************************************
* *
* Program package 'lvq_pak': *
* *
* accuracy.c *
* -computes the recognition accuracy by the nearest-neighbor rule *
* *
* 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 "header.h"
#include <stdio.h>
#include <float.h>
#include "lvq.h"
#include "matlvq.h"
#include "lvq_pak.h"
#include "tools.h"
#include "errors.h"
void accuracy_report(PLVQPARAMS params, struct teach_params *tparams);
static char *usage[] = {
"accuracy - computes the recognition accuracy by the nearest-neighbor rule\n",
"Required parameters:\n",
" -cin filename codebook file\n",
" -din filename test data\n",
"Optional parameters:\n",
" -cfout filename output classification file\n",
" -buffer integer buffered reading of data, integer lines at a time\n",
NULL};
int compute_accuracy(struct teach_params *teach, struct file_info *of)
{
long total, stotal, noc;
struct winner_info winner;
struct hitlist *correct, *totals;
struct hit_entry *he;
FILE *ocf;
int datalabel;
struct entries *data = teach->data;
struct entries *codes = teach->codes;
WINNER_FUNCTION *find_winner = teach->winner;
struct data_entry *datatmp;
eptr p;
ocf = of ? fi2fp(of) : NULL;
if ((correct = new_hitlist()) == NULL)
{
return ERR_NOMEM;
}
if ((totals = new_hitlist()) == NULL)
{
free_hitlist(correct);
return ERR_NOMEM;
}
stotal = 0;
total = 0;
if ((datatmp = rewind_entries(data, &p)) == NULL)
{
Msg(0, "compute_accuracy: can't get data");
goto end;
}
/* Number of data vectors */
noc = data->flags.totlen_known ? data->num_entries : 0;
/* Scan all input entries */
while (datatmp != NULL) {
find_winner(codes, datatmp, &winner, 1);
/* If classification was correct */
datalabel = get_entry_label(datatmp);
if (get_entry_label(winner.winner) == datalabel) {
/* Number of correct classifications */
stotal++;
/* Number of correct classifications in that class */
add_hit(correct, datalabel);
/* Write '1' to classification description file */
if (ocf != NULL) fprintf(ocf,"1\n");
} else {
/* Write '0' to classification description file */
if (ocf != NULL) fprintf(ocf,"0\n");
}
/*THE FOLLOWINF LINE HAS BEEN ADDED BY SR
Overwrite the label of the data entry : put the label of the class into which the entry
has been classified*/
set_entry_label(datatmp, get_entry_label(winner.winner));
/* Total number of entries in that class */
add_hit(totals, datalabel);
/* Total number of entries */
total++;
/* Take the next input entry */
datatmp = next_entry(&p);
ifverbose(1)
if (noc)
mprint((long) noc--);
}
ifverbose(1)
{
mprint((long) 0);
}
#ifdef SAMI /*Comments (SAMI is not defined)*/
Msg(0, "Recognition accuracy:");
for (he = totals->head; he != NULL; he = he->next)
{
long res, tot;
tot = he->freq;
res = hitlist_label_freq(correct, he->label);
Msg(0, "%9s: %4d entries ", find_conv_to_lab(he->label), tot);
Msg(0, "%6.2f %%\n", 100.0 * (float) res / tot);
}
Msg(0, "\nTotal accuracy: %5ld entries %6.2f %%\n\n", total,
100.0 * (float) stotal / total);
#endif
end:
free_hitlist(correct);
free_hitlist(totals);
return 0;
}
int accuracy(PLVQPARAMS params)
{
int ret;
struct teach_params tparams;
LVQParamstoTeach(&tparams, params); /*Initialize the teach_param struct*/
ret = compute_accuracy(&tparams, NULL);
if (ret < 0)
return ret;
accuracy_report(params, &tparams);
return ret;
}
void accuracy_report(PLVQPARAMS params, struct teach_params *tparams)
{
int c, c2;
long lRow, lCol, lFace;
struct data_entry *datatmp;
int num_class = NumClass();
float val;
eptr p;
char str[MAX_NAME];
if (params->pconfusion != NULL)
delete params->pconfusion;
params->pconfusion = new CMatrixSym(num_class);
params->pconfusion->Initialize();
/*Take the first entry*/
if ((datatmp = rewind_entries(tparams->data, &p)) == NULL)
{
Msg(0, "compute_accuracy: can't get data");
return;
}
lFace = 1;
params->pconfusion->Lock();
while (datatmp != NULL)
{
params->pdata->GetColName(lFace, str, sizeof(str));
lRow = GetLabelInt(str);
lCol = get_entry_label(datatmp);
if (lRow > 0 && lCol > 0 && lRow <= num_class && lCol <= num_class)
{
val = params->pconfusion->GetAt(lRow, lCol);
params->pconfusion->SetAt(lRow, lCol, val+1);
}
/* Take the next input entry */
datatmp = next_entry(&p);
lFace++;
}
params->pconfusion->Unlock();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -