00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <iostream>
00019 #include <fstream>
00020 #include <string>
00021 #include <sstream>
00022 #include <cmath>
00023
00024
00025
00026 #include "PatternRecognitionUtilities.h"
00027
00028 namespace Flood
00029 {
00030
00031
00032
00035
00036 PatternRecognitionUtilities::PatternRecognitionUtilities(void)
00037 {
00038 multilayer_perceptron_pointer = NULL;
00039 input_target_data_set_pointer = NULL;
00040
00041 display = true;
00042 }
00043
00044
00045
00046
00050
00051 PatternRecognitionUtilities::PatternRecognitionUtilities(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00052 {
00053 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00054 input_target_data_set_pointer = NULL;
00055
00056 display = true;
00057 }
00058
00059
00060
00061
00065
00066 PatternRecognitionUtilities::PatternRecognitionUtilities(InputTargetDataSet* new_input_target_data_set_pointer)
00067 {
00068 multilayer_perceptron_pointer = NULL;
00069 input_target_data_set_pointer = new_input_target_data_set_pointer;
00070
00071 display = true;
00072 }
00073
00074
00075
00076
00081
00082 PatternRecognitionUtilities::PatternRecognitionUtilities
00083 (MultilayerPerceptron* new_multilayer_perceptron_pointer, InputTargetDataSet* new_input_target_data_set_pointer)
00084 {
00085 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00086 input_target_data_set_pointer = new_input_target_data_set_pointer;
00087
00088 display = true;
00089 }
00090
00091
00092
00093
00095
00096 PatternRecognitionUtilities::~PatternRecognitionUtilities(void)
00097 {
00098 }
00099
00100
00101
00102
00103
00104
00106
00107 MultilayerPerceptron* PatternRecognitionUtilities::get_multilayer_perceptron_pointer(void)
00108 {
00109 return(multilayer_perceptron_pointer);
00110 }
00111
00112
00113
00114
00116
00117 InputTargetDataSet* PatternRecognitionUtilities::get_input_target_data_set_pointer(void)
00118 {
00119 return(input_target_data_set_pointer);
00120 }
00121
00122
00123
00124
00127
00128 bool PatternRecognitionUtilities::get_display(void)
00129 {
00130 return(display);
00131 }
00132
00133
00134
00135
00138
00139 void PatternRecognitionUtilities::
00140 set_multilayer_perceptron_pointer(MultilayerPerceptron* new_multilayer_perceptron_pointer)
00141 {
00142 multilayer_perceptron_pointer = new_multilayer_perceptron_pointer;
00143 }
00144
00145
00146
00147
00150
00151 void PatternRecognitionUtilities
00152 ::set_input_target_data_set_pointer(InputTargetDataSet* new_input_target_data_set_pointer)
00153 {
00154 input_target_data_set_pointer = new_input_target_data_set_pointer;
00155 }
00156
00157
00158
00159
00164
00165 void PatternRecognitionUtilities::set_display(bool new_display)
00166 {
00167 display = new_display;
00168 }
00169
00170
00171
00172
00179
00180 void PatternRecognitionUtilities::generate_artificial_data
00181 (const Vector< Vector<double> >& mean, const Vector< Vector<double> >& standard_deviation, const Vector<double>& prior_probability)
00182 {
00183
00184
00185 int instances_number = input_target_data_set_pointer->get_instances_number();
00186 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00187 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00188
00189 int prior_probability_size = prior_probability.get_size();
00190
00191
00192
00193 #ifdef _DEBUG
00194
00195 int mean_size = mean.get_size();
00196 int standard_deviation_size = standard_deviation.get_size();
00197
00198 if(target_variables_number == 1)
00199 {
00200 if(mean_size != 2)
00201 {
00202 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00203 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00204 << "Size of mean must be two." << std::endl;
00205
00206 exit(1);
00207 }
00208 else if(standard_deviation_size != 2)
00209 {
00210 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00211 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00212 << "Size of standard deviation must be two." << std::endl;
00213
00214 exit(1);
00215 }
00216 else if(prior_probability_size != 2)
00217 {
00218 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00219 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00220 << "Size of prior probability must be two." << std::endl;
00221
00222 exit(1);
00223 }
00224 }
00225 else
00226 {
00227 if(mean_size != target_variables_number)
00228 {
00229 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00230 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00231 << "Size of mean must be equal to number of target variables." << std::endl;
00232
00233 exit(1);
00234 }
00235 else if(standard_deviation_size != target_variables_number)
00236 {
00237 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00238 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00239 << "Size of standard deviation must be equal to number of target variables." << std::endl;
00240
00241 exit(1);
00242 }
00243 else if(prior_probability_size != target_variables_number)
00244 {
00245 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00246 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00247 << "Size of prior probability must be equal to number of target variables." << std::endl;
00248
00249 exit(1);
00250 }
00251 }
00252
00253 double sum = prior_probability.calculate_sum();
00254
00255 if(sum != 1.0)
00256 {
00257 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00258 << "void generate_artificial_data(const Vector< Vector<double> >&, const Vector< Vector<double> >&, const Vector<double>&) method." << std::endl
00259 << "Sum of prior probabilities must one." << std::endl;
00260
00261 exit(1);
00262 }
00263
00264 #endif
00265
00266 Vector<double> input_instance(input_variables_number);
00267 Vector<double> target_instance(target_variables_number);
00268
00269 double random;
00270
00271
00272
00273 Vector<double> cumulative_prior_probability(prior_probability_size);
00274
00275 cumulative_prior_probability[0] = prior_probability[0];
00276
00277 for(int i = 1; i < prior_probability_size; i++)
00278 {
00279 cumulative_prior_probability[i] = cumulative_prior_probability[i-1] + prior_probability[i];
00280 }
00281
00282
00283 for(int i = 0; i < instances_number; i++)
00284 {
00285 if(target_variables_number == 1)
00286 {
00287 random = (double)rand()/(RAND_MAX+1.0);
00288
00289 if(random < cumulative_prior_probability[0])
00290 {
00291 input_instance.initialize_normal(mean[0], standard_deviation[0]);
00292 target_instance[0] = 1.0;
00293 }
00294 else
00295 {
00296 input_instance.initialize_normal(mean[1], standard_deviation[1]);
00297 target_instance[0] = 0.0;
00298 }
00299 }
00300 else
00301 {
00302
00303 }
00304
00305 input_target_data_set_pointer->set_input_instance(i, input_instance);
00306 input_target_data_set_pointer->set_target_instance(i, target_instance);
00307 }
00308 }
00309
00310
00311
00312
00315
00316 Matrix<bool> PatternRecognitionUtilities::get_binary_target_data(void)
00317 {
00318
00319
00320 #ifdef _DEBUG
00321
00322 if(input_target_data_set_pointer == NULL)
00323 {
00324 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00325 << "Matrix<bool> get_binary_target_data(void) method." << std::endl
00326 << "Input-target data set object cannot be null." << std::endl;
00327
00328 exit(1);
00329 }
00330
00331 #endif
00332
00333
00334
00335 int instances_number = input_target_data_set_pointer->get_instances_number();
00336
00337 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00338
00339 #ifdef _DEBUG
00340
00341 if(instances_number == 0)
00342 {
00343 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00344 << "Matrix<bool> get_binary_target_data(void) method." << std::endl
00345 << "Number of instances must be greater than zero." << std::endl;
00346
00347 exit(1);
00348 }
00349
00350 if(target_variables_number == 0)
00351 {
00352 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00353 << "Matrix<bool> get_binary_target_data(void) method." << std::endl
00354 << "Number of target variables must be greater than zero." << std::endl;
00355
00356 exit(1);
00357 }
00358
00359 #endif
00360
00361
00362
00363
00364 Matrix<bool> target_data(instances_number, target_variables_number, false);
00365
00366 Vector<double> target(target_variables_number);
00367
00368 if(target_variables_number == 1)
00369 {
00370 for(int i = 0; i < instances_number; i++)
00371 {
00372 target = input_target_data_set_pointer->get_target_instance(i);
00373
00374 if(target[0] > 0.5)
00375 {
00376 target_data[i][0] = true;
00377 }
00378 }
00379 }
00380 else if(target_variables_number > 1)
00381 {
00382 int winner_index;
00383
00384 for(int i = 0; i < instances_number; i++)
00385 {
00386 target = input_target_data_set_pointer->get_target_instance(i);
00387
00388 winner_index = target.calculate_maximal_index();
00389
00390 target_data[i][winner_index] = true;
00391 }
00392 }
00393
00394 return(target_data);
00395 }
00396
00397
00398
00399
00402
00403 Matrix<bool> PatternRecognitionUtilities::get_binary_training_target_data(void)
00404 {
00405
00406
00407 #ifdef _DEBUG
00408
00409 if(input_target_data_set_pointer == NULL)
00410 {
00411 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00412 << "Matrix<bool> get_binary_training_target_data(void) method." << std::endl
00413 << "Input-target data set object cannot be null." << std::endl;
00414
00415 exit(1);
00416 }
00417
00418 #endif
00419
00420
00421
00422 int training_instances_number = input_target_data_set_pointer->get_training_instances_number();
00423
00424 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00425
00426 #ifdef _DEBUG
00427
00428 if(training_instances_number == 0)
00429 {
00430 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00431 << "Matrix<bool> get_binary_training_target_data(void) method." << std::endl
00432 << "Number of training instances must be greater than zero." << std::endl;
00433
00434 exit(1);
00435 }
00436
00437 if(target_variables_number == 0)
00438 {
00439 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00440 << "Matrix<bool> get_binary_training_target_data(void) method." << std::endl
00441 << "Number of target variables must be greater than zero." << std::endl;
00442
00443 exit(1);
00444 }
00445
00446 #endif
00447
00448
00449
00450
00451 Matrix<bool> training_target_data(training_instances_number, target_variables_number, false);
00452
00453 Vector<double> target(target_variables_number);
00454
00455 if(target_variables_number == 1)
00456 {
00457 for(int i = 0; i < training_instances_number; i++)
00458 {
00459 target = input_target_data_set_pointer->get_training_target_instance(i);
00460
00461 if(target[0] > 0.5)
00462 {
00463 training_target_data[i][0] = true;
00464 }
00465 }
00466 }
00467 else if(target_variables_number > 1)
00468 {
00469 int winner_index;
00470
00471 for(int i = 0; i < training_instances_number; i++)
00472 {
00473 target = input_target_data_set_pointer->get_training_target_instance(i);
00474
00475 winner_index = target.calculate_maximal_index();
00476
00477 training_target_data[i][winner_index] = true;
00478 }
00479 }
00480
00481 return(training_target_data);
00482 }
00483
00484
00485
00486
00489
00490 Matrix<bool> PatternRecognitionUtilities::get_binary_validation_target_data(void)
00491 {
00492
00493
00494 #ifdef _DEBUG
00495
00496 if(input_target_data_set_pointer == NULL)
00497 {
00498 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00499 << "Matrix<bool> get_binary_validation_target_data(void) method." << std::endl
00500 << "Input-target data set object cannot be null." << std::endl;
00501
00502 exit(1);
00503 }
00504
00505 #endif
00506
00507
00508
00509 int validation_instances_number = input_target_data_set_pointer->get_validation_instances_number();
00510
00511 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00512
00513 #ifdef _DEBUG
00514
00515 if(validation_instances_number == 0)
00516 {
00517 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00518 << "Matrix<bool> get_binary_validation_target_data(void) method." << std::endl
00519 << "Number of validation instances must be greater than zero." << std::endl;
00520
00521 exit(1);
00522 }
00523
00524 if(target_variables_number == 0)
00525 {
00526 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00527 << "Matrix<bool> get_binary_validation_target_data(void) method." << std::endl
00528 << "Number of target variables must be greater than zero." << std::endl;
00529
00530 exit(1);
00531 }
00532
00533 #endif
00534
00535 Matrix<bool> validation_target_data(validation_instances_number, target_variables_number, false);
00536
00537 Vector<double> target(target_variables_number);
00538
00539 if(target_variables_number == 1)
00540 {
00541 for(int i = 0; i < validation_instances_number; i++)
00542 {
00543 target = input_target_data_set_pointer->get_validation_target_instance(i);
00544
00545 if(target[0] > 0.5)
00546 {
00547 validation_target_data[i][0] = true;
00548 }
00549 }
00550 }
00551 else if(target_variables_number > 1)
00552 {
00553 int winner_index;
00554
00555 for(int i = 0; i < validation_instances_number; i++)
00556 {
00557 target = input_target_data_set_pointer->get_validation_target_instance(i);
00558
00559 winner_index = target.calculate_maximal_index();
00560
00561 validation_target_data[i][winner_index] = true;
00562 }
00563 }
00564
00565 return(validation_target_data);
00566 }
00567
00568
00569
00570
00573
00574 Matrix<bool> PatternRecognitionUtilities::get_binary_testing_target_data(void)
00575 {
00576
00577
00578 #ifdef _DEBUG
00579
00580 if(input_target_data_set_pointer == NULL)
00581 {
00582 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00583 << "Matrix<bool> get_binary_testing_target_data(void) method." << std::endl
00584 << "Input-target data set object cannot be null." << std::endl;
00585
00586 exit(1);
00587 }
00588
00589 #endif
00590
00591
00592
00593 int testing_instances_number = input_target_data_set_pointer->get_testing_instances_number();
00594
00595 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00596
00597
00598
00599 #ifdef _DEBUG
00600
00601 if(testing_instances_number == 0)
00602 {
00603 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00604 << "Matrix<bool> get_binary_testing_target_data(void) method." << std::endl
00605 << "Number of testing instances must be greater than zero." << std::endl;
00606
00607 exit(1);
00608 }
00609
00610 if(target_variables_number == 0)
00611 {
00612 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00613 << "Matrix<bool> get_binary_testing_target_data(void) method." << std::endl
00614 << "Number of target var must be greater than zero." << std::endl;
00615
00616 exit(1);
00617 }
00618
00619 #endif
00620
00621 Matrix<bool> testing_target_data(testing_instances_number, target_variables_number, false);
00622
00623 Vector<double> target(target_variables_number);
00624
00625 if(target_variables_number == 1)
00626 {
00627 for(int i = 0; i < testing_instances_number; i++)
00628 {
00629 target = input_target_data_set_pointer->get_testing_target_instance(i);
00630
00631 if(target[0] > 0.5)
00632 {
00633 testing_target_data[i][0] = true;
00634 }
00635 }
00636 }
00637 else if(target_variables_number > 1)
00638 {
00639 int winner_index;
00640
00641 for(int i = 0; i < testing_instances_number; i++)
00642 {
00643 target = input_target_data_set_pointer->get_testing_target_instance(i);
00644
00645 winner_index = target.calculate_maximal_index();
00646
00647 testing_target_data[i][winner_index] = true;
00648 }
00649 }
00650
00651 return(testing_target_data);
00652 }
00653
00654
00655
00656
00659
00660 Matrix<bool> PatternRecognitionUtilities::calculate_binary_output_data(void)
00661 {
00662
00663
00664 #ifdef _DEBUG
00665
00666 if(input_target_data_set_pointer == NULL)
00667 {
00668 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00669 << "Matrix<bool> calculate_binary_output_data(void) method." << std::endl
00670 << "Input-target data set object cannot be null." << std::endl;
00671
00672 exit(1);
00673 }
00674
00675 #endif
00676
00677 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00678 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00679
00680 #ifdef _DEBUG
00681
00682 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00683 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00684
00685
00686
00687 if(inputs_number != input_variables_number)
00688 {
00689 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00690 << "Matrix<bool> calculate_binary_output_data(void) method." << std::endl
00691 << "Number of inputs in multilayer perceptron "
00692 << "must be equal to number of input variables in the data set." << std::endl;
00693
00694 exit(1);
00695 }
00696 else if(outputs_number != target_variables_number)
00697 {
00698 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00699 << "Matrix<bool> calculate_binary_output_data(void) method." << std::endl
00700 << "Number of outputs in multilayer perceptron " << std::endl
00701 << "must be equal to number of target variables in the data set." << std::endl;
00702
00703 exit(1);
00704 }
00705
00706 #endif
00707
00708
00709
00710 int instances_number = input_target_data_set_pointer->get_instances_number();
00711
00712 Matrix<bool> output_data(instances_number, outputs_number, false);
00713
00714 Vector<double> input(inputs_number);
00715 Vector<double> output(outputs_number);
00716
00717 if(outputs_number == 1)
00718 {
00719 for(int i = 0; i < instances_number; i++)
00720 {
00721 input = input_target_data_set_pointer->get_input_instance(i);
00722
00723 output = multilayer_perceptron_pointer->calculate_output(input);
00724
00725 if(output[0] > 0.5)
00726 {
00727 output_data[i][0] = true;
00728 }
00729 }
00730 }
00731 else
00732 {
00733 int winner_index;
00734
00735 for(int i = 0; i < instances_number; i++)
00736 {
00737 input = input_target_data_set_pointer->get_input_instance(i);
00738
00739 output = multilayer_perceptron_pointer->calculate_output(input);
00740
00741 winner_index = output.calculate_maximal_index();
00742
00743 output_data[i][winner_index] = true;
00744 }
00745 }
00746
00747 return(output_data);
00748 }
00749
00750
00751
00752
00755
00756 Matrix<bool> PatternRecognitionUtilities::calculate_binary_training_output_data(void)
00757 {
00758
00759
00760 #ifdef _DEBUG
00761
00762 if(input_target_data_set_pointer == NULL)
00763 {
00764 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00765 << "Matrix<bool> calculate_binary_training_output_data(void) method." << std::endl
00766 << "Input-target data set object cannot be null." << std::endl;
00767
00768 exit(1);
00769 }
00770
00771 #endif
00772
00773 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00774 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00775
00776 #ifdef _DEBUG
00777
00778 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00779 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00780
00781
00782
00783 if(inputs_number != input_variables_number)
00784 {
00785 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00786 << "Matrix<bool> calculate_binary_training_output_data(void) method." << std::endl
00787 << "Number of inputs in multilayer perceptron "
00788 << "must be equal to number of input variables in training data set." << std::endl;
00789
00790 exit(1);
00791 }
00792 else if(outputs_number != target_variables_number)
00793 {
00794 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00795 << "Matrix<bool> calculate_binary_output_data(void) method." << std::endl
00796 << "Number of outputs in multilayer perceptron " << std::endl
00797 << "must be equal to number of target variables in training data set." << std::endl;
00798
00799 exit(1);
00800 }
00801
00802 #endif
00803
00804
00805
00806 int training_instances_number = input_target_data_set_pointer->get_training_instances_number();
00807
00808 Matrix<bool> training_output_data(training_instances_number, outputs_number, false);
00809
00810 Vector<double> input(inputs_number);
00811 Vector<double> output(outputs_number);
00812
00813 if(outputs_number == 1)
00814 {
00815 for(int i = 0; i < training_instances_number; i++)
00816 {
00817 input = input_target_data_set_pointer->get_training_input_instance(i);
00818
00819 output = multilayer_perceptron_pointer->calculate_output(input);
00820
00821 if(output[0] > 0.5)
00822 {
00823 training_output_data[i][0] = true;
00824 }
00825 }
00826 }
00827 else
00828 {
00829 int winner_index;
00830
00831 for(int i = 0; i < training_instances_number; i++)
00832 {
00833 input = input_target_data_set_pointer->get_training_input_instance(i);
00834
00835 output = multilayer_perceptron_pointer->calculate_output(input);
00836
00837 winner_index = output.calculate_maximal_index();
00838
00839 training_output_data[i][winner_index] = true;
00840 }
00841 }
00842
00843 return(training_output_data);
00844 }
00845
00846
00847
00848
00849
00852
00853 Matrix<bool> PatternRecognitionUtilities::calculate_binary_validation_output_data(void)
00854 {
00855
00856
00857 #ifdef _DEBUG
00858
00859 if(input_target_data_set_pointer == NULL)
00860 {
00861 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00862 << "Matrix<bool> calculate_binary_validation_output_data(void) method." << std::endl
00863 << "Input-target data set object cannot be null." << std::endl;
00864
00865 exit(1);
00866 }
00867
00868 #endif
00869
00870 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00871 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00872
00873 #ifdef _DEBUG
00874
00875 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00876 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00877
00878
00879
00880 if(inputs_number != input_variables_number)
00881 {
00882 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00883 << "Matrix<bool> calculate_binary_validation_output_data(void) method." << std::endl
00884 << "Number of inputs in multilayer perceptron "
00885 << "must be equal to number of input variables in validation data set."
00886 << std::endl;
00887
00888 exit(1);
00889 }
00890 else if(outputs_number != target_variables_number)
00891 {
00892 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00893 << "Matrix<bool> calculate_binary_validation_output_data(void) method." << std::endl
00894 << "Number of outputs in multilayer perceptron " << std::endl
00895 << "must be equal to number of target variables in validation data set."
00896 << std::endl;
00897
00898 exit(1);
00899 }
00900
00901 #endif
00902
00903
00904
00905 int validation_instances_number = input_target_data_set_pointer->get_validation_instances_number();
00906
00907 Matrix<bool> validation_output_data(validation_instances_number, outputs_number, false);
00908
00909 Vector<double> input(inputs_number);
00910 Vector<double> output(outputs_number);
00911
00912 if(outputs_number == 1)
00913 {
00914 for(int i = 0; i < validation_instances_number; i++)
00915 {
00916 input = input_target_data_set_pointer->get_validation_input_instance(i);
00917
00918 output = multilayer_perceptron_pointer->calculate_output(input);
00919
00920 if(output[0] > 0.5)
00921 {
00922 validation_output_data[i][0] = true;
00923 }
00924 }
00925 }
00926 else
00927 {
00928 int winner_index;
00929
00930 for(int i = 0; i < validation_instances_number; i++)
00931 {
00932 input = input_target_data_set_pointer->get_validation_input_instance(i);
00933
00934 output = multilayer_perceptron_pointer->calculate_output(input);
00935
00936 winner_index = output.calculate_maximal_index();
00937
00938 validation_output_data[i][winner_index] = true;
00939 }
00940 }
00941
00942 return(validation_output_data);
00943 }
00944
00945
00946
00947
00950
00951 Matrix<bool> PatternRecognitionUtilities::calculate_binary_testing_output_data(void)
00952 {
00953
00954
00955 #ifdef _DEBUG
00956
00957 if(input_target_data_set_pointer == NULL)
00958 {
00959 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00960 << "Matrix<bool> calculate_binary_testing_output_data(void) method." << std::endl
00961 << "Input-target data set object cannot be null." << std::endl;
00962
00963 exit(1);
00964 }
00965
00966 #endif
00967
00968 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
00969 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
00970
00971 #ifdef _DEBUG
00972
00973 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
00974 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
00975
00976
00977
00978 if(inputs_number != input_variables_number)
00979 {
00980 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00981 << "Matrix<bool> calculate_binary_testing_output_data(void) method." << std::endl
00982 << "Number of inputs in multilayer perceptron "
00983 << "must be equal to number of input variables in testing data set."
00984 << std::endl;
00985
00986 exit(1);
00987 }
00988 else if(outputs_number != target_variables_number)
00989 {
00990 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
00991 << "Matrix<bool> calculate_binary_testing_output_data(void) method." << std::endl
00992 << "Number of outputs in multilayer perceptron " << std::endl
00993 << "must be equal to number of target variables in testing data set."
00994 << std::endl;
00995
00996 exit(1);
00997 }
00998
00999 #endif
01000
01001
01002
01003 int testing_instances_number = input_target_data_set_pointer->get_testing_instances_number();
01004
01005 Matrix<bool> testing_output_data(testing_instances_number, outputs_number, false);
01006
01007 Vector<double> input(inputs_number);
01008 Vector<double> output(outputs_number);
01009
01010 if(outputs_number == 1)
01011 {
01012 for(int i = 0; i < testing_instances_number; i++)
01013 {
01014 input = input_target_data_set_pointer->get_testing_input_instance(i);
01015
01016 output = multilayer_perceptron_pointer->calculate_output(input);
01017
01018 if(output[0] > 0.5)
01019 {
01020 testing_output_data[i][0] = true;
01021 }
01022 }
01023 }
01024 else
01025 {
01026 int winner_index;
01027
01028 for(int i = 0; i < testing_instances_number; i++)
01029 {
01030 input = input_target_data_set_pointer->get_testing_input_instance(i);
01031
01032 output = multilayer_perceptron_pointer->calculate_output(input);
01033
01034 winner_index = output.calculate_maximal_index();
01035
01036 testing_output_data[i][winner_index] = true;
01037 }
01038 }
01039
01040 return(testing_output_data);
01041 }
01042
01043
01044
01045
01050
01051 Vector<int> PatternRecognitionUtilities::calculate_class_distribution(void)
01052 {
01053
01054
01055 #ifdef _DEBUG
01056
01057 if(input_target_data_set_pointer == NULL)
01058 {
01059 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01060 << "Vector<int> calculate_class_distribution(void) method." << std::endl
01061 << "Input-target data set object cannot be null." << std::endl;
01062
01063 exit(1);
01064 }
01065
01066 #endif
01067
01068 int instances_number = input_target_data_set_pointer->get_instances_number();
01069 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
01070
01071 Matrix<bool> binary_target_data = get_binary_target_data();
01072
01073 Vector<int> class_distribution;
01074
01075 if(target_variables_number == 1)
01076 {
01077 class_distribution.set(2, 0);
01078
01079 for(int i = 0; i < instances_number; i++)
01080 {
01081 if(binary_target_data[i][0])
01082 {
01083 class_distribution[0]++;
01084 }
01085 else
01086 {
01087 class_distribution[1]++;
01088 }
01089 }
01090 }
01091 else
01092 {
01093 class_distribution.set(target_variables_number, 0);
01094
01095 for(int i = 0; i < instances_number; i++)
01096 {
01097 for(int j = 0; j < target_variables_number; j++)
01098 {
01099 if(binary_target_data[i][j])
01100 {
01101 class_distribution[j]++;
01102 }
01103 }
01104 }
01105 }
01106
01107 return(class_distribution);
01108 }
01109
01110
01111
01112
01115
01116 std::string PatternRecognitionUtilities::get_class_distribution_XML(bool show_declaration)
01117 {
01118
01119
01120 #ifdef _DEBUG
01121
01122 if(input_target_data_set_pointer == NULL)
01123 {
01124 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01125 << "std::string get_class_distribution_XML(bool) method." << std::endl
01126 << "Input-target data set object cannot be null." << std::endl;
01127
01128 exit(1);
01129 }
01130
01131 #endif
01132
01133 std::stringstream buffer;
01134
01135 if(show_declaration)
01136 {
01137 buffer << "<Flood version='3.0' class='PatternRecognitionUtilities' content='ClassDistribution'>" << std::endl;
01138 }
01139
01140 int instances_number = input_target_data_set_pointer->get_instances_number();
01141 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
01142
01143 Vector<int> class_distribution = calculate_class_distribution();
01144
01145 buffer << "<InstancesNumber>" << std::endl
01146 << instances_number << std::endl
01147 << "</InstancesNumber>" << std::endl;
01148
01149 if(target_variables_number == 1)
01150 {
01151 buffer << "<TrueInstancesNumber>" << std::endl
01152 << class_distribution[0] << std::endl
01153 << "</TrueInstancesNumber>" << std::endl;
01154
01155 buffer << "<FalseInstancesNumber>" << std::endl
01156 << class_distribution[1] << std::endl
01157 << "</FalseInstancesNumber>" << std::endl;
01158 }
01159 else
01160 {
01161 for(int i = 0; i < target_variables_number; i++)
01162 {
01163 buffer << "<ClassInstancesNumber>" << std::endl
01164 << class_distribution[i] << std::endl
01165 << "</ClassInstancesNumber>" << std::endl;
01166 }
01167 }
01168
01169 return(buffer.str());
01170 }
01171
01172
01173
01174
01177
01178 void PatternRecognitionUtilities::print_class_distribution(void)
01179 {
01180 std::cout << get_class_distribution_XML(true);
01181 }
01182
01183
01184
01185
01189
01190 void PatternRecognitionUtilities::save_class_distribution(const char* filename)
01191 {
01192 std::fstream file;
01193
01194 file.open(filename, std::ios::out);
01195
01196 if(!file.is_open())
01197 {
01198 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01199 << "void save_class_distribution(const char*) method." << std::endl
01200 << "Cannot open class distribution data file." << std::endl;
01201
01202 exit(1);
01203 }
01204
01205 file << get_class_distribution_XML(true);
01206
01207 file.close();
01208 }
01209
01210
01211
01212
01214
01215 Matrix<int> PatternRecognitionUtilities::calculate_confusion(void)
01216 {
01217
01218
01219 #ifdef _DEBUG
01220
01221 if(input_target_data_set_pointer == NULL)
01222 {
01223 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01224 << "Matrix<int> calculate_confusion(void) method." << std::endl
01225 << "Input-target data set object cannot be null." << std::endl;
01226
01227 exit(1);
01228 }
01229
01230 #endif
01231
01232 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
01233
01234 #ifdef _DEBUG
01235
01236 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
01237
01238 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
01239 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
01240
01241
01242
01243 if(inputs_number != input_variables_number)
01244 {
01245 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01246 << "Matrix<int> calculate_confusion(void) method." << std::endl
01247 << "Number of inputs in multilayer perceptron "
01248 << "must be equal to number of input variables in input target data set."
01249 << std::endl;
01250
01251 exit(1);
01252 }
01253
01254 if(outputs_number != target_variables_number)
01255 {
01256 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01257 << "Matrix<int> calculate_confusion(void) method." << std::endl
01258 << "Number of outputs in multilayer perceptron "
01259 << "must be equal to number of target variables in input target data set."
01260 << std::endl;
01261
01262 exit(1);
01263 }
01264
01265 #endif
01266
01267 int testing_instances_number = input_target_data_set_pointer->get_testing_instances_number();
01268
01269 Matrix<bool> binary_testing_target_data = get_binary_testing_target_data();
01270 Matrix<bool> binary_testing_output_data = calculate_binary_testing_output_data();
01271
01272 if(outputs_number == 1)
01273 {
01274 Matrix<int> confusion(2, 2);
01275 int true_positive = 0;
01276 int false_positive = 0;
01277 int false_negative = 0;
01278 int true_negative = 0;
01279
01280 for(int i = 0; i < testing_instances_number; i++)
01281 {
01282 if(binary_testing_output_data[0][i] == true && binary_testing_target_data[0][i] == true)
01283 {
01284 true_positive++;
01285 }
01286 else if(binary_testing_output_data[0][i] == true && binary_testing_target_data[0][i] == false)
01287 {
01288 false_positive++;
01289 }
01290 else if(binary_testing_output_data[0][i] == false && binary_testing_target_data[0][i] == true)
01291 {
01292 false_negative++;
01293 }
01294 else if(binary_testing_output_data[0][i] == false && binary_testing_target_data[0][i] == false)
01295 {
01296 true_negative++;
01297 }
01298 else
01299 {
01300 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01301 << "Matrix<int> calculate_confusion(void) method." << std::endl
01302 << "Unknown condition." << std::endl;
01303
01304 exit(1);
01305 }
01306
01307 confusion[0][0] = true_positive;
01308 confusion[0][1] = false_positive;
01309 confusion[1][0] = false_negative;
01310 confusion[1][1] = true_negative;
01311 }
01312
01313 return(confusion);
01314 }
01315 else
01316 {
01317 Matrix<int> confusion(outputs_number, outputs_number, 0);
01318
01319 int predicted_class = 0;
01320 int actual_class = 0;
01321
01322 for(int i = 0; i < testing_instances_number; i++)
01323 {
01324 for(int j = 0; j < outputs_number; j++)
01325 {
01326 if(binary_testing_output_data[i][j] == true)
01327 {
01328 predicted_class = j;
01329 }
01330 if(binary_testing_target_data[i][j] == true)
01331 {
01332 actual_class = j;
01333 }
01334 }
01335
01336 confusion[predicted_class][actual_class] += 1;
01337 }
01338
01339 return(confusion);
01340 }
01341 }
01342
01343
01344
01345
01348
01349 std::string PatternRecognitionUtilities::get_confusion_XML(bool show_declaration)
01350 {
01351 std::stringstream buffer;
01352
01353
01354
01355 if(show_declaration)
01356 {
01357 buffer << "<Flood version='3.0' class='InputTargetDataSet' content='Confusion'>" << std::endl;
01358 }
01359
01360 Matrix<int> confusion = calculate_confusion();
01361
01362 buffer << "<Confusion>" << std::endl
01363 << confusion
01364 << "</Confusion>" << std::endl;
01365
01366 return(buffer.str());
01367 }
01368
01369
01370
01371
01373
01374 void PatternRecognitionUtilities::print_confusion(void)
01375 {
01376 std::cout << get_confusion_XML(true);
01377 }
01378
01379
01380
01381
01384
01385 void PatternRecognitionUtilities::save_confusion(const char* filename)
01386 {
01387 std::fstream file;
01388
01389 file.open(filename, std::ios::out);
01390
01391 if(!file.is_open())
01392 {
01393 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01394 << "void save_confusion(const char*) method." << std::endl
01395 << "Cannot open confusion XML-type file." << std::endl;
01396
01397 exit(1);
01398 }
01399
01400 file << get_confusion_XML(true);
01401
01402 file.close();
01403 }
01404
01405
01406
01407
01419
01420 Vector<double> PatternRecognitionUtilities::calculate_binary_classification_test(void)
01421 {
01422
01423
01424 #ifdef _DEBUG
01425
01426 int inputs_number = multilayer_perceptron_pointer->get_inputs_number();
01427
01428 if(input_target_data_set_pointer == NULL)
01429 {
01430 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01431 << "Vector<double> calculate_binary_classification_test(void)." << std::endl
01432 << "Input-target data set object cannot be null." << std::endl;
01433
01434 exit(1);
01435 }
01436
01437 int input_variables_number = input_target_data_set_pointer->get_input_variables_number();
01438 int target_variables_number = input_target_data_set_pointer->get_target_variables_number();
01439
01440 int outputs_number = multilayer_perceptron_pointer->get_outputs_number();
01441
01442
01443
01444 if(inputs_number != input_variables_number)
01445 {
01446 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01447 << "Vector<double> calculate_binary_classification_test(void)." << std::endl
01448 << "Number of inputs in multilayer perceptron "
01449 << "must be equal to number of input variables in input target data set."
01450 << std::endl;
01451
01452 exit(1);
01453 }
01454 else if(outputs_number != 1)
01455 {
01456 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01457 << "Vector<double> calculate_binary_classification_test(void)" << std::endl
01458 << "Number of outputs in multilayer perceptron must be one." << std::endl;
01459
01460 exit(1);
01461 }
01462 else if(target_variables_number != 1)
01463 {
01464 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01465 << "Vector<double> calculate_binary_classification_test(void)" << std::endl
01466 << "Number of target variables in input-target data set must be one." << std::endl;
01467
01468 exit(1);
01469 }
01470
01471 #endif
01472
01473
01474
01475 Matrix<int> confusion = calculate_confusion();
01476
01477 int true_positive = confusion[0][0];
01478 int false_positive = confusion[0][1];
01479 int false_negative = confusion[1][0];
01480 int true_negative = confusion[1][1];
01481
01482
01483
01484 double classification_accuracy = (double)(true_positive + true_negative)/double(true_positive + true_negative + false_positive + false_negative);
01485
01486
01487
01488 double error_rate = (double)(false_positive + false_negative)/(double)(true_positive + true_negative + false_positive + false_negative);
01489
01490
01491
01492 double sensitivity = (double)true_positive/(double)(true_positive + false_negative);
01493
01494
01495
01496 double specifity = (double)true_negative/(double)(true_negative + false_positive);
01497
01498
01499
01500 double positive_likelihood = sensitivity/(1.0 - specifity);
01501
01502
01503
01504 double negative_likelihood = specifity/(1.0 - sensitivity);
01505
01506 Vector<double> binary_classification_test(6);
01507 binary_classification_test[0] = classification_accuracy;
01508 binary_classification_test[1] = error_rate;
01509 binary_classification_test[2] = sensitivity;
01510 binary_classification_test[3] = specifity;
01511 binary_classification_test[4] = positive_likelihood;
01512 binary_classification_test[5] = negative_likelihood;
01513
01514 return(binary_classification_test);
01515 }
01516
01517
01518
01519
01522
01523 std::string PatternRecognitionUtilities::get_binary_classification_test_XML(bool show_declaration)
01524 {
01525 std::stringstream buffer;
01526
01527
01528
01529 if(show_declaration)
01530 {
01531 buffer << "<Flood version='3.0' class='PatternRecognitionUtilities' content='BinaryClassificationTest'>" << std::endl;
01532 }
01533
01534 Vector<double> binary_classification_test = calculate_binary_classification_test();
01535
01536 buffer << "<ClassificationAccuracy>" << std::endl
01537 << binary_classification_test[0] << std::endl
01538 << "</ClassificationAccuracy>" << std::endl;
01539
01540 buffer << "<ErrorRate>" << std::endl
01541 << binary_classification_test[1] << std::endl
01542 << "</ErrorRate>" << std::endl;
01543
01544 buffer << "<Sensitivity>" << std::endl
01545 << binary_classification_test[2] << std::endl
01546 << "</Sensitivity>" << std::endl;
01547
01548 buffer << "<Specifity>" << std::endl
01549 << binary_classification_test[3] << std::endl
01550 << "</Specifity>" << std::endl;
01551
01552 buffer << "<PositiveLikelihood>" << std::endl
01553 << binary_classification_test[4] << std::endl
01554 << "</PositiveLikelihood>" << std::endl;
01555
01556 buffer << "<NegativeLikelihood>" << std::endl
01557 << binary_classification_test[5] << std::endl
01558 << "</NegativeLikelihood>" << std::endl;
01559
01560 return(buffer.str());
01561 }
01562
01563
01564
01565
01567
01568 void PatternRecognitionUtilities::print_binary_classification_test(void)
01569 {
01570 std::cout << get_binary_classification_test_XML(true);
01571 }
01572
01573
01574
01575
01578
01579 void PatternRecognitionUtilities::save_binary_classification_test(const char* filename)
01580 {
01581 std::fstream file;
01582
01583 file.open(filename, std::ios::out);
01584
01585 if(!file.is_open())
01586 {
01587 std::cerr << "Flood Error: PatternRecognitionUtilities class." << std::endl
01588 << "void save_binary_classification_test(const char*) method." << std::endl
01589 << "Cannot open class distribution XML-type file." << std::endl;
01590
01591 exit(1);
01592 }
01593
01594 file << get_binary_classification_test_XML(true);
01595
01596 file.close();
01597 }
01598
01599 }
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615
01616
01617