21 #ifndef _XBOOST_TEST_H
22 #define _XBOOST_TEST_H
42 double pmargin, nmargin;
44 inline double Fpr()
const {
45 return double(FP) / double(TN + FP);
47 inline double ErrorRate()
const {
48 return double(FN+FP) / double(TP+TN+FN+FP);
52 return double(TP) / double(TP + FN);
54 inline double Precision()
const {
55 return double(TP)/double(FP+TP);
57 inline double Accuracy()
const {
58 return double(TP+TN)/double(TP+TN+FN+FP);
60 inline double Margin()
const {
61 return pmargin - nmargin;
64 void print(std::ostream & out)
66 out <<
"TP:" << TP <<
", TN:" << TN <<
", FN:" << FN <<
", FP:" << FP <<
'\n';
67 out <<
"Correct Detection: " << TP+TN <<
" (" << (100*(TP+TN))/(TP+TN+FN+FP) <<
"%), Errors: " << FN+FP <<
'\n';
68 out <<
"Accuracy: " << Accuracy()
69 <<
" Error Rate: " << ErrorRate()
70 <<
" Precision: " << Precision()
71 <<
" Recall: " <<
Recall() << std::endl;
80 template<
class ClassifierType,
class TrainingSet>
84 int n = train_set.Size();
85 double *response =
new double [n];
88 for(
int i =0; i<n; ++i)
90 response[i] = classifier.response( train_set.templates[i] );
91 th.insert(response[i]);
95 for( std::set<double>::const_iterator j = th.begin(); j != th.end(); ++j)
100 for(
int i =0; i<n; ++i)
102 int test = ((response[i] > (*j)) ? 1 : -1);
104 if( train_set.templates[i].category == 1)
117 out << (*j) <<
'\t' << TP <<
'\t' << TN <<
'\t' << FP <<
'\t' << FN <<
'\t' << (double)TP/(
double)(TP+FN) <<
'\t' << (
double)FP/(double)(TN+FP) <<
'\t' << (double)TP/(
double)(TP+FP) <<
'\n';
124 template<
class ClassifierType,
class TrainingSet>
128 int n = train_set.Size();
133 for(
int i =0; i<n; ++i)
135 double response = classifier.response( train_set.templates[i] );
136 int test = ((response > th) ? 1 : -1);
138 if( train_set.templates[i].category == 1)
151 std::cout <<
"TP: " <<TP <<
" | TN: " <<TN <<
" | FP: " << FP <<
" | FN: " << FN <<
" | TPR: " << (double)TP/(
double)(TP+FN) <<
" | FPR: " << (
double)FP/(double)(TN+FP) <<
" | Precision: " << (double)TP/(
double)(TP+FP) << std::endl;
155 template<
class DataType,
class ClassifierType>
158 unsigned int cd[2],er[2];
161 double pmargin = 1000000000.0;
162 double nmargin = -1000000000.0;
163 cd[0] = er[0] = cd[1] = er[1] = 0;
168 double *r =
new double[ data.Size() ];
170 for(
unsigned int i = 0; i<data.templates.size(); i++)
173 double ret = c( getData1(data.templates[i], data), getData2(data.templates[i], data) );
176 if(data.templates[i].category == 1)
210 pavg /= (double) (cd[0]+er[0]);
211 navg /= (double) (cd[1]+er[1]);
213 std::cout <<
"TP:" << cd[0] <<
", TN:" << cd[1] <<
", FN:" << er[0] <<
", FP:" << er[1] <<
'\n';
214 std::cout <<
"Correct Detection: " << cd[0]+cd[1] <<
" (" << (100*(cd[0]+cd[1]))/(cd[0]+cd[1]+er[0]+er[1]) <<
"%), Errors: " << er[0]+er[1] <<
'\n';
215 std::cout <<
"Accuracy: " << (float)(cd[0]+cd[1])/(float)(cd[0]+cd[1]+er[0]+er[1])
216 <<
" Error Rate: " << (float)(er[0]+er[1])/(float)(cd[0]+cd[1]+er[0]+er[1])
217 <<
" Precision: " << (float)(cd[0])/(float)(er[1]+cd[0])
218 <<
" Recall: " << (float)(cd[0])/(float)(er[0]+cd[0]) << std::endl;
220 std::cout <<
"Average: " << pavg <<
"(+) " << navg <<
"(-)\n";
223 std::cout <<
"Margin: " << pmargin-nmargin <<
'/' << 2*c.max_response() << std::endl;
228 for(
unsigned int i = 0; i<data.templates.size(); i++)
230 if(data.templates[i].category == 1 && r[i]<nmargin)
232 if(data.templates[i].category == -1 && r[i]>pmargin)
236 std::cout <<
"No separation: " << pmargin-nmargin <<
'/' << 2*c.max_response() <<
" (" << pm+nm <<
" pattern inside margin: " << pm <<
" +," << nm <<
" -)" << std::endl;
241 return (er[0]+er[1])==0;
248 template<
class DataType,
class ClassifierType>
249 void compute_response(
double *r,
const ClassifierType * c,
const DataType * data,
int s0,
int s1)
251 for(
int i = s0; i<s1; i++)
254 double ret = (*c)( getData1(data->templates[i], *data), getData2(data->templates[i], *data) );
264 template<
class DataType,
class ClassifierType>
267 unsigned int cd[2],er[2];
271 double pmargin = 1000000000.0;
272 double nmargin = -1000000000.0;
273 cd[0] = er[0] = cd[1] = er[1] = 0;
278 int n_samples = data.Size();
281 double *r =
new double[ n_samples ];
285 if(max_concurrent_jobs > 1)
289 for(
int k=0; k<max_concurrent_jobs; ++k)
291 int s0 = (k * n_samples) / max_concurrent_jobs;
292 int s1 = ((k+1) * n_samples) / max_concurrent_jobs;
294 thread_pool_.
create_thread(sprint::thread_bind(&detail::compute_response<DataType,ClassifierType>, r, &c, &data, s0, s1));
299 for(
int i = 0; i<n_samples; i++)
303 if(data.templates[i].category == 1)
339 #endif // #ifdef _MULTITHREAD
342 for(
int i = 0; i<n_samples; i++)
345 double ret = c( getData1(data.templates[i], data), getData2(data.templates[i], data) );
348 if(data.templates[i].category == 1)
384 pavg /= (double) (cd[0]+er[0]);
385 navg /= (double) (cd[1]+er[1]);
391 report.pmargin = pmargin;
392 report.nmargin = nmargin;
394 report.print(std::cout);
395 std::cout <<
"Average: " << pavg <<
"(+) " << navg <<
"(-)\n";
398 std::cout <<
"Margin: " << pmargin-nmargin <<
'/' << 2*c.max_response() << std::endl;
403 for(
unsigned int i = 0; i<data.templates.size(); i++)
405 if(data.templates[i].category == 1 && r[i]<nmargin)
407 if(data.templates[i].category == -1 && r[i]>pmargin)
411 std::cout <<
"No separation: " << pmargin-nmargin <<
'/' << 2*c.max_response() <<
" (" << pm+nm <<
" pattern inside margin: " << pm <<
" +," << nm <<
" -)" << std::endl;
void DumpRoc(std::ostream &out, const ClassifierType &classifier, const TrainingSet &train_set)
TODO Va bene per chi ha response Template.
Definition: Test.h:81
ReportTest ExportStat(const double *r, const int *category, int n_samples, double threshold)
void TestSimple(const ClassifierType &classifier, const TrainingSet &train_set, double th=0.0)
TODO Va bene per chi ha response Template.
Definition: Test.h:126
bool Test(const ClassifierType &c, const DataType &data, double threshold=0.0)
Definition: Test.h:156
ClassifierType
Definition: Types.h:31
Definition: thread_group.h:82
void join_all()
wait all threads terminate
Definition: thread_group.h:114
proposal 1 for thread group
double Recall() const
Recall/TruePositiveRate.
Definition: Test.h:51
method to create function pointer for thread call
bool create_thread(const sprint::thread_function &p)
create an additional thread
Definition: thread_group.h:102
ReportTest TestAndExportStat(const ClassifierType &c, const DataType &data, double threshold, int max_concurrent_jobs)
Definition: Test.h:265