testing_analysis.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// T E S T I N G A N A L Y S I S C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef TESTINGANALYSIS_H
10#define TESTINGANALYSIS_H
11
12// System includes
13
14#include <iostream>
15#include <fstream>
16#include <string>
17#include <sstream>
18#include <cmath>
19
20#include <numeric>
21
22// OpenNN includes
23
24#include "config.h"
25
26#include "correlations.h"
27#include "data_set.h"
28#include "neural_network.h"
29
30//Eigen includes
31
32namespace OpenNN
33{
34
36
40
42{
43
44public:
45
46 // Constructors
47
48 explicit TestingAnalysis();
49
51
52 // Destructor
53
54 virtual ~TestingAnalysis();
55
57
59 {
61
62 type correlation = type(0);
63
64 type intercept = type(0);
65
66 type slope = type(0);
67
68 Tensor<type, 1> targets;
69 Tensor<type, 1> outputs;
70
72
73 void save(const string&) const
74 {
75 }
76
77 void print() const
78 {
79 cout << "Linear regression analysis" << endl;
80 cout << "Correlation: " << correlation << endl;
81 cout << "Intercept: " << intercept << endl;
82 cout << "Slope: " << slope << endl;
83 }
84
85 };
86
87
89
91 {
93
94 Tensor<type, 2> roc_curve;
95
97
99
101
103
105
107 };
108
109
111
113 {
115
117
119
121
123
124 Tensor<type, 1> maximum_gain;
125 };
126
127
129
131 {
133
134 Tensor<Index, 1> true_positives_indices;
135
137
138 Tensor<Index, 1> false_positives_indices;
139
141
142 Tensor<Index, 1> false_negatives_indices;
143
145
146 Tensor<Index, 1> true_negatives_indices;
147 };
148
149 // Get methods
150
153
154 const bool& get_display() const;
155
156 // Set methods
157
160
161 void set_display(const bool&);
162
163 void set_default();
164
165 void set_threads_number(const int&);
166
167 // Checking methods
168
169 void check() const;
170
171 // Error data methods
172
173 Tensor<type, 3> calculate_error_data() const;
174 Tensor<type, 2> calculate_percentage_error_data() const;
175
176 Tensor<Descriptives, 1> calculate_absolute_errors_descriptives() const;
177 Tensor<Descriptives, 1> calculate_absolute_errors_descriptives(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
178
179 Tensor<Descriptives, 1> calculate_percentage_errors_descriptives() const;
180 Tensor<Descriptives, 1> calculate_percentage_errors_descriptives(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
181
182 Tensor<Tensor<Descriptives, 1>, 1> calculate_error_data_descriptives() const;
183 void print_error_data_descriptives() const;
184
185 Tensor<Histogram, 1> calculate_error_data_histograms(const Index& = 10) const;
186
187 Tensor<Tensor<Index, 1>, 1> calculate_maximal_errors(const Index& = 10) const;
188
189 Tensor<type, 2> calculate_errors() const;
190 Tensor<type, 2> calculate_binary_classification_errors() const;
191 Tensor<type, 2> calculate_multiple_classification_errors() const;
192
193 Tensor<type, 1> calculate_training_errors() const;
194 Tensor<type, 1> calculate_binary_classification_training_errors() const;
195 Tensor<type, 1> calculate_multiple_classification_training_errors() const;
196
197 Tensor<type, 1> calculate_selection_errors() const;
198 Tensor<type, 1> calculate_binary_classification_selection_errors() const;
199 Tensor<type, 1> calculate_multiple_classification_selection_errors() const;
200
201 Tensor<type, 1> calculate_testing_errors() const;
204
205 type calculate_normalized_squared_error(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
206 type calculate_cross_entropy_error(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
207 type calculate_weighted_squared_error(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<type, 1>& = Tensor<type, 1>()) const;
208 type calculate_Minkowski_error(const Tensor<type, 2>&, const Tensor<type, 2>&, const type = type(1.5)) const;
209
210 // Linear regression analysis methods
211
212 Tensor<Correlation, 1> linear_correlation() const;
213 Tensor<Correlation, 1> linear_correlation(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
214
215 void print_linear_regression_correlations() const;
216
217 Tensor<LinearRegressionAnalysis, 1> perform_linear_regression_analysis() const;
218 void print_linear_regression_analysis() const;
219
220 // Binary classifcation methods
221
222 Tensor<type, 1> calculate_binary_classification_tests() const;
223
224 void print_binary_classification_tests() const;
225
226 type calculate_logloss() const;
227
228 // Confusion methods
229
230 Tensor<Index, 2> calculate_confusion_binary_classification(const Tensor<type, 2>&, const Tensor<type, 2>&, const type&) const;
231 Tensor<Index, 2> calculate_confusion_multiple_classification(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
232
233 Tensor<Index, 1> calculate_positives_negatives_rate(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
234
235 Tensor<Index, 2> calculate_confusion(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
236 Tensor<Index, 2> calculate_confusion() const;
237
238 // ROC curve
239
241
242 type calculate_Wilcoxon_parameter(const type&, const type&) const;
243
244 Tensor<type, 2> calculate_roc_curve(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
245 type calculate_area_under_curve(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
246 type calculate_area_under_curve(const Tensor<type, 2>&) const;
247 type calculate_area_under_curve_confidence_limit(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
248 type calculate_area_under_curve_confidence_limit(const Tensor<type, 2>&, const Tensor<type, 2>&, const type&) const;
249 type calculate_optimal_threshold(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
250 type calculate_optimal_threshold(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<type, 2>&) const;
251
252 // Lift Chart
253
254 Tensor<type, 2> perform_cumulative_gain_analysis() const;
255 Tensor<type, 2> calculate_cumulative_gain(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
256 Tensor<type, 2> calculate_negative_cumulative_gain(const Tensor<type, 2>&, const Tensor<type, 2>&)const;
257
258 Tensor<type, 2> perform_lift_chart_analysis() const;
259 Tensor<type, 2> calculate_lift_chart(const Tensor<type, 2>&) const;
260
262 Tensor<type, 1> calculate_maximum_gain(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
263
264 // Calibration plot
265
266 Tensor<type, 2> perform_calibration_plot_analysis() const;
267
268 Tensor<type, 2> calculate_calibration_plot(const Tensor<type, 2>&, const Tensor<type, 2>&) const;
269
270 // Output histogram
271
272 Tensor<Histogram, 1> calculate_output_histogram(const Tensor<type, 2>&, const Index& = 10) const;
273
274 // Binary classification rates
275
277
278 Tensor<Index, 1> calculate_true_positive_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<Index, 1>&, const type&) const;
279 Tensor<Index, 1> calculate_false_positive_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<Index, 1>&, const type&) const;
280 Tensor<Index, 1> calculate_false_negative_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<Index, 1>&, const type&) const;
281 Tensor<Index, 1> calculate_true_negative_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<Index, 1>&, const type&) const;
282
283 // Multiple classification tests
284
285 Tensor<type, 1> calculate_multiple_classification_tests() const;
286 void save_confusion(const string&) const;
287 void save_multiple_classification_tests(const string&) const;
288
289 // Multiple classification rates
290
291 Tensor<Tensor<Index,1>, 2> calculate_multiple_classification_rates() const;
292
293 Tensor<Tensor<Index,1>, 2> calculate_multiple_classification_rates(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<Index, 1>&) const;
294
295 Tensor<string, 2> calculate_well_classified_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&);
296
297 Tensor<string, 2> calculate_misclassified_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&);
298
299 void save_well_classified_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
300
301 void save_misclassified_samples(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
302
303 void save_well_classified_samples_statistics(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
304
305 void save_misclassified_samples_statistics(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
306
307 void save_well_classified_samples_probability_histogram(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
308
309 void save_well_classified_samples_probability_histogram(const Tensor<string, 2>&, const string&);
310
311 void save_misclassified_samples_probability_histogram(const Tensor<type, 2>&, const Tensor<type, 2>&, const Tensor<string, 1>&, const string&);
312
313 void save_misclassified_samples_probability_histogram(const Tensor<string, 2>&, const string&);
314
315 // Forecasting methods
316
317 Tensor<Tensor<type, 1>, 1> calculate_error_autocorrelation(const Index& = 10) const;
318
319 Tensor<Tensor<type, 1>, 1> calculate_inputs_errors_cross_correlation(const Index& = 10) const;
320
321 // Serialization methods
322
323 void print() const;
324
325 virtual void from_XML(const tinyxml2::XMLDocument&);
326
327 virtual void write_XML(tinyxml2::XMLPrinter&) const;
328
329 void save(const string&) const;
330 void load(const string&);
331
332 bool contains(const Tensor<type, 1>&, const type&) const;
333 Tensor<type, 2> delete_row(const Tensor<type, 2>&, const Index&) const;
334
335
336private:
337
338 NonBlockingThreadPool* non_blocking_thread_pool = nullptr;
339 ThreadPoolDevice* thread_pool_device = nullptr;
340
342
344
346
348
350
351 bool display = true;
352
353 const Eigen::array<IndexPair<Index>, 2> SSE = {IndexPair<Index>(0, 0), IndexPair<Index>(1, 1)};
354};
355
356}
357
358#endif
359
360// OpenNN: Open Neural Networks Library.
361// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
362//
363// This library is free software; you can redistribute it and/or
364// modify it under the terms of the GNU Lesser General Public
365// License as published by the Free Software Foundation; either
366// version 2.1 of the License, or any later version.
367//
368// This library is distributed in the hope that it will be useful,
369// but WITHOUT ANY WARRANTY; without even the implied warranty of
370// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
371// Lesser General Public License for more details.
372
373// You should have received a copy of the GNU Lesser General Public
374// License along with this library; if not, write to the Free Software
375// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This class represents the concept of data set for data modelling problems, such as approximation,...
Definition: data_set.h:57
This class contains tools for testing neural networks in different learning tasks.
Tensor< type, 2 > calculate_roc_curve(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< type, 2 > perform_calibration_plot_analysis() const
Performs a calibration plot analysis.
KolmogorovSmirnovResults perform_Kolmogorov_Smirnov_analysis() const
DataSet * data_set_pointer
Pointer to a data set object.
Tensor< type, 2 > calculate_lift_chart(const Tensor< type, 2 > &) const
NeuralNetwork * neural_network_pointer
Pointer to the neural network object to be tested.
type calculate_optimal_threshold(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< Tensor< Index, 1 >, 2 > calculate_multiple_classification_rates() const
Returns a matrix of subvectors which have the rates for a multiple classification problem.
Tensor< type, 2 > calculate_percentage_error_data() const
void set_data_set_pointer(DataSet *)
Tensor< type, 2 > calculate_errors() const
const bool & get_display() const
Tensor< type, 1 > calculate_multiple_classification_testing_errors() const
Tensor< Index, 1 > calculate_false_negative_samples(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const Tensor< Index, 1 > &, const type &) const
Tensor< Index, 1 > calculate_true_positive_samples(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const Tensor< Index, 1 > &, const type &) const
type calculate_weighted_squared_error(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const Tensor< type, 1 > &=Tensor< type, 1 >()) const
type calculate_cross_entropy_error(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
virtual void from_XML(const tinyxml2::XMLDocument &)
void load(const string &)
Tensor< type, 3 > calculate_error_data() const
Tensor< Index, 1 > calculate_positives_negatives_rate(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< Index, 1 > calculate_false_positive_samples(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const Tensor< Index, 1 > &, const type &) const
bool display
Display messages to screen.
Tensor< type, 2 > perform_cumulative_gain_analysis() const
Tensor< Index, 2 > calculate_confusion_multiple_classification(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< type, 2 > perform_lift_chart_analysis() const
type calculate_Wilcoxon_parameter(const type &, const type &) const
type calculate_area_under_curve_confidence_limit(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< type, 2 > calculate_calibration_plot(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
type calculate_area_under_curve(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
RocAnalysisResults perform_roc_analysis() const
Tensor< Index, 2 > calculate_confusion_binary_classification(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const type &) const
Tensor< Tensor< Index, 1 >, 1 > calculate_maximal_errors(const Index &=10) const
void save(const string &) const
Tensor< Tensor< type, 1 >, 1 > calculate_inputs_errors_cross_correlation(const Index &=10) const
NeuralNetwork * get_neural_network_pointer() const
Returns a pointer to the neural network object which is to be tested.
type calculate_normalized_squared_error(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< Index, 2 > calculate_confusion() const
Tensor< type, 2 > calculate_negative_cumulative_gain(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
void set_neural_network_pointer(NeuralNetwork *)
Tensor< Tensor< type, 1 >, 1 > calculate_error_autocorrelation(const Index &=10) const
Tensor< LinearRegressionAnalysis, 1 > perform_linear_regression_analysis() const
type calculate_logloss() const
Returns the logloss for a binary classification problem.
Tensor< type, 1 > calculate_maximum_gain(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
Tensor< type, 1 > calculate_binary_classification_tests() const
void print() const
Prints to the standard output the string representation of this testing analysis object.
Tensor< Histogram, 1 > calculate_error_data_histograms(const Index &=10) const
void set_display(const bool &)
virtual void write_XML(tinyxml2::XMLPrinter &) const
Tensor< type, 1 > calculate_binary_classification_testing_errors() const
Tensor< type, 1 > calculate_testing_errors() const
Tensor< Index, 1 > calculate_true_negative_samples(const Tensor< type, 2 > &, const Tensor< type, 2 > &, const Tensor< Index, 1 > &, const type &) const
Tensor< Correlation, 1 > linear_correlation() const
BinaryClassifcationRates calculate_binary_classification_rates() const
Tensor< type, 2 > calculate_cumulative_gain(const Tensor< type, 2 > &, const Tensor< type, 2 > &) const
DataSet * get_data_set_pointer() const
Returns a pointer to the data set object on which the neural network is tested.
Tensor< Histogram, 1 > calculate_output_histogram(const Tensor< type, 2 > &, const Index &=10) const
Tensor< Tensor< Descriptives, 1 >, 1 > calculate_error_data_descriptives() const
Structure with the binary classification rates.
Tensor< Index, 1 > true_negatives_indices
Vector with the indices of the samples which are true negative.
Tensor< Index, 1 > true_positives_indices
Vector with the indices of the samples which are true positive.
Tensor< Index, 1 > false_positives_indices
Vector with the indices of the samples which are false positive.
Tensor< Index, 1 > false_negatives_indices
Vector with the indices of the samples which are false negative.
Structure with the results from Kolmogorov-Smirnov analysis.
Tensor< type, 2 > positive_cumulative_gain
Matrix containing the data of a positive cumulative gain.
Tensor< type, 1 > maximum_gain
Maximum gain of the cumulative gain analysis.
Tensor< type, 2 > negative_cumulative_gain
Matrix containing the data of a negative cumulative gain.
Structure with the results from a linear regression analysis.
type correlation
Target data from data set and output data from neural network.
Structure with the results from a roc curve analysis.
type optimal_threshold
Optimal threshold of a ROC curve.
Tensor< type, 2 > roc_curve
Matrix containing the data of a ROC curve.
type area_under_curve
Area under a ROC curve.