16 #include "testing_analysis.h"
28 : neural_network_pointer(NULL),
29 data_set_pointer(NULL),
30 mathematical_model_pointer(NULL)
44 : neural_network_pointer(new_neural_network_pointer),
45 data_set_pointer(NULL),
46 mathematical_model_pointer(NULL)
60 : neural_network_pointer(NULL),
61 data_set_pointer(NULL),
62 mathematical_model_pointer(new_mathematical_model_pointer)
76 : neural_network_pointer(NULL),
77 data_set_pointer(new_data_set_pointer),
78 mathematical_model_pointer(NULL)
93 : neural_network_pointer(new_neural_network_pointer),
94 data_set_pointer(NULL),
95 mathematical_model_pointer(new_mathematical_model_pointer)
110 : neural_network_pointer(new_neural_network_pointer),
111 data_set_pointer(new_data_set_pointer),
112 mathematical_model_pointer(NULL)
128 : neural_network_pointer(new_neural_network_pointer),
129 data_set_pointer(new_data_set_pointer),
130 mathematical_model_pointer(new_mathematical_model_pointer)
144 : neural_network_pointer(NULL),
145 data_set_pointer(NULL),
146 mathematical_model_pointer(NULL)
150 from_XML(testing_analysis_document);
162 : neural_network_pointer(NULL),
163 data_set_pointer(NULL),
164 mathematical_model_pointer(NULL)
193 std::ostringstream buffer;
195 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
196 <<
"NeuralNetwork* get_neural_network_pointer(void) const method.\n"
197 <<
"Neural network pointer is NULL.\n";
199 throw std::logic_error(buffer.str());
218 std::ostringstream buffer;
220 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
221 <<
"DataSet* get_data_set_pointer(void) const method.\n"
222 <<
"Data set pointer is NULL.\n";
224 throw std::logic_error(buffer.str());
243 std::ostringstream buffer;
245 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
246 <<
"MathematicalModel* get_mathematical_model_pointer(void) const method.\n"
247 <<
"Mathematical model pointer is NULL.\n";
249 throw std::logic_error(buffer.str());
338 std::ostringstream buffer;
342 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
343 <<
"void check(void) const method.\n"
344 <<
"Neural network pointer is NULL.\n";
346 throw std::logic_error(buffer.str());
351 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
352 <<
"void check(void) const method.\n"
353 <<
"Data set pointer is NULL.\n";
355 throw std::logic_error(buffer.str());
397 for(
size_t i = 0; i < outputs_number; i++)
399 target_output_data[i].
set(testing_instances_number, 2);
401 target_output_data[i].set_column(0, target_data.
arrange_column(i));
402 target_output_data[i].set_column(1, output_data.
arrange_column(i));
405 return(target_output_data);
443 std::ostringstream buffer;
445 if(testing_instances_number == 0)
447 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
448 <<
"Vector< LinearRegressionParameters<double> > calculate_linear_regression_parameters(void) const method.\n"
449 <<
"Number of testing instances is zero.\n";
451 throw std::logic_error(buffer.str());
467 for(
size_t i = 0; i < outputs_number; i++)
475 return(linear_regression_parameters);
496 if(testing_instances_number == 0)
498 std::ostringstream buffer;
500 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
501 <<
"LinearRegressionResults perform_linear_regression_analysis(void) const method.\n"
502 <<
"Number of testing instances is zero.\n";
504 throw std::logic_error(buffer.str());
512 return(linear_regression_results);
523 std::ofstream file(file_name.c_str());
526 <<
"Target-output data:\n"
559 std::ostringstream buffer;
561 if(testing_instances_number == 0)
563 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
564 <<
"Vector< Matrix<double> > calculate_error_data(void) const.\n"
565 <<
"Number of testing instances is zero.\n";
567 throw std::logic_error(buffer.str());
585 if(!unscaling_layer_pointer)
587 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
588 <<
"Vector< Matrix<double> > calculate_error_data(void) const.\n"
589 <<
"Unscaling layer is NULL.\n";
591 throw std::logic_error(buffer.str());
608 for(
size_t i = 0; i < outputs_number; i++)
610 error_data[i].
set(testing_instances_number, 3, 0.0);
617 error_data[i].set_column(0, (targets - outputs).calculate_absolute_value());
621 error_data[i].set_column(1, (targets - outputs).calculate_absolute_value()/(outputs_maximum[i]-outputs_minimum[i]));
625 error_data[i].set_column(2, (targets - outputs).calculate_absolute_value()*100.0/(outputs_maximum[i]-outputs_minimum[i]));
656 for(
size_t i = 0; i < outputs_number; i++)
676 const size_t outputs_number = error_data_statistics.size();
680 for(
size_t i = 0; i < outputs_number; i++)
682 statistics[i].
set(3, 4);
683 statistics[i].set_row(0, error_data_statistics[i][0].to_vector());
684 statistics[i].set_row(1, error_data_statistics[i][1].to_vector());
685 statistics[i].set_row(2, error_data_statistics[i][2].to_vector());
702 const size_t outputs_number = error_data.size();
706 for(
size_t i = 0; i < outputs_number; i++)
725 const size_t outputs_number = error_data.size();
729 for(
size_t i = 0; i < outputs_number; i++)
734 return(maximal_errors);
750 size_t true_positive = 0;
751 size_t false_negative = 0;
752 size_t false_positive = 0;
753 size_t true_negative = 0;
758 for(
size_t i = 0; i < rows_number; i++)
762 if(actual_data(i,0) > 0.5 && predicted_data(i,0) > 0.5)
769 if(actual_data(i,0) > 0.5 && predicted_data(i,0) < 0.5)
776 if(actual_data(i,0) < 0.5 && predicted_data(i,0) > 0.5)
783 if(actual_data(i,0) < 0.5 && predicted_data(i,0) < 0.5)
789 confusion(0,0) = true_positive;
790 confusion(0,1) = false_negative;
791 confusion(1,0) = false_positive;
792 confusion(1,1) = true_negative;
811 size_t actual_index = 0;
812 size_t predicted_index = 0;
814 for(
size_t i = 0; i < rows_number; i++)
819 confusion(actual_index,predicted_index)++;
845 if(!multilayer_perceptron_pointer)
847 std::ostringstream buffer;
849 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
850 <<
"Matrix<size_t> calculate_confusion(void) const method.\n"
851 <<
"Pointer to multilayer perceptron in neural network is NULL.\n";
853 throw std::logic_error(buffer.str());
870 std::ostringstream buffer;
872 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
873 <<
"Matrix<size_t> calculate_confusion(void) const method." << std::endl
874 <<
"Number of inputs in neural network must be equal to number of inputs in data set." << std::endl;
876 throw std::logic_error(buffer.str());
881 std::ostringstream buffer;
883 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
884 <<
"Matrix<size_t> calculate_confusion(void) const method." << std::endl
885 <<
"Number of outputs in neural network must be equal to number of targets in data set." << std::endl;
887 throw std::logic_error(buffer.str());
897 if(outputs_number == 1)
935 std::ostringstream buffer;
937 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
938 <<
"Vector<double> calculate_binary_classification_tests(void) const." << std::endl
939 <<
"Data set is NULL." << std::endl;
941 throw std::logic_error(buffer.str());
954 std::ostringstream buffer;
956 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
957 <<
"Vector<double> calculate_binary_classification_tests(void) const." << std::endl
958 <<
"Number of inputs in neural network is not equal to number of inputs in data set." << std::endl;
960 throw std::logic_error(buffer.str());
962 else if(outputs_number != 1)
964 std::ostringstream buffer;
966 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
967 <<
"Vector<double> calculate_binary_classification_tests(void) const." << std::endl
968 <<
"Number of outputs in neural network must be one." << std::endl;
970 throw std::logic_error(buffer.str());
972 else if(targets_number != 1)
974 std::ostringstream buffer;
976 buffer <<
"OpenNN Exception: TestingAnalysis class." << std::endl
977 <<
"Vector<double> calculate_binary_classification_tests(void) const." << std::endl
978 <<
"Number of targets in data set must be one." << std::endl;
980 throw std::logic_error(buffer.str());
989 const size_t true_positive = confusion(0,0);
990 const size_t false_positive = confusion(0,1);
991 const size_t false_negative = confusion(1,0);
992 const size_t true_negative = confusion(1,1);
996 double classification_accuracy;
998 if(true_positive + true_negative + false_positive + false_negative == 0)
1000 classification_accuracy = 0.0;
1004 classification_accuracy = (double)(true_positive + true_negative)/double(true_positive + true_negative + false_positive + false_negative);
1011 if(true_positive + true_negative + false_positive + false_negative == 0)
1017 error_rate = (double)(false_positive + false_negative)/(double)(true_positive + true_negative + false_positive + false_negative);
1024 if(true_positive + false_negative == 0)
1030 sensitivity = (double)true_positive/(
double)(true_positive + false_negative);
1037 if(true_negative + false_positive == 0)
1043 specificity = (double)true_negative/(
double)(true_negative + false_positive);
1048 double positive_likelihood;
1050 if(classification_accuracy == 1.0)
1052 positive_likelihood = 1.0;
1054 else if(1.0 - specificity == 0.0)
1056 positive_likelihood = 0.0;
1060 positive_likelihood = sensitivity/(1.0 - specificity);
1065 double negative_likelihood;
1067 if(classification_accuracy == 1.0)
1069 negative_likelihood = 1.0;
1071 else if(1.0 - sensitivity == 0.0)
1073 negative_likelihood = 0.0;
1077 negative_likelihood = specificity/(1.0 - sensitivity);
1083 binary_classification_test[0] = classification_accuracy;
1084 binary_classification_test[1] = error_rate;
1085 binary_classification_test[2] = sensitivity;
1086 binary_classification_test[3] = specificity;
1087 binary_classification_test[4] = positive_likelihood;
1088 binary_classification_test[5] = negative_likelihood;
1090 return(binary_classification_test);
1100 std::ostringstream buffer;
1102 buffer <<
"Testing analysis\n"
1103 <<
"Display: " <<
display <<
"\n";
1105 return(buffer.str());
1126 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
1128 std::ostringstream buffer;
1132 tinyxml2::XMLElement* testing_analysis_element = document->NewElement(
"TestingAnalysis");
1134 document->InsertFirstChild(testing_analysis_element);
1138 tinyxml2::XMLElement* display_element = document->NewElement(
"Display");
1139 testing_analysis_element->LinkEndChild(display_element);
1144 tinyxml2::XMLText* display_text = document->NewText(buffer.str().c_str());
1145 testing_analysis_element->LinkEndChild(display_text);
1158 std::ostringstream buffer;
1160 const tinyxml2::XMLElement* root_element = document.FirstChildElement(
"TestingAnalysis");
1164 buffer <<
"OpenNN Exception: TestingAnalysis class.\n"
1165 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
1166 <<
"Testing analysis element is NULL.\n";
1168 throw std::logic_error(buffer.str());
1173 const tinyxml2::XMLElement* element = root_element->FirstChildElement(
"Display");
1177 std::string new_display_string = element->GetText();
1183 catch(
const std::logic_error& e)
1185 std::cout << e.what() << std::endl;
1198 tinyxml2::XMLDocument* document =
to_XML();
1200 document->SaveFile(file_name.c_str());
1215 tinyxml2::XMLDocument document;
1217 if(document.LoadFile(file_name.c_str()))
1219 std::ostringstream buffer;
1221 buffer <<
"OpenNN Exception: Testing analysis class.\n"
1222 <<
"void load(const std::string&) method.\n"
1223 <<
"Cannot load XML file " << file_name <<
".\n";
1225 throw std::logic_error(buffer.str());
DataSet * get_data_set_pointer(void) const
Returns a pointer to the data set object on which the neural network is tested.
Vector< T > arrange_column(const size_t &) const
void print(void) const
Prints to the standard output the string representation of this testing analysis object.
virtual ~TestingAnalysis(void)
const Variables & get_variables(void) const
Returns a constant reference to the variables object composing this data set object.
Vector< double > arrange_minimums(void) const
bool display
Display messages to screen.
size_t get_inputs_number(void) const
Returns the number of inputs to the multilayer perceptron.
NeuralNetwork * get_neural_network_pointer(void) const
Returns a pointer to the neural network object which is to be tested.
void set_mathematical_model_pointer(MathematicalModel *)
void set(void)
Sets the size of a vector to zero.
Vector< size_t > calculate_maximal_indices(const size_t &) const
void set_display(const bool &)
std::string to_string(void) const
Returns a string representation of the testing analysis object.
Matrix< double > arrange_testing_target_data(void) const
Vector< LinearRegressionParameters< double > > linear_regression_parameters
Intercept, slope and correlation coefficient for each output variable.
size_t get_outputs_number(void) const
Returns the number of outputs neurons in the multilayer perceptron.
Vector< LinearRegressionParameters< double > > calculate_linear_regression_parameters(void) const
void set_data_set_pointer(DataSet *)
void save(const std::string &) const
LinearRegressionParameters< T > calculate_linear_regression_parameters(const Vector< T > &) const
Histogram< T > calculate_histogram(const size_t &=10) const
Matrix< size_t > calculate_confusion_multiple_classification(const Matrix< double > &, const Matrix< double > &) const
size_t count_testing_instances_number(void) const
Returns the number of instances in the data set which will be used for testing.
const size_t & get_columns_number(void) const
Returns the number of columns in the matrix.
MathematicalModel * get_mathematical_model_pointer(void) const
Returns a pointer to the mathematical model object on which the neural network is tested...
Vector< Matrix< double > > calculate_error_data(void) const
Vector< Matrix< double > > calculate_error_data_statistics_matrices(void) const
MultilayerPerceptron * get_multilayer_perceptron_pointer(void) const
Returns a pointer to the multilayer perceptron composing this neural network.
size_t calculate_maximal_index(void) const
Returns the index of the largest element in the vector.
LinearRegressionResults perform_linear_regression_analysis(void) const
virtual tinyxml2::XMLDocument * to_XML(void) const
Vector< Vector< size_t > > calculate_maximal_errors(const size_t &=10) const
Matrix< double > calculate_output_data(const Matrix< double > &) const
void save(const std::string &) const
NeuralNetwork * neural_network_pointer
Pointer to the neural network object to be tested.
Matrix< size_t > calculate_confusion(void) const
Matrix< size_t > calculate_confusion_binary_classification(const Matrix< double > &, const Matrix< double > &) const
void set_neural_network_pointer(NeuralNetwork *)
Vector< Vector< Statistics< double > > > calculate_error_data_statistics(void) const
const size_t & get_rows_number(void) const
Returns the number of rows in the matrix.
void load(const std::string &)
UnscalingLayer * get_unscaling_layer_pointer(void) const
Returns a pointer to the unscaling layer composing this neural network.
size_t get_unscaling_neurons_number(void) const
Returns the number of unscaling neurons in this layer.
Vector< Matrix< double > > target_output_data
Target data from data set and output data from neural network.
Matrix< double > arrange_testing_input_data(void) const
size_t count_inputs_number(void) const
Returns the number of input variables of the data set.
Vector< double > calculate_binary_classification_tests(void) const
DataSet * data_set_pointer
Pointer to a data set object.
Statistics< T > calculate_statistics(void) const
Returns the minimum, maximum, mean and the standard deviation of the elements in the vector...
virtual void from_XML(const tinyxml2::XMLDocument &)
size_t count_targets_number(void) const
Returns the number of target variables of the data set.
Vector< Matrix< double > > calculate_target_output_data(void) const
const bool & get_display(void) const
MathematicalModel * mathematical_model_pointer
Pointer to a mathematical model object.
Vector< double > arrange_maximums(void) const
Vector< T > arrange_row(const size_t &) const
Vector< Histogram< double > > calculate_error_data_histograms(const size_t &=10) const
const Instances & get_instances(void) const
Returns a constant reference to the instances object composing this data set object.