Changeset 514
- Timestamp:
- Feb 20, 2006, 10:45:34 AM (17 years ago)
- Location:
- trunk
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/classifier/CrossSplitter.cc
r509 r514 44 44 45 45 for (size_t part=0; part<k && i<N; i++, part++) { 46 47 46 std::vector<size_t> training_index; 48 47 std::vector<size_t> validation_index; 49 50 48 for (size_t j=0; j<v.size(); j++) { 51 49 if (j%k==part) -
trunk/lib/classifier/CrossSplitter.h
r509 r514 21 21 /// to the proportions in the whole dataset. In the first \a k 22 22 /// rounds each sample is returned k-1 times, for next round the 23 /// samples are shuffled and... 23 /// samples are shuffled and... In total there are N partitions, in 24 /// other words, each sample is in validation roughly N/k 24 25 /// 25 26 … … 29 30 public: 30 31 /// 31 /// Constructor taking \a target and \a k for k-fold cross 32 /// validation 32 /// @brief Constructor 33 /// 34 /// @parameter Target targets 35 /// @parameter data data to split up in validation and training. 36 /// @parameter N total number of partitions 37 /// @parameter k for k-fold crossvalidation 33 38 /// 34 35 CrossSplitter(const Target& target, const DataLookup2D&, 36 const size_t N, const size_t k); 39 CrossSplitter(const Target& target, const DataLookup2D& data, 40 const size_t N, const size_t k); 37 41 38 42 /// … … 122 126 123 127 std::vector<const DataLookup2D*> training_data_; 128 std::vector<std::vector<size_t> > training_index_; 124 129 std::vector<Target> training_target_; 125 std::vector<std::vector<size_t> > training_index_;126 130 127 131 std::vector<const DataLookup2D*> validation_data_; 132 std::vector<std::vector<size_t> > validation_index_; 128 133 std::vector<Target> validation_target_; 129 std::vector<std::vector<size_t> > validation_index_;130 134 }; 131 135 -
trunk/lib/classifier/SVM.cc
r509 r514 250 250 bool SVM::choose(const theplu::gslapi::vector& E) 251 251 { 252 //std::cout << "e choose\n" ;253 252 // First check for violation among SVs 254 253 // E should be the same for all SVs … … 257 256 sample_.update_first(0); 258 257 if (sample_.nof_sv()>1){ 259 //std::cout << "there is SVs\n"; 258 260 259 double max = E(sample_(0)); 261 260 double min = max; … … 280 279 281 280 // If no violation check among non-support vectors 282 //std::cout << "no violation SVs\n";283 281 284 282 sample_.shuffle(); 285 //std::cout << "randomized\n";286 283 287 284 for (size_t i=sample_.nof_sv(); i<sample_.n();i++){ 288 285 //std::cout << "nr: " << i << std::endl; 289 if (target_. one(sample_(i))){286 if (target_.binary(sample_(i))){ 290 287 if(E(sample_(i)) < E(sample_.value_first()) - 2*tolerance_){ 291 288 sample_.update_second(i); … … 305 302 else{ 306 303 for (size_t i=0; i<sample_.n(); i++) { 307 if (target_. one(sample_(i))){304 if (target_.binary(sample_(i))){ 308 305 for (size_t j=0; j<sample_.n(); j++) { 309 if ( !target_. one(sample_(j)) &&306 if ( !target_.binary(sample_(j)) && 310 307 E(sample_(i)) < E(sample_(j))+2*tolerance_ ){ 311 308 sample_.update_first(i); -
trunk/lib/classifier/SVM.h
r509 r514 189 189 190 190 /// 191 /// @ todo Function calculating bias191 /// @brief calculates the bias term 192 192 /// 193 193 /// @return true if successful … … 214 214 215 215 /// @return 1 if i belong to class one and -1 if i belong to rest 216 inline int target(size_t i) const { return target_. one(i) ? 1 : -1; }216 inline int target(size_t i) const { return target_.binary(i) ? 1 : -1; } 217 217 218 218 gslapi::vector alpha_; … … 233 233 /// @todo The output operator for the SVM class. 234 234 /// 235 std::ostream& operator<< (std::ostream& s, const SVM&);235 //std::ostream& operator<< (std::ostream& s, const SVM&); 236 236 237 237 -
trunk/lib/classifier/Target.cc
r509 r514 17 17 18 18 Target::Target(const std::vector<std::string>& label) 19 : one_(0)20 19 { 21 20 init(label); … … 24 23 Target::Target(const Target& t, 25 24 const std::vector<size_t>& index) 26 : class_map_(t.class_map_) , one_(t.one_)25 : class_map_(t.class_map_) 27 26 { 27 binary_.resize(index.size()); 28 28 classes_.resize(index.size()); 29 29 for (size_t i=0; i<index.size(); i++) { 30 30 assert(index[i]<t.size()); 31 31 classes_[i]=t.classes_[index[i]]; 32 binary_[i]=t.binary_[index[i]]; 32 33 } 33 34 labels_ = t.labels_; … … 35 36 36 37 37 // Peter to Markus, align with gslapi38 38 Target::Target(std::istream& is, char sep) 39 39 throw (utility::IO_error,std::exception) 40 : one_(0)41 40 { 42 41 std::vector<std::string> vec; … … 72 71 class_map_.clear(); 73 72 labels_.clear(); 74 75 73 76 74 for (size_t i=0; i<label.size(); i++) { 77 std::map<std::string,size_t>:: const_iterator iter =75 std::map<std::string,size_t>::iterator iter = 78 76 class_map_.lower_bound(label[i]); 79 77 … … 87 85 else{ 88 86 classes_.push_back(class_map_.size()); 89 // Peter, should use iter to hint on position 90 class_map_.insert(std::make_pair(label[i],classes_[i])); 87 class_map_.insert(iter, std::make_pair(label[i],classes_[i])); 91 88 labels_.push_back(label[i]); 92 89 } 93 90 } 91 // setting binary to false for every class 92 binary_=std::vector<short>(label.size(),0); 93 94 set_binary(0,true); 94 95 95 96 } … … 102 103 // return *this; 103 104 //} 105 106 void Target::set_binary(const size_t target, const bool b) 107 { 108 for (size_t i=0; i<classes_.size(); i++) 109 if (classes_[i]==target) 110 binary_[i]=b; 111 } 112 104 113 105 114 std::ostream& operator<<(std::ostream& s, const Target& a) -
trunk/lib/classifier/Target.h
r509 r514 69 69 70 70 /// 71 /// @return true if class of sample @a i is equal to variable one71 /// Default binary is set to false for all classes except class 0 72 72 /// 73 /// @ see set_one(const size_t one)73 /// @return true if class of sample @a i is set to true 74 74 /// 75 inline bool one(const size_t i) const 76 { assert(i<size()); return classes_[i]==one_; } 75 /// @see set_binary 76 /// 77 inline bool binary(const size_t i) const 78 { assert(i<size()); return binary_[i]; } 77 79 78 80 /// 79 /// Function to set variable one. This variable is used in 2-class 80 /// algorithm in rder to use the Targer object in a one-versus-all 81 /// manner. 81 /// Class @a target is set to @a b. Default is binary set to false 82 /// for each class. 82 83 /// 83 /// @see one(const size_t i) 84 /// 85 inline void set_one(const size_t one) { assert(one<size()); one_=one; } 84 void set_binary(const size_t target, const bool b); 86 85 87 86 /// … … 95 94 const size_t size(const std::string& label) const; 96 95 97 //98 //@brief sort99 //100 //inline void sort(void) { std::sort(classes_.begin(), classes_.end()); }101 102 96 /// 103 97 /// @return the class of @a sample … … 106 100 { assert(sample<size()); return classes_[sample]; } 107 101 108 ///109 /// @brief assignment operator110 ///111 //const Target& operator=(const Target& other);112 102 113 103 private: 104 std::vector<short> binary_; // avoid using vector<bool> 114 105 std::vector<size_t> classes_; // class of sample i 115 106 std::map<std::string,size_t> class_map_; 116 107 std::vector<std::string> labels_; // label of class i 117 size_t one_;118 108 119 109 void init(const std::vector<std::string>&); -
trunk/lib/statistics/Fisher.cc
r509 r514 116 116 a_=b_=c_=d_=0; 117 117 for (size_t i=0; i<target.size(); i++) 118 if (target. one(i))118 if (target.binary(i)) 119 119 if (value(i)>value_cutoff_) 120 120 a_++; … … 143 143 a_=b_=c_=d_=0; 144 144 for (size_t i=0; i<target.size(); i++) 145 if (target. one(i))145 if (target.binary(i)) 146 146 if (value(i)>value_cutoff_) 147 147 a_+=weight(i); -
trunk/lib/statistics/FoldChange.cc
r509 r514 39 39 40 40 for (size_t i=0; i<value.size(); i++) 41 if (target. one(i))41 if (target.binary(i)) 42 42 pos.add(value(i)); 43 43 else … … 58 58 59 59 for (size_t i=0; i<value.size(); i++) 60 if (target. one(i))60 if (target.binary(i)) 61 61 pos.add(value(i),weight(i)); 62 62 else -
trunk/lib/statistics/Pearson.cc
r509 r514 46 46 AveragerPair ap; 47 47 for (size_t i=0; i<target.size(); i++){ 48 if (target. one(i))48 if (target.binary(i)) 49 49 ap.add(1, value(i)); 50 50 else … … 66 66 AveragerPairWeighted ap; 67 67 for (size_t i=0; i<target.size(); i++){ 68 if (target. one(i))68 if (target.binary(i)) 69 69 ap.add(1, value(i),1,weight(i)); 70 70 else -
trunk/lib/statistics/ROC.cc
r509 r514 75 75 vec_pair_.reserve(target.size()); 76 76 for (size_t i=0; i<target.size(); i++) 77 vec_pair_.push_back(std::make_pair(target. one(i),value(i)));77 vec_pair_.push_back(std::make_pair(target.binary(i),value(i))); 78 78 79 79 std::sort(vec_pair_.begin(),vec_pair_.end(), … … 110 110 for (unsigned int i=0; i<target.size(); i++) 111 111 if (weight(i)) 112 vec_pair_.push_back(std::make_pair(target. one(i),value(i)));112 vec_pair_.push_back(std::make_pair(target.binary(i),value(i))); 113 113 114 114 std::sort(vec_pair_.begin(),vec_pair_.end(), … … 120 120 121 121 for (size_t i=0; i<n(); i++) 122 if (target. one(i))122 if (target.binary(i)) 123 123 for (size_t j=0; j<n(); j++) 124 if (!target. one(j)){124 if (!target.binary(j)){ 125 125 if (value(i)>value(j)) 126 126 area_+=weight(i)*weight(j); -
trunk/lib/statistics/tScore.cc
r509 r514 27 27 dof_=target.size()-2; 28 28 for(size_t i=0; i<target.size(); i++){ 29 if (target. one(i))29 if (target.binary(i)) 30 30 positive.add(value(i)); 31 31 else … … 52 52 dof_=target.size()-2; 53 53 for(size_t i=0; i<target.size(); i++){ 54 if (target. one(i))54 if (target.binary(i)) 55 55 positive.add(value(i),weight(i)); 56 56 else -
trunk/test/crossvalidation_test.cc
r509 r514 27 27 bool ok = true; 28 28 29 30 29 std::vector<std::string> label(10,"default"); 31 30 label[2]=label[7]="white"; -
trunk/test/score_test.cc
r509 r514 45 45 ok = false; 46 46 } 47 target.set_one(1); 47 target.set_binary(0,false); 48 target.set_binary(1,true); 48 49 area = roc.score(target, value); 49 50 if (area!=1.0){ -
trunk/test/svm_test.cc
r509 r514 59 59 double tmp=0; 60 60 for (size_t i=0; i<target2.size(); i++) 61 if (target2. one(i))61 if (target2.binary(i)) 62 62 tmp += classifier2.alpha()(i); 63 63 else … … 121 121 for (unsigned int i=0; i<target.size(); i++){ 122 122 if (output(i)*target(i) < 1){ 123 if (target. one(i))123 if (target.binary(i)) 124 124 slack += 1 - output(i); 125 125 else -
trunk/test/target_test.cc
r509 r514 60 60 *error << "Error: target(20)!=2" << std::endl; 61 61 } 62 if (!target. one(0)){62 if (!target.binary(0)){ 63 63 ok=false; 64 *error << "Error: target. one(0) not true" << std::endl;64 *error << "Error: target.binary(0) not true" << std::endl; 65 65 } 66 if (target. one(20)){66 if (target.binary(20)){ 67 67 ok=false; 68 *error << "Error: target. one(20) not false" << std::endl;68 *error << "Error: target.binary(20) not false" << std::endl; 69 69 } 70 target.set_ one(2);71 if (!target. one(20)){70 target.set_binary(2,true); 71 if (!target.binary(20)){ 72 72 ok=false; 73 *error << "Error: target. one(20) not true" << std::endl;73 *error << "Error: target.binary(20) not true" << std::endl; 74 74 } 75 75 std::ifstream is("data/rank_target.txt");
Note: See TracChangeset
for help on using the changeset viewer.