60 template<
class Item,
class Target>
66 typedef std::multimap<unsigned, unsigned, std::less<unsigned> >
splitTS;
93 :isCalibrated(_calib), nTargets(0), theItems(_n)
122 if ((_tp > 1) || (_tp <= 0) || (_vp > 1) || (_vp <= 0) || (_tp + _vp > 1))
124 throw std::invalid_argument(
"Split mode proportions must be < 1");
126 std::vector<float> acc(2);
129 std::random_device rd;
130 std::mt19937 gen(rd());
131 std::uniform_real_distribution<> dist(0.
f, 1.
f);
132 for (
unsigned i = 0;
i <
size();
i ++)
134 float aRnd = dist(gen);
144 splitTrainingSet.insert(std::pair<unsigned, unsigned>(rw,
i));
156 return splitTrainingSet.lower_bound(_um);
162 return splitTrainingSet.upper_bound(_um);
171 virtual void add(
const Item& _i,
const Target& _tg)
173 theItems.push_back(_i);
174 theTargets.push_back(_tg);
183 virtual void add(
const Item& _i)
185 theItems.push_back(_i);
193 virtual bool remove(
unsigned int _idx)
196 if (_idx > theItems.size())
199 theItems.erase(theItems.begin() + _idx);
201 theTargets.erase(theTargets.begin() + _idx);
212 return theItems.size();
226 msg =
"The training set is not calibrated.";
227 throw std::out_of_range(msg);
233 throw std::out_of_range(msg);
236 return theTargets[_i];
251 throw std::out_of_range(msg);
254 return theTargets[_i];
270 throw std::out_of_range(msg);
290 throw std::out_of_range(msg);
312 isCalibrated = _calib;
376 throw std::runtime_error(
"TS not calibrated");
385 if (_i >
size() || _j >
size() || _i == _j)
387 swap(theItems[_i], theItems[_j]);
389 swap(theTargets[_i], theTargets[_j]);
424 typedef std::set< Target, std::less<Target> > STB;
426 for (
unsigned i = 0;
i <
size();
i ++)
431 nTargets = targetSet.size();
457 for (std::string::iterator
i = s.begin() ;
i != s.end() ;
i++)
460 if (s2 ==
"CALIBRATED")
463 for (std::string::iterator
i = s.end() ;
i > s.begin() ; _is.putback(*(--
i)));
470 msg =
"Error reading the file";
471 throw std::runtime_error(msg);
493 catch (std::exception&)
496 msg =
"Error reading the item";
497 throw std::runtime_error(msg);
502 theItems.push_back(item);
511 if (_is) _is.putback(c);
521 catch (std::exception&)
524 msg =
"Error reading the item";
525 throw std::runtime_error(msg);
528 theTargets.push_back(target);
531 theTargets.push_back(Target());
536 msg =
"Error reading the item";
537 throw std::runtime_error(msg);
552 _os <<
"calibrated" << std::endl;
561 void writeItems(std::ostream& _os,
bool _delim =
false)
const 563 typename std::vector<Item>::const_iterator
i;
564 typename std::vector<Target>::const_iterator
j;
565 for (i = theItems.begin(), j = theTargets.begin() ; i < theItems.end() ;
573 for (
size_t d = 0;
d < (*i).size();
d++)
576 if (
d != (*i).size() - 1) _os <<
" ";
605 while (_is && c ==
'#')
608 while (_is && _is.get() !=
'\n');
624 typename std::vector<Item>::const_iterator
itemsBegin()
const 626 return theItems.begin();
633 typename std::vector<Item>::const_iterator
itemsEnd()
const 635 return theItems.end();
645 return theTargets.begin();
651 typename std::vector<Target>::const_iterator
targetsEnd()
const 653 return theTargets.end();
void checkCalibrated(std::istream &_is)
splitIt beginSubset(unsigned _um)
std::vector< Item >::const_iterator itemsBegin() const
splitMode
Ways the training set can be used.
friend std::ostream & operator<<(std::ostream &_os, const ClassificationTrainingSet &_ts)
Target & targetAt(unsigned _i)
virtual void add(const Item &_i)
splitTS::iterator splitIt
iterator
virtual bool swapItems(unsigned _i, unsigned _j)
void setSplit(float _tp, float _vp)
String integerToString(int I, int _width, char fill_with)
const Target & targetAt(unsigned _i) const
friend std::istream & operator>>(std::istream &_is, ClassificationTrainingSet &_ts)
virtual void printSelf(std::ostream &_os) const
virtual void readSelf(std::istream &_is)
virtual ~ClassificationTrainingSet()
std::vector< Item > theItems
Item & itemAt(unsigned _i)
std::vector< Target >::const_iterator targetsEnd() const
virtual void saveObject(std::ostream &_os) const
unsigned numTargets() const
void readItems(std::istream &_is)
basic_istream< char, std::char_traits< char > > istream
void writeItems(std::ostream &_os, bool _delim=false) const
std::vector< Target > theTargets
std::vector< Item >::const_iterator itemsEnd() const
splitIt endSubset(unsigned _um)
Returns an iterator to the end of the subset.
void writeCalibrated(std::ostream &_os) const
void skipComments(std::istream &_is) const
void calibrated(const bool &_calib)
virtual void loadObject(std::istream &_is)
const Item & itemAt(unsigned _i) const
std::vector< Target >::const_iterator targetsBegin() const
ClassificationTrainingSet(std::istream &_is)
virtual void add(const Item &_i, const Target &_tg)
std::multimap< unsigned, unsigned, std::less< unsigned > > splitTS
Training sets mode.
ClassificationTrainingSet(const bool &_calib=true, unsigned _n=0)