Changeset 593
- Timestamp:
- Aug 25, 2006, 2:59:21 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/classifier/DataLookup1D.cc
r537 r593 6 6 #include <cassert> 7 7 #include <iostream> 8 #include <iomanip> 8 9 9 10 namespace theplu { … … 36 37 } 37 38 39 38 40 double DataLookup1D::operator*(const DataLookup1D& other) const 39 41 { … … 45 47 } 46 48 47 std::ostream& operator<< (std::ostream& os, const DataLookup1D& x) 49 50 std::ostream& operator<<(std::ostream& os, const DataLookup1D& x) 48 51 { 49 52 os.setf(std::ios::dec); 50 53 os.precision(12); 51 for (size_t i = 0; i < x.size(); ++i) { 54 55 for (size_t i=0; i<x.size(); ++i) { 52 56 os << x(i); 53 if ( (i+1)<x.size())54 os << " ";57 if ((i+1)<x.size()) 58 os << os.fill(); 55 59 } 56 60 return os; 57 61 } 62 58 63 }} // of namespace classifier and namespace theplu -
trunk/c++_tools/classifier/DataLookup1D.h
r581 r593 87 87 const bool owner_; 88 88 89 }; 89 }; 90 90 91 /// 91 /// The output operator DataLook1D92 /// @brief The output operator for DataLookup1D. 92 93 /// 93 /// @todo test that output can be used from gslapi::vector istream constructor 94 std::ostream& operator<< (std::ostream& s, const DataLookup1D&); 94 /** 95 * Elements are separated by the character from the omanip fill. 96 * The following example will write the elements separated by tab. 97 * 98 @verbatim 99 char prev=s.fill('\t'); 100 s << v; 101 s.fill(prev); 102 @endverbatim 103 */ 104 std::ostream& operator<<(std::ostream& s, const DataLookup1D& v); 95 105 96 106 }} // of namespace classifier and namespace theplu -
trunk/c++_tools/classifier/EnsembleBuilder.cc
r559 r593 30 30 cross_splitter_.reset(); 31 31 while(cross_splitter_.more()) { 32 const DataLookup2D& training=cross_splitter_.training_data();33 const Target& targets=cross_splitter_.training_target();34 35 32 SupervisedClassifier* classifier= 36 mother_.make_classifier( training,targets);33 mother_.make_classifier(cross_splitter_); 37 34 classifier->train(); 38 35 classifier_.push_back(classifier); -
trunk/c++_tools/classifier/MatrixLookup.cc
r592 r593 162 162 s << m(i,j); 163 163 if (j<m.columns()-1) 164 s << "\t";164 s << s.fill(); 165 165 else if (i<m.rows()-1) 166 166 s << "\n"; -
trunk/c++_tools/classifier/NCC.cc
r584 r593 3 3 #include <c++_tools/classifier/NCC.h> 4 4 5 #include <c++_tools/classifier/CrossSplitter.h> 5 6 #include <c++_tools/classifier/DataLookup1D.h> 6 7 #include <c++_tools/classifier/DataLookup2D.h> … … 22 23 NCC::NCC(const MatrixLookup& data, const Target& target, 23 24 const statistics::Distance& distance) 24 : SupervisedClassifier(target), distance_(distance), matrix_(data) 25 { 25 : SupervisedClassifier(target), distance_(distance), matrix_(data), 26 weighted_(false), 27 weights_(new MatrixLookup(data.rows(),data.columns(),1.0)) 28 { 26 29 } 30 31 NCC::NCC(const MatrixLookup& data, const Target& target, 32 const statistics::Distance& distance, const MatrixLookup& weights) 33 : SupervisedClassifier(target), distance_(distance), matrix_(data), 34 weighted_(true),weights_(&weights) 35 { 36 } 37 27 38 28 39 NCC::NCC(const MatrixLookup& data, const Target& target, … … 30 41 statistics::Score& score, size_t nof_inputs) 31 42 : SupervisedClassifier(target, &score, nof_inputs), 32 distance_(distance), matrix_(data) 43 distance_(distance), matrix_(data),weighted_(false), 44 weights_(new MatrixLookup(data.rows(),data.columns(),1.0)) 33 45 { 34 46 } 35 47 36 NCC::~NCC() 48 NCC::NCC(const MatrixLookup& data, const Target& target, 49 const statistics::Distance& distance, 50 const MatrixLookup& weights, 51 statistics::Score& score, size_t nof_inputs) 52 : SupervisedClassifier(target, &score, nof_inputs), 53 distance_(distance), matrix_(data),weighted_(true), 54 weights_(&weights) 37 55 { 38 56 } 39 57 40 58 59 NCC::~NCC() 60 { 61 if(!weighted_) 62 if(weights_) 63 delete weights_; 64 else 65 std::cerr << "Error in NCC implementation: probably a constructor" 66 << " should be debugged to make sure a unity weight matrix is" 67 << " dynamically allocated for all unweighted cases" 68 << std::endl; 69 } 70 71 41 72 SupervisedClassifier* 42 NCC::make_classifier(const DataLookup2D& data, 43 const Target& target) const 73 NCC::make_classifier(const CrossSplitter& cs) const 44 74 { 45 const MatrixLookup& tmp = dynamic_cast<const MatrixLookup&>(data); 46 47 NCC* ncc= new NCC(tmp,target,this->distance_); 75 const MatrixLookup& training_data = 76 dynamic_cast<const MatrixLookup&>(cs.training_data()); 77 NCC* ncc=0; 78 if(cs.weighted()) { 79 ncc= new NCC(training_data,cs.training_target(),this->distance_, 80 cs.training_weight()); 81 } 82 else { 83 ncc= new NCC(training_data,cs.training_target(),this->distance_); 84 } 48 85 ncc->score_=this->score_; 49 86 ncc->nof_inputs_=this->nof_inputs_; … … 61 98 size_t rows=matrix_.rows(); 62 99 if(score_) { 63 // Markus: missing values should not be handled here, but a weight matrix 64 // should be supported throughout the classifier class structure. 65 gslapi::matrix weight(matrix_.rows(),matrix_.columns(),0.0); 66 for(size_t i=0; i<matrix_.rows(); i++) 67 for(size_t j=0; j<matrix_.columns(); j++) 68 if(!std::isnan(matrix_(i,j))) 69 weight(i,j)=1.0; 70 MatrixLookup weightview(weight); 71 ranker_=new InputRanker(matrix_, target_, *score_, weightview); 100 ranker_=new InputRanker(matrix_, target_, *score_, *weights_); 72 101 rows=nof_inputs_; 73 102 } … … 77 106 for(size_t j=0; j<matrix_.columns(); j++) { 78 107 double value=matrix_(i,j); 79 if(score_) 108 double weight=(*weights_)(i,j); 109 if(score_) { 80 110 value=matrix_(ranker_->id(i),j); 81 if(!std::isnan(value)) { 82 centroids_(i,target_(j)) += value; 83 nof_in_class(i,target_(j))++; 111 weight=(*weights_)(ranker_->id(i),j); 112 } 113 if(weight) { 114 centroids_(i,target_(j)) += value*weight; 115 nof_in_class(i,target_(j))+=weight; 84 116 } 85 117 } -
trunk/c++_tools/classifier/NCC.h
r526 r593 19 19 namespace classifier { 20 20 21 class Target;21 class CrossSplitter; 22 22 class DataLookup1D; 23 23 class DataLookup2D; 24 24 class MatrixLookup; 25 class Target; 25 26 26 27 /// … … 33 34 public: 34 35 /// 35 /// Constructor taking the training data, the target vector and36 /// the distance measure as input. 36 /// Constructor taking the training data, the target vector, and 37 /// the distance measure as input. 37 38 /// 38 39 NCC(const MatrixLookup&, const Target&, const statistics::Distance&); 40 41 /// 42 /// Constructor taking the training data, the target vector, the 43 /// distance measure, and a weight matrix for the training data as 44 /// input. 45 /// 46 NCC(const MatrixLookup&, const Target&, const statistics::Distance&, 47 const MatrixLookup&); 39 48 49 40 50 41 51 /// 42 52 /// Constructor taking the training data, the target vector, the 43 53 /// distance measure, the score used to rank data inputs, and the 44 /// number of top ranked data inputs to use in the classification. 54 /// number of top ranked data inputs to use in the classification 55 /// as input 45 56 /// 46 57 NCC(const MatrixLookup&, const Target&, const statistics::Distance&, 47 58 statistics::Score&, const size_t); 59 60 /// 61 /// Constructor taking the training data, the target vector, the 62 /// distance measure, a weight matrix for the training data, the 63 /// score used to rank data inputs, and the number of top ranked 64 /// data inputs to use in the classification as input 65 /// 66 NCC(const MatrixLookup&, const Target&, const statistics::Distance&, 67 const MatrixLookup&, statistics::Score&, const size_t); 68 48 69 49 70 virtual ~NCC(); … … 55 76 56 77 inline SupervisedClassifier* 57 make_classifier(const DataLookup2D&, const Target&) const;78 make_classifier(const CrossSplitter&) const; 58 79 59 80 /// … … 81 102 const statistics::Distance& distance_; 82 103 const MatrixLookup& matrix_; 83 104 bool weighted_; 105 const MatrixLookup* weights_; 84 106 }; 85 107 -
trunk/c++_tools/classifier/SVM.cc
r571 r593 3 3 #include <c++_tools/classifier/SVM.h> 4 4 5 #include <c++_tools/classifier/CrossSplitter.h> 5 6 #include <c++_tools/classifier/DataLookup2D.h> 6 7 #include <c++_tools/classifier/InputRanker.h> … … 111 112 112 113 113 SupervisedClassifier* SVM::make_classifier(const DataLookup2D& data, 114 const Target& target) const 115 { 114 SupervisedClassifier* SVM::make_classifier(const CrossSplitter& cs) const 115 { 116 // Peter, should check success of dynamic_cast 117 const KernelLookup& data = 118 dynamic_cast<const KernelLookup&>(cs.training_data()); 119 const Target& target=cs.training_target(); 120 116 121 assert(data.rows()==data.columns()); 117 122 assert(data.columns()==target.size()); 118 // Peter, should check success of dynamic_cast119 const KernelLookup& tmp = dynamic_cast<const KernelLookup&>(data);120 123 SVM* sc; 121 124 if (ranker_) 122 sc = new SVM( tmp,target,*score_,nof_inputs_);125 sc = new SVM(data,target,*score_,nof_inputs_); 123 126 else 124 sc = new SVM( tmp,target);127 sc = new SVM(data,target); 125 128 126 129 -
trunk/c++_tools/classifier/SVM.h
r592 r593 17 17 namespace theplu { 18 18 namespace classifier { 19 20 // forward declarations 21 class CrossSplitter; 19 22 20 23 // @internal Class keeping track of which samples are support vectors and … … 136 139 /// 137 140 SupervisedClassifier* 138 make_classifier(const DataLookup2D&, const Target&) const;141 make_classifier(const CrossSplitter&) const; 139 142 140 143 /// -
trunk/c++_tools/classifier/SupervisedClassifier.h
r592 r593 20 20 namespace classifier { 21 21 22 class CrossSplitter; 22 23 class DataLookup2D; 23 class InputRanker; class Target;24 24 class InputRanker; 25 class Target; 25 26 26 27 /// … … 59 60 /// 60 61 virtual SupervisedClassifier* 61 make_classifier(const DataLookup2D&, const Target&) const =0;62 make_classifier(const CrossSplitter&) const =0; 62 63 63 64 -
trunk/c++_tools/gslapi/matrix.cc
r570 r593 231 231 s << m(i,j); 232 232 if (j<m.columns()-1) 233 s << "\t";233 s << s.fill(); 234 234 else if (i<m.rows()-1) 235 235 s << "\n"; -
trunk/c++_tools/gslapi/vector.cc
r581 r593 258 258 s << a[j]; 259 259 if ( (j+1)<a.size() ) 260 s << " ";260 s << s.fill(); 261 261 } 262 262 -
trunk/c++_tools/statistics/AveragerPair.h
r582 r593 6 6 #include <c++_tools/statistics/Averager.h> 7 7 8 #include <cassert> 8 9 #include <cmath> 9 10 #include <utility> -
trunk/test/data_lookup_1d_test.cc
r565 r593 8 8 #include <iostream> 9 9 #include <vector> 10 #include <cmath> 10 11 11 12 using namespace theplu; … … 39 40 index_even.push_back(4); 40 41 classifier::MatrixLookup m1(gsl_m1,index_odd,index_even); 42 *error << gsl_m1 << std::endl << '\n'; 43 *error << m1 << std::endl; 41 44 *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n" 42 45 << " const size_t, const bool)..."; … … 93 96 *error << "Ok" << std::endl; 94 97 98 *error << "Testing that output from ostream operator for DataLookup1D" 99 << " can be used by the gslapi::vector istream constructor..."; 100 101 // First with a vector with no missing values separated by ' '. 102 std::ofstream my_out("data/tmp_test_datalookup1D.txt"); 103 my_out << v1; 104 my_out.close(); 105 std::ifstream is("data/tmp_test_datalookup1D.txt"); 106 gslapi::vector v5(is); 107 is.close(); 108 if (v5.size()!=v1.size() || v5(0)!=v1(0) || v5(1)!=v1(1) || 109 v5(2)!=v1(2)) { 110 ok=false; 111 *error << "\nERROR\n" << std::endl; 112 } 113 unlink("data/tmp_test_datalookup1D.txt"); 114 115 // Second with a vector with a missing value separated by '\t'. 116 gsl_m1(3,0)=std::numeric_limits<double>::quiet_NaN(); 117 classifier::DataLookup1D v6(m1,1, true); 118 my_out.open("data/tmp_test_datalookup1D.txt"); 119 char prev=my_out.fill('\t'); 120 my_out << v1; 121 my_out.fill(prev); 122 my_out.close(); 123 is.open("data/tmp_test_datalookup1D.txt"); 124 gslapi::vector v7(is,'\t'); 125 is.close(); 126 if (v7.size()!=v6.size() || !std::isnan(v7(1))) { 127 ok=false; 128 *error << "\nERROR\n" 129 << "size: " << v7.size() << " expected " << v6.size() << "\n" 130 << "v7(1): " << v7(1) << " expected nan\n" 131 << std::endl; 132 } 133 unlink("data/tmp_test_datalookup1D.txt"); 134 135 95 136 if (ok) 96 137 *error << "Ok." << std::endl; -
trunk/test/ncc_test.cc
r584 r593 34 34 bool ok = true; 35 35 36 37 36 std::ifstream is("data/sorlie_centroids.txt"); 38 37 gslapi::matrix data(is,'\t'); … … 43 42 is.close(); 44 43 45 44 // Generate weight matrix with 0 for missing values and 1 for others. 45 gslapi::matrix weights(data.rows(),data.columns(),0.0); 46 for(size_t i=0;i<data.rows();++i) 47 for(size_t j=0;j<data.columns();++j) 48 if(!std::isnan(data(i,j))) 49 weights(i,j)=1.0; 50 46 51 classifier::MatrixLookup dataview(data); 52 classifier::MatrixLookup weightview(weights); 47 53 statistics::PearsonDistance pearson; 48 classifier::NCC ncc(dataview,targets,pearson );54 classifier::NCC ncc(dataview,targets,pearson,weightview); 49 55 ncc.train(); 50 56 … … 78 84 *error << igp.score() << std::endl; 79 85 86 if(ok) 87 *error << "OK" << std::endl; 88 80 89 81 90 if (error!=&std::cerr) 82 91 delete error; 83 92 84 if(ok) 93 if(ok) 85 94 return 0; 86 95 return -1;
Note: See TracChangeset
for help on using the changeset viewer.