Changeset 714 for trunk/yat/classifier


Ignore:
Timestamp:
Dec 22, 2006, 1:10:54 AM (17 years ago)
Author:
Jari Häkkinen
Message:

Addresses #170..

Location:
trunk/yat/classifier
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/SVindex.cc

    r680 r714  
    5151    for (size_t i=0; i<vec_.size(); i++)
    5252      vec_[i]=i;
     53  }
     54
     55  size_t SVindex::index_first(void) const
     56  {
     57    assert(index_first_<size());
     58    return index_first_;
     59  }
     60
     61  size_t SVindex::index_second(void) const
     62  {
     63    assert(index_second_<size());
     64    return index_second_;
    5365  }
    5466
     
    7082  }
    7183
    72   void SVindex::sv_first(void)
    73   {
    74     // if already sv, do nothing
    75     if (index_first_<nof_sv())
    76       return;
    77 
    78     // swap elements
    79     if(index_second_==nof_sv_){
    80       index_second_=index_first_;
    81     }
    82     vec_[index_first_]=vec_[nof_sv_];
    83     vec_[nof_sv_]=value_first_;
    84     index_first_ = nof_sv_;
    85 
    86     nof_sv_++;
    87 
    88   }
    89 
    90   void SVindex::sv_second(void)
    91   {
    92     // if already sv, do nothing
    93     if (index_second_<nof_sv())
    94       return;
    95 
    96     // swap elements
    97     if(index_first_==nof_sv_){
    98       index_first_=index_second_;
    99     }
    100 
    101     vec_[index_second_]=vec_[nof_sv_];
    102     vec_[nof_sv_]=value_second_;
    103     index_second_=nof_sv_;
    104 
    105     nof_sv_++;
     84  size_t SVindex::nof_sv(void) const
     85  {
     86    return nof_sv_;
    10687  }
    10788
     
    143124  }
    144125
     126  size_t SVindex::size(void) const
     127  {
     128    return vec_.size();
     129  }
     130
     131  void SVindex::sv_first(void)
     132  {
     133    // if already sv, do nothing
     134    if (index_first_<nof_sv())
     135      return;
     136
     137    // swap elements
     138    if(index_second_==nof_sv_){
     139      index_second_=index_first_;
     140    }
     141    vec_[index_first_]=vec_[nof_sv_];
     142    vec_[nof_sv_]=value_first_;
     143    index_first_ = nof_sv_;
     144
     145    nof_sv_++;
     146
     147  }
     148
     149  void SVindex::sv_second(void)
     150  {
     151    // if already sv, do nothing
     152    if (index_second_<nof_sv())
     153      return;
     154
     155    // swap elements
     156    if(index_first_==nof_sv_){
     157      index_first_=index_second_;
     158    }
     159
     160    vec_[index_second_]=vec_[nof_sv_];
     161    vec_[nof_sv_]=value_second_;
     162    index_second_=nof_sv_;
     163
     164    nof_sv_++;
     165  }
     166
    145167  void SVindex::update_first(const size_t i)
    146168  {
     
    157179  }
    158180
     181  size_t SVindex::value_first(void) const
     182  {
     183    assert(value_first_<size());
     184    return value_first_;
     185  }
     186
     187  size_t SVindex::value_second(void) const
     188  {
     189    assert(value_second_<size());
     190    return value_second_;
     191  }
     192
     193  size_t SVindex::operator()(size_t i) const
     194  {
     195    assert(i<size());
     196    assert(vec_[i]<size());
     197    return vec_[i];
     198  }
     199
    159200}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/SVindex.h

    r706 r714  
    5050
    5151    // @return index_first
    52     inline size_t index_first(void) const { return index_first_; }
     52    size_t index_first(void) const;
    5353
    5454    // @return index_second
    55     inline size_t index_second(void) const { return index_second_; }
     55    size_t index_second(void) const;
    5656
    5757    // synch the object against alpha
    5858    void init(const utility::vector& alpha, const double);
    5959
    60     // @return nof samples
    61     inline size_t size(void) const { return vec_.size(); }
    62 
    6360    // @return nof support vectors
    64     inline size_t nof_sv(void) const { return nof_sv_; }
     61    size_t nof_sv(void) const;
    6562
    6663    // making first to an nsv. If already sv, nothing happens.
     
    7370    // nof_sv_ (the first nsv)
    7471    void shuffle(void);
     72
     73    // @return nof samples
     74    size_t size(void) const;
    7575
    7676    // making first to a sv. If already sv, nothing happens.
     
    8787
    8888    // @return value_first
    89     inline size_t value_first(void) const { return value_first_; }
     89    size_t value_first(void) const;
    9090
    9191    // @return const ref value_second
    92     inline size_t value_second(void) const { return value_second_; }
     92    size_t value_second(void) const;
    9393
    94     inline size_t operator()(size_t i) const { return vec_[i]; }
     94    size_t operator()(size_t i) const;
    9595
    9696  private:
  • trunk/yat/classifier/Target.cc

    r680 r714  
    8888  }
    8989
     90  bool Target::binary(size_t i) const
     91  {
     92    assert(i<size());
     93    return binary_[operator()(i)];
     94  }
     95
     96  const std::map<std::string,size_t>& Target::classes(void) const
     97  {
     98    return class_map_;
     99  }
     100
    90101  void Target::init(const std::vector<std::string>& label)
    91102  {
     
    118129  }
    119130
     131  const std::vector<std::string>& Target::labels(void)
     132  {
     133    return labels_;
     134  }
     135
     136  const size_t Target::nof_classes(void) const
     137  {
     138    return classes().size();
     139  }
     140
    120141  void Target::random_shuffle(void)
    121142  {
    122143    random::DiscreteUniform d;
    123144    std::random_shuffle(classes_.begin(), classes_.end(),d);
     145  }
     146
     147  void Target::set_binary(size_t i, bool b)
     148  {
     149    assert(i<nof_classes());
     150    binary_[i]=b;
    124151  }
    125152
     
    133160  }
    134161 
     162  size_t Target::size(void) const
     163  {
     164    return classes_.size();
     165  }
     166
    135167  const size_t Target::size(size_t cl) const
    136168  {
     
    138170  }
    139171
    140 
    141   std::ostream& operator<<(std::ostream& s, const Target& a)
    142   {
    143     for (size_t j = 0; j < a.size(); ++j) {
    144       s << a(j);
    145       if ( (j+1)<a.size() )
    146         s << " ";
    147     }
    148 
    149     return s;
    150   }
    151 
     172  size_t Target::operator()(size_t sample) const
     173  {
     174    assert(sample<size());
     175    return classes_[sample];
     176  }
     177
     178  size_t Target::operator[](size_t sample) const
     179  {
     180    return this->operator()(sample);
     181  }
    152182
    153183  const Target& Target::operator=(const Target& other)
     
    161191
    162192
     193  std::ostream& operator<<(std::ostream& s, const Target& a)
     194  {
     195    for (size_t j = 0; j < a.size(); ++j) {
     196      s << a(j);
     197      if ( (j+1)<a.size() )
     198        s << " ";
     199    }
     200
     201    return s;
     202  }
     203
     204
    163205}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/Target.h

    r706 r714  
    8080    /// @return a map with label as key and class as value.
    8181    ///
    82     inline const std::map<std::string,size_t>& classes(void) const
    83     { return class_map_; }
     82    const std::map<std::string,size_t>& classes(void) const;
    8483   
    8584    ///
     
    8887    /// @return number of classes
    8988    ///
    90     inline const size_t nof_classes(void) const { return classes().size(); }
     89    const size_t nof_classes(void) const;
    9190
    9291    ///
     
    9897    /// @see set_binary
    9998    ///
    100     inline bool binary(const size_t i) const { return binary_[operator()(i)]; }
     99    bool binary(size_t i) const;
    101100   
    102101    ///
    103102    /// @return vector of labels for classes
    104103    ///
    105     inline const std::vector<std::string>& labels() { return labels_; }
     104    const std::vector<std::string>& labels(void);
    106105
    107106    ///
     
    110109    /// 0 which is set to true.
    111110    ///
    112     inline void set_binary(const size_t i, const bool b) { binary_[i]=b; }
     111    void set_binary(size_t i, bool b);
    113112
    114113    ///
     
    123122    /// @return number of samples
    124123    ///
    125     inline size_t size(void) const { return classes_.size(); }
     124    size_t size(void) const;
    126125
    127126    ///
     
    139138    /// @return the class of @a sample
    140139    ///
    141     inline size_t operator()(const size_t sample) const
    142     { return classes_[sample]; }
     140    size_t operator()(size_t sample) const;
    143141   
    144142    ///
    145143    /// @return the class of @a sample
    146144    ///
    147     inline size_t operator[](const size_t sample) const
    148     { return this->operator()(sample); }
     145    size_t operator[](size_t sample) const;
    149146
    150147    ///
Note: See TracChangeset for help on using the changeset viewer.