/***************************************************************************** Copyright (c) 2005, Uwe Schmitt (http://www.procoders.net) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of procoders.net nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ******************************************************************************/ #ifndef __PCSVM_PROBLEM__ #define __PCSVM_PROBLEM__ #include #include #include #include #include "Vector.hpp" namespace pcsvm { using namespace pcsvm; template class Example { public: Example(const SparseVector &vec, LabelT lab): vector(vec), label(lab) {}; Example(const Vector &vec, LabelT lab): vector(vec), label(lab) {}; // Example(): vector(), label() {}; std::string to_str() const; VectorBase vector; LabelT label; }; template class Problem { public: // folgendes keine memberfunction, da msvc7 mit geschachtelten klassen nicht immer // zurechtkommt... doof.... typedef std::vector > ExampleVector; typedef typename std::vector >::iterator iterator; typedef typename std::vector >::const_iterator const_iterator; Problem(unsigned int reserve=1000): _exVec() { _exVec.reserve(reserve >= 100 ? reserve: 100); } // Adapter-Methoden inline const Example& operator[](int idx) const { return _exVec[idx]; } inline Example& operator[](int idx) { return _exVec[idx]; } inline const Example& at(int idx) const { return _exVec.at(idx); } inline Example& at(int idx) { return _exVec.at(idx); } inline typename ExampleVector::iterator begin() { return _exVec.begin(); } inline typename ExampleVector::const_iterator begin() const { return _exVec.begin(); } inline typename ExampleVector::iterator end() { return _exVec.end(); } inline typename ExampleVector::const_iterator end() const { return _exVec.end(); } // inline void push_back(const Example& ex) { _exVec.push_back(ex); } inline void push_back(const VectorBase& ex, const LabelT &label) { _exVec.push_back(Example(ex,label)); } inline bool empty() const { return _exVec.empty(); } inline void clear() { _exVec.clear(); } inline std::size_t size() const { return _exVec.size(); } inline void reserve(unsigned int l) { _exVec.reserve(l); } inline void swap(int i, int j) { Example tmp = _exVec[i]; _exVec[i]=_exVec[j]; _exVec[j]=tmp; } // Klassenspezielle Mathoden //operator const char*() const; std::string to_str() const; void normalize(); // get and put for mapping via python ! inline void put(unsigned int idx, Example ex) { _exVec.at(idx)=ex; } inline Example get(unsigned int idx) { return _exVec.at(idx); } private: ExampleVector _exVec; }; template std::string Problem::to_str() const { std::ostringstream res; res << ""; return res.str(); //.c_str(); } template void Problem::normalize() { int maxidx=-1; int _len; iterator it; typename VectorBase::const_item_iterator iit; for (it=begin(); it!=end(); ++it) { _len = it->vector.lastIndex(); if (_len>maxidx) maxidx=_len; } std::vector maxvec(maxidx+1); VectorBase * _svp; float _val; unsigned int _idx; // vector-weise fuer jede koordinate das absolute maximum bestimmen for (it=begin(); it!=end(); ++it) { _svp = &(it->vector); for (iit=_svp->begin(); iit!=_svp->end(); ++iit) { _idx = iit->index; _val = std::fabs(iit->value); if (_val>maxvec[_idx]) maxvec[_idx] = _val; } } // fuer jedes example ueber jede koordinate des example-vektors // normieren float _scal; for (it=begin(); it!=end(); ++it) { _svp = &(it->vector); for (iit=_svp->begin(); iit!=_svp->end(); ++iit) { _idx = iit->index; _scal= maxvec[_idx]; // _scal == 0 dürfte wegen struktur von sparsevector eigentlich nicht passieren if (_scal) iit->value /= maxvec[_idx]; } } } template std::string Example::to_str() const { std::ostringstream res; res << " " << label; res << ">"; return res.str(); // ._cstr(); } template std::ostream & operator<<(std::ostream & os, const Example & ex) { os << ex.to_str(); return os; } template std::ostream & operator<<(std::ostream & os, const Problem & ex) { os << ex.to_str(); return os; } typedef Example ClassificationExample; typedef Example RegressionExample; typedef Problem ClassificationProblem; typedef Problem RegressionProblem; typedef Example SparseClassificationExample; typedef Example SparseRegressionExample; typedef Problem SparseClassificationProblem; typedef Problem SparseRegressionProblem; } #endif