OpenNN  2.2
Open Neural Networks Library
instances.h
1 /****************************************************************************************************************/
2 /* */
3 /* OpenNN: Open Neural Networks Library */
4 /* www.artelnics.com/opennn */
5 /* */
6 /* I N S T A N C E S C L A S S H E A D E R */
7 /* */
8 /* Roberto Lopez */
9 /* Artelnics - Making intelligent use of data */
11 /* */
12 /****************************************************************************************************************/
13 
14 #ifndef __INSTANCES_H__
15 #define __INSTANCES_H__
16 
17 // System includes
18 
19 #include <iostream>
20 #include <fstream>
21 #include <string>
22 #include <sstream>
23 #include <cmath>
24 #include <algorithm>
25 #include <cstdlib>
26 #include <stdexcept>
27 #include <ctime>
28 #include <exception>
29 
30 // OpenNN includes
31 
32 #include "vector.h"
33 #include "matrix.h"
34 
35 // TinyXml includes
36 
37 #include "../tinyxml2/tinyxml2.h"
38 
39 
40 namespace OpenNN
41 {
42 
45 
46 class Instances
47 {
48 
49 public:
50 
51  // DEFAULT CONSTRUCTOR
52 
53  explicit Instances(void);
54 
55  // INSTANCES NUMBER CONSTRUCTOR
56 
57  explicit Instances(const size_t&);
58 
59  // XML CONSTRUCTOR
60 
61  explicit Instances(const tinyxml2::XMLDocument&);
62 
63 
64  // COPY CONSTRUCTOR
65 
66  Instances(const Instances&);
67 
68 
69  // DESTRUCTOR
70 
71  virtual ~Instances(void);
72 
73  // ASSIGNMENT OPERATOR
74 
76 
77  // EQUAL TO OPERATOR
78 
79  bool operator == (const Instances&) const;
80 
81  // ENUMERATIONS
82 
85 
86  enum Use{Unused, Training, Generalization, Testing};
87 
90 
91  enum SplittingMethod{Sequential, Random};
92 
93  // STRUCTURES
94 
99 
100  struct Item
101  {
103 
104  Item(void)
105  {
106  use = Training;
107  }
108 
110 
111  Item(const Use& new_use)
112  {
113  use = new_use;
114  }
115 
117 
118  virtual ~Item(void)
119  {
120  }
121 
123 
125  };
126 
127 
128  // METHODS
129 
130  static SplittingMethod get_splitting_method(const std::string&);
131 
133 
134  inline size_t get_instances_number(void) const
135  {
136  return(items.size());
137  }
138 
139  bool empty(void) const;
140 
141  // Instances methods
142 
143  Vector<Use> arrange_uses(void) const;
144  Vector<std::string> write_uses(void) const;
146 
147  const Use& get_use(const size_t&) const;
148  std::string write_use(const size_t&) const;
149 
150  bool is_used(const size_t&) const;
151 
152  size_t count_training_instances_number(void) const;
153  size_t count_generalization_instances_number(void) const;
154  size_t count_testing_instances_number(void) const;
155  size_t count_unused_instances_number(void) const;
156  size_t count_used_instances_number(void) const;
157 
158  Vector<size_t> count_uses(void) const;
159 
163 
164  const bool& get_display(void) const;
165 
166  // Set methods
167 
168  void set(void);
169  void set(const size_t&);
170  void set(const tinyxml2::XMLDocument&);
171 
172  void set_default(void);
173 
174  // Data methods
175 
176  void set_instances_number(const size_t&);
177 
178  // Instances methods
179 
180  void set_uses(const Vector<Use>&);
181  void set_uses(const Vector<std::string>&);
182 
183  void set_use(const size_t&, const Use&);
184  void set_use(const size_t&, const std::string&);
185 
186  void set_training(void);
187  void set_generalization(void);
188  void set_testing(void);
189 
190  void set_display(const bool&);
191 
192  // Splitting methods
193 
194  void split_sequential_indices(const double& training_ratio = 0.6, const double& generalization_ratio = 0.2, const double& testing_ratio = 0.2);
195 
196  void split_random_indices(const double& training_ratio = 0.6, const double& generalization_ratio = 0.2, const double& testing_ratio = 0.2);
197 
198  void split_instances(const SplittingMethod& splitting_method = Random, const double& training_ratio = 0.6, const double& generalization_ratio = 0.2, const double& testing_ratio = 0.2);
199 
201 
202  void convert_time_series(const size_t&);
203 
204  // Serialization methods
205 
206  std::string to_string(void) const;
207 
208  void print(void) const;
209 
210  tinyxml2::XMLDocument* to_XML(void) const;
211  void from_XML(const tinyxml2::XMLDocument&);
212 
213 private:
214 
215  // MEMBERS
216 
218 
220 
222 
223  bool display;
224 };
225 
226 }
227 
228 #endif
229 
230 // OpenNN: Open Neural Networks Library.
231 // Copyright (c) 2005-2015 Roberto Lopez.
232 //
233 // This library is free software; you can redistribute it and/or
234 // modify it under the terms of the GNU Lesser General Public
235 // License as published by the Free Software Foundation; either
236 // version 2.1 of the License, or any later version.
237 //
238 // This library is distributed in the hope that it will be useful,
239 // but WITHOUT ANY WARRANTY; without even the implied warranty of
240 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
241 // Lesser General Public License for more details.
242 
243 // You should have received a copy of the GNU Lesser General Public
244 // License along with this library; if not, write to the Free Software
245 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
246 
const Use & get_use(const size_t &) const
Definition: instances.cpp:286
bool empty(void) const
Returns empty if the number of instances is zero, and false otherwise.
Definition: instances.cpp:157
tinyxml2::XMLDocument * to_XML(void) const
Definition: instances.cpp:851
void set_display(const bool &)
Definition: instances.cpp:826
Vector< std::string > write_uses(void) const
Returns the use of every instance (training, generalization, testing) in a string vector...
Definition: instances.cpp:193
Item(void)
Default constructor.
Definition: instances.h:104
std::string to_string(void) const
Returns a string representation of the current instances object.
Definition: instances.cpp:1329
void set(void)
Sets a instances object with zero instances.
Definition: instances.cpp:581
void set_instances_number(const size_t &)
Definition: instances.cpp:838
void set_testing(void)
Sets all the instances in the data set for testing.
Definition: instances.cpp:808
size_t count_training_instances_number(void) const
Returns the number of instances in the data set which will be used for training.
Definition: instances.cpp:387
void print(void) const
Prints to the screen information about the instances object.
Definition: instances.cpp:1349
std::string write_use(const size_t &) const
Definition: instances.cpp:297
virtual ~Instances(void)
Destructor.
Definition: instances.cpp:77
static SplittingMethod get_splitting_method(const std::string &)
Definition: instances.cpp:130
const bool & get_display(void) const
Definition: instances.cpp:571
void set_training(void)
Sets all the instances in the data set for training.
Definition: instances.cpp:778
Vector< size_t > arrange_testing_indices(void) const
Returns the indices of the instances which will be used for testing.
Definition: instances.cpp:543
size_t count_testing_instances_number(void) const
Returns the number of instances in the data set which will be used for testing.
Definition: instances.cpp:431
Vector< size_t > arrange_training_indices(void) const
Returns the indices of the instances which will be used for training.
Definition: instances.cpp:489
size_t count_generalization_instances_number(void) const
Returns the number of instances in the data set which will be used for generalization.
Definition: instances.cpp:409
void set_generalization(void)
Sets all the instances in the data set for generalization.
Definition: instances.cpp:793
void from_XML(const tinyxml2::XMLDocument &)
Definition: instances.cpp:916
void convert_time_series(const size_t &)
Definition: instances.cpp:1304
bool operator==(const Instances &) const
Definition: instances.cpp:109
virtual ~Item(void)
Destructor.
Definition: instances.h:118
Vector< Item > items
Uses of instances (none, training, generalization or testing).
Definition: instances.h:219
Vector< double > calculate_uses_percentage(void) const
Definition: instances.cpp:1281
void split_instances(const SplittingMethod &splitting_method=Random, const double &training_ratio=0.6, const double &generalization_ratio=0.2, const double &testing_ratio=0.2)
Definition: instances.cpp:1201
void set_default(void)
Definition: instances.cpp:623
void split_sequential_indices(const double &training_ratio=0.6, const double &generalization_ratio=0.2, const double &testing_ratio=0.2)
Definition: instances.cpp:1114
size_t count_unused_instances_number(void) const
Definition: instances.cpp:351
void set_use(const size_t &, const Use &)
Definition: instances.cpp:731
Vector< std::string > write_abbreviated_uses(void) const
Returns the use of every instance (training, generalization, testing) in a string vector...
Definition: instances.cpp:239
void split_random_indices(const double &training_ratio=0.6, const double &generalization_ratio=0.2, const double &testing_ratio=0.2)
Definition: instances.cpp:1016
Use use
Use of an instance (training, generalization, testing or unused).
Definition: instances.h:124
bool is_used(const size_t &) const
Definition: instances.cpp:333
Item(const Use &new_use)
Use constructor.
Definition: instances.h:111
size_t count_used_instances_number(void) const
Definition: instances.cpp:374
Vector< size_t > count_uses(void) const
Definition: instances.cpp:455
void set_uses(const Vector< Use > &)
Definition: instances.cpp:635
size_t get_instances_number(void) const
Returns the number of instances in the data set.
Definition: instances.h:134
Vector< size_t > arrange_generalization_indices(void) const
Returns the indices of the instances which will be used for generalization.
Definition: instances.cpp:516
bool display
Display messages to screen.
Definition: instances.h:223
Vector< Use > arrange_uses(void) const
Returns the use of every instance (training, generalization, testing) in a vector.
Definition: instances.cpp:174
Instances & operator=(const Instances &)
Definition: instances.cpp:88