| 188 | } |
| 189 | |
| 190 | void DataSet::group_classes(bool classification) { |
| 191 | if (classification) { |
| 192 | start_.clear(); |
| 193 | count_.clear(); |
| 194 | label_.clear(); |
| 195 | perm_.clear(); |
| 196 | vector<int> dataLabel(y_.size());//temporary labels of all the instances |
| 197 | |
| 198 | //get the class labels; count the number of instances in each class. |
| 199 | for (int i = 0; i < y_.size(); ++i) { |
| 200 | int j; |
| 201 | for (j = 0; j < label_.size(); ++j) { |
| 202 | if (y_[i] == label_[j]) { |
| 203 | count_[j]++; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | dataLabel[i] = j; |
| 208 | //if the label is unseen, add it to label vector. |
| 209 | if (j == label_.size()) { |
| 210 | //real to int conversion is safe, because group_classes only used in classification |
| 211 | label_.push_back(int(y_[i])); |
| 212 | count_.push_back(1); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | //logically put instances of the same class consecutively. |
| 217 | start_.push_back(0); |
| 218 | for (int i = 1; i < count_.size(); ++i) { |
| 219 | start_.push_back(start_[i - 1] + count_[i - 1]); |
| 220 | } |
| 221 | vector<int> start_copy(start_); |
| 222 | perm_ = vector<int>(y_.size());//index of each instance in the original array |
| 223 | for (int i = 0; i < y_.size(); ++i) { |
| 224 | perm_[start_copy[dataLabel[i]]] = i; |
| 225 | start_copy[dataLabel[i]]++; |
| 226 | } |
| 227 | } else { |
| 228 | for (int i = 0; i < instances_.size(); ++i) { |
| 229 | perm_.push_back(i); |
| 230 | } |
| 231 | start_.push_back(0); |
| 232 | count_.push_back(instances_.size()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | size_t DataSet::n_instances() const {//return the total number of instances |
| 237 | return total_count_; |