Changeset 658 for trunk/c++_tools
- Timestamp:
- Sep 25, 2006, 4:12:03 AM (17 years ago)
- Location:
- trunk/c++_tools/classifier
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/classifier/DataLookup2D.cc
r640 r658 15 15 16 16 17 DataLookup2D::DataLookup2D( )17 DataLookup2D::DataLookup2D(const bool own) 18 18 : ref_count_(NULL) 19 19 { 20 if (own) 21 ref_count_ = new u_int(1); 20 22 } 21 23 -
trunk/c++_tools/classifier/DataLookup2D.h
r656 r658 31 31 /// Default constructor. 32 32 /// 33 DataLookup2D( void);33 DataLookup2D(const bool own=false); 34 34 35 35 -
trunk/c++_tools/classifier/Kernel.cc
r640 r658 13 13 namespace classifier { 14 14 15 Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf) 16 : data_(&data), data_w_(0), kf_(&kf), data_owner_(false), 15 Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf, 16 const bool own) 17 : data_(&data), data_w_(0), kf_(&kf), data_owner_(own), 17 18 weight_owner_(false) 18 19 { … … 20 21 21 22 22 Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf) 23 : data_(&data), data_w_(&data), kf_(&kf), data_owner_(false), 24 weight_owner_(false) 23 Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 24 const bool own) 25 : data_(&data), data_w_(&data), kf_(&kf), data_owner_(own), 26 weight_owner_(own) 25 27 { 26 28 } -
trunk/c++_tools/classifier/Kernel.h
r648 r658 47 47 /// @note Can not handle NaNs. 48 48 /// 49 Kernel(const MatrixLookup& data, const KernelFunction& kf); 49 Kernel(const MatrixLookup& data, const KernelFunction& kf, 50 const bool own=false); 50 51 51 52 /// … … 58 59 /// @note Can not handle NaNs. 59 60 /// 60 Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf); 61 Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf, 62 const bool own=false); 61 63 62 64 /// … … 105 107 106 108 /// 109 /// An interface for making new classifier objects. This function 110 /// allows for specification at run-time of which kernel to 111 /// instatiate (see 'Prototype' in Design Patterns). 112 /// 113 /// @Note Returns a dynamically allocated Kernel, which has 114 /// to be deleted by the caller to avoid memory leaks. 115 /// 116 virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0; 117 118 119 /// 120 /// An interface for making new classifier objects. This function 121 /// allows for specification at run-time of which kernel to 122 /// instatiate (see 'Prototype' in Design Patterns). 123 /// 124 /// @Note Returns a dynamically allocated Kernel, which has 125 /// to be deleted by the caller to avoid memory leaks. 126 /// 127 virtual const Kernel* make_kernel(const MatrixLookupWeighted&, 128 const bool own=false) const=0; 129 130 131 /// 107 132 /// Created Kernel is built from selected features in data. The 108 133 /// @a index corresponds to which rows in data to use for the … … 114 139 /// to be deleted by the caller to avoid memory leaks. 115 140 /// 141 /// @todo remove this function 116 142 virtual const Kernel* selected(const std::vector<size_t>& index) const=0; 117 143 -
trunk/c++_tools/classifier/KernelLookup.cc
r640 r658 3 3 #include <c++_tools/classifier/KernelLookup.h> 4 4 #include <c++_tools/classifier/DataLookup2D.h> 5 #include <c++_tools/classifier/MatrixLookup.h> 6 #include <c++_tools/classifier/MatrixLookupWeighted.h> 5 7 6 8 … … 105 107 106 108 const KernelLookup* 107 KernelLookup::training_data(const std::vector<size_t>& features,108 const std::vector<size_t>& train) const109 {110 const Kernel* kernel = kernel_->selected(features);111 return new KernelLookup(*kernel, train, train, true);112 }113 114 115 const KernelLookup*116 109 KernelLookup::validation_data(const std::vector<size_t>& train, 117 110 const std::vector<size_t>& validation) const … … 121 114 122 115 123 const KernelLookup*124 KernelLookup::validation_data(const std::vector<size_t>& features,125 const std::vector<size_t>& train,126 const std::vector<size_t>& validation) const127 {128 const Kernel* kernel = kernel_->selected(features);129 return new KernelLookup(*kernel, train, validation, true);130 }131 132 133 116 const KernelLookup* 134 117 KernelLookup::selected(const std::vector<size_t>& inputs) const … … 138 121 } 139 122 123 124 const KernelLookup* KernelLookup::test_kernel(const MatrixLookup& data) const 125 { 126 assert(data.rows()==kernel_->data().rows()); 127 if (!weighted()){ 128 utility::matrix* data_all = 129 new utility::matrix(data.rows(), rows()+data.columns()); 130 for (size_t i=0; i<data.rows(); ++i){ 131 // first columns are equal to data in kernel_ 132 for (size_t j=0; j<row_index_.size(); ++j) 133 (*data_all)(i,j) = kernel_->data()(i,row_index_[j]); 134 // last columns are equal to new data 135 for (size_t j=0;j<data.columns(); ++j) 136 (*data_all)(i,j+row_index_.size()) = data(i,j); 137 } 138 std::vector<size_t> column_index; 139 column_index.reserve(data.columns()); 140 for (size_t i=0;i<data.columns(); ++i) 141 column_index.push_back(i+row_index_.size()); 142 const Kernel* kernel = 143 kernel_->make_kernel(MatrixLookup(*data_all, true), true); 144 return new KernelLookup(*kernel, row_index_, column_index, true); 145 } 146 147 // kernel_ holds MatrixLookupWeighted, hence new Kernel also 148 // should hold a MatrixLookupweighted. 149 utility::matrix* data_all = 150 new utility::matrix(data.rows(), rows()+data.columns()); 151 utility::matrix* weight_all = 152 new utility::matrix(data.rows(), rows()+data.columns(), 1.0); 153 const MatrixLookupWeighted& kernel_data = 154 dynamic_cast<const MatrixLookupWeighted&>(kernel_->data()); 155 156 for (size_t i=0; i<data.rows(); ++i){ 157 // first columns are equal to data in kernel_ 158 for (size_t j=0; j<row_index_.size(); ++j){ 159 (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 160 (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]); 161 } 162 // last columns are equal to new data 163 for (size_t j=0;j<data.columns(); ++j){ 164 (*data_all)(i,j+row_index_.size()) = data(i,j); 165 } 166 } 167 std::vector<size_t> column_index; 168 column_index.reserve(data.columns()); 169 for (size_t i=0;i<data.columns(); ++i) 170 column_index.push_back(i+row_index_.size()); 171 const Kernel* kernel = 172 kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true)); 173 return new KernelLookup(*kernel, row_index_, column_index, true); 174 } 175 176 177 178 const KernelLookup* 179 KernelLookup::test_kernel(const MatrixLookupWeighted& data) const 180 { 181 utility::matrix* data_all = 182 new utility::matrix(data.rows(), rows()+data.columns()); 183 utility::matrix* weight_all = 184 new utility::matrix(data.rows(), rows()+data.columns(), 1.0); 185 186 if (weighted()){ 187 const MatrixLookupWeighted& kernel_data = 188 dynamic_cast<const MatrixLookupWeighted&>(kernel_->data()); 189 190 for (size_t i=0; i<data.rows(); ++i){ 191 // first columns are equal to data in kernel_ 192 for (size_t j=0; j<row_index_.size(); ++j){ 193 (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 194 (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]); 195 } 196 } 197 } 198 else { 199 200 dynamic_cast<const MatrixLookupWeighted&>(kernel_->data()); 201 202 for (size_t i=0; i<data.rows(); ++i){ 203 // first columns are equal to data in kernel_ 204 for (size_t j=0; j<row_index_.size(); ++j) 205 (*data_all)(i,j) = kernel_->data()(i,row_index_[j]); 206 } 207 } 208 209 // last columns are equal to new data 210 for (size_t i=0; i<data.rows(); ++i){ 211 for (size_t j=0;j<data.columns(); ++j){ 212 (*data_all)(i,j+row_index_.size()) = data.data(i,j); 213 (*weight_all)(i,j+row_index_.size()) = data.weight(i,j); 214 } 215 } 216 217 std::vector<size_t> column_index; 218 column_index.reserve(data.columns()); 219 for (size_t i=0;i<data.columns(); ++i) 220 column_index.push_back(i+row_index_.size()); 221 const Kernel* kernel = 222 kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true)); 223 return new KernelLookup(*kernel, row_index_, column_index, true); 224 } 225 140 226 }} // of namespace classifier and namespace theplu -
trunk/c++_tools/classifier/KernelLookup.h
r656 r658 139 139 140 140 /// 141 /// @return a sub-kernel of kernel calculated using data defined by142 /// @a features. Each row and each columns corresponds to a traing143 /// sample defined by @a train.144 ///145 /// @return pointer to dynamically allocated sub-Lookup of the KernelLookup146 ///147 /// @Note Returns a dynamically allocated DataLookup2D, which has148 /// to be deleted by the caller to avoid memory leaks.149 ///150 const KernelLookup* training_data(const std::vector<size_t>& features,151 const std::vector<size_t>& train) const;152 153 154 ///155 141 /// In returned kernel each row corresponds to a training sample 156 142 /// and each column corresponds to a validation sample. The … … 168 154 169 155 170 /// 171 /// In returned kernel each row corresponds to a training sample 172 /// and each column corresponds to a validation sample. The kernel 173 /// is based on the features defined by @a features. 174 /// 175 /// @Note Returns a dynamically allocated DataLookup2D, which has 176 /// to be deleted by the caller to avoid memory leaks. 177 /// 178 const KernelLookup* 179 validation_data(const std::vector<size_t>& features, 180 const std::vector<size_t>& train, 181 const std::vector<size_t>& validation) const; 156 /** 157 This function is useful when predicting on a independent data 158 set using a kernel-based classifier. In returned KernelLookup 159 column \f$ i \f$ corresponds to column \f$ i \f$ in @a 160 data. Row \f$ i \f$ in returned KernelLookup corresponds to 161 same sample as row \f$ i \f$ in @a this. In other words, this 162 function returns a KernelLookup containing the kernel elements 163 between the passed @a data and the internal underlying data @a 164 this was built from. 165 166 @Note Returns a dynamically allocated DataLookup2D, which has 167 to be deleted by the caller to avoid memory leaks. 168 */ 169 const KernelLookup* test_kernel(const MatrixLookup& data) const; 170 171 172 /** 173 This function is useful when predicting on a independent data 174 set using a kernel-based classifier. In returned KernelLookup 175 column \f$ i \f$ corresponds to column \f$ i \f$ in @a 176 data. Row \f$ i \f$ in returned KernelLookup corresponds to 177 same sample as row \f$ i \f$ in @a this. In other words, this 178 function returns a KernelLookup containing the kernel elements 179 between the passed @a data and the internal underlying data @a 180 this was built from. 181 182 @Note Returns a dynamically allocated DataLookup2D, which has 183 to be deleted by the caller to avoid memory leaks. 184 */ 185 const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const; 182 186 183 187 -
trunk/c++_tools/classifier/Kernel_MEV.cc
r628 r658 9 9 namespace classifier { 10 10 11 Kernel_MEV::Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf) 12 : Kernel(data,kf) 11 Kernel_MEV::Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf, 12 const bool own) 13 : Kernel(data,kf,own) 13 14 { 14 15 } … … 16 17 17 18 Kernel_MEV::Kernel_MEV(const MatrixLookupWeighted& data, 18 const KernelFunction& kf )19 : Kernel(data,kf )19 const KernelFunction& kf, const bool own) 20 : Kernel(data,kf,own) 20 21 { 21 22 } … … 26 27 : Kernel(kernel,index) 27 28 { 29 } 30 31 32 const Kernel_MEV* Kernel_MEV::make_kernel(const MatrixLookup& data, 33 const bool own) const 34 { 35 return new Kernel_MEV(data, *kf_, own); 36 } 37 38 39 const Kernel_MEV* Kernel_MEV::make_kernel(const MatrixLookupWeighted& data, 40 const bool own) const 41 { 42 return new Kernel_MEV(data, *kf_, own); 28 43 } 29 44 -
trunk/c++_tools/classifier/Kernel_MEV.h
r651 r658 34 34 /// sample. @note Can not handle NaNs. 35 35 /// 36 Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf); 36 Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf, 37 const bool own=false); 37 38 38 39 … … 42 43 /// sample. @note Can not handle NaNs. 43 44 /// 44 Kernel_MEV(const MatrixLookupWeighted& data, const KernelFunction& kf); 45 Kernel_MEV(const MatrixLookupWeighted& data, const KernelFunction& kf, 46 const bool own=false); 45 47 46 48 … … 52 54 53 55 54 ///55 /// Destructor56 ///57 inline virtual ~Kernel_MEV(void) {};58 59 56 /// 60 57 /// @return Element at position (\a row, \a column) of the Kernel … … 63 60 double operator()(const size_t row, const size_t column) const; 64 61 62 /// 63 /// An interface for making new classifier objects. This function 64 /// allows for specification at run-time of which kernel to 65 /// instatiate (see 'Prototype' in Design Patterns). 66 /// 67 /// @Note Returns a dynamically allocated Kernel, which has 68 /// to be deleted by the caller to avoid memory leaks. 69 /// 70 const Kernel_MEV* make_kernel(const MatrixLookup&, 71 const bool own=false) const; 72 73 74 /// 75 /// An interface for making new classifier objects. This function 76 /// allows for specification at run-time of which kernel to 77 /// instatiate (see 'Prototype' in Design Patterns). 78 /// 79 /// @Note Returns a dynamically allocated Kernel, which has 80 /// to be deleted by the caller to avoid memory leaks. 81 /// 82 const Kernel_MEV* make_kernel(const MatrixLookupWeighted&, 83 const bool own=false) const; 84 85 65 86 /// 66 87 /// @see Kernel_MEV(const Kernel_MEV&, const std::vector<size_t>&); -
trunk/c++_tools/classifier/Kernel_SEV.cc
r628 r658 14 14 15 15 16 Kernel_SEV::Kernel_SEV(const MatrixLookup& data, const KernelFunction& kf) 17 : Kernel(data,kf) 16 Kernel_SEV::Kernel_SEV(const MatrixLookup& data, const KernelFunction& kf, 17 const bool own) 18 : Kernel(data,kf, own) 18 19 { 19 20 build_kernel(); … … 22 23 23 24 Kernel_SEV::Kernel_SEV(const MatrixLookupWeighted& data, 24 const KernelFunction& kf )25 : Kernel(data,kf )25 const KernelFunction& kf, const bool own) 26 : Kernel(data,kf, own) 26 27 { 27 28 kernel_matrix_ = utility::matrix(data_->columns(),data_->columns()); … … 52 53 53 54 55 double Kernel_SEV::operator()(const size_t row,const size_t column) const 56 { 57 return kernel_matrix_(row,column); 58 } 59 60 61 const Kernel_SEV* Kernel_SEV::make_kernel(const MatrixLookup& data, 62 const bool own) const 63 { 64 return new Kernel_SEV(data, *kf_, own); 65 } 66 67 68 const Kernel_SEV* Kernel_SEV::make_kernel(const MatrixLookupWeighted& data, 69 const bool own) const 70 { 71 return new Kernel_SEV(data, *kf_, own); 72 } 73 74 54 75 const Kernel* Kernel_SEV::selected(const std::vector<size_t>& index) const 55 76 { -
trunk/c++_tools/classifier/Kernel_SEV.h
r651 r658 34 34 /// use KernelWeighted_SEV instead. 35 35 /// 36 Kernel_SEV(const MatrixLookup&, const KernelFunction& );36 Kernel_SEV(const MatrixLookup&, const KernelFunction&,const bool own=false); 37 37 38 38 /// … … 41 41 /// use KernelWeighted_SEV instead. 42 42 /// 43 Kernel_SEV(const MatrixLookupWeighted&, const KernelFunction&); 43 Kernel_SEV(const MatrixLookupWeighted&, const KernelFunction&, 44 const bool own=false); 44 45 45 46 /// … … 48 49 Kernel_SEV(const Kernel_SEV& kernel, const std::vector<size_t>& index); 49 50 51 52 /// 53 /// An interface for making new classifier objects. This function 54 /// allows for specification at run-time of which kernel to 55 /// instatiate (see 'Prototype' in Design Patterns). 56 /// 57 /// @Note Returns a dynamically allocated Kernel, which has 58 /// to be deleted by the caller to avoid memory leaks. 59 /// 60 const Kernel_SEV* make_kernel(const MatrixLookup&, 61 const bool own=false) const; 62 63 64 /// 65 /// An interface for making new classifier objects. This function 66 /// allows for specification at run-time of which kernel to 67 /// instatiate (see 'Prototype' in Design Patterns). 68 /// 69 /// @Note Returns a dynamically allocated Kernel, which has 70 /// to be deleted by the caller to avoid memory leaks. 71 /// 72 const Kernel_SEV* make_kernel(const MatrixLookupWeighted&, 73 const bool own=false) const; 74 75 50 76 /// 51 77 /// @return element at position (\a row, \a column) in the Kernel 52 78 /// matrix 53 79 /// 54 inline double operator()(const size_t row,const size_t column) const55 { return kernel_matrix_(row,column); } 80 double operator()(const size_t row,const size_t column) const; 81 56 82 57 83 /// -
trunk/c++_tools/classifier/MatrixLookup.cc
r656 r658 14 14 namespace classifier { 15 15 16 MatrixLookup::MatrixLookup(const utility::matrix& data )17 : DataLookup2D( ), data_(&data)16 MatrixLookup::MatrixLookup(const utility::matrix& data, const bool own) 17 : DataLookup2D(own), data_(&data) 18 18 { 19 19 for(size_t i=0;i<(*data_).rows();i++) -
trunk/c++_tools/classifier/MatrixLookup.h
r657 r658 30 30 /// 31 31 /// There is a possibility to set the MatrixLookup as owner of the 32 /// underlying matrix. In that case the underlying matrix will be 33 /// destroyed in the destructor. Consequently, the underlying matrix 34 /// must have been dynamically allocated and no other MatrixLookup 35 /// can own the Kernel. 32 /// underlying matrix. 36 33 /// 37 34 class MatrixLookup : public DataLookup2D … … 43 40 /// 44 41 /// Constructor creating a lookup into the entire @a matrix. 42 /// @param own if true MatrixLookup owns its underlying @a matrix 45 43 /// 46 44 /// @note If @a matrix goes out of scope or is deleted, the … … 48 46 /// undefined. 49 47 /// 50 explicit MatrixLookup(const utility::matrix& matrix);48 MatrixLookup(const utility::matrix& matrix, const bool own=false); 51 49 52 50 /// -
trunk/c++_tools/classifier/MatrixLookupWeighted.cc
r656 r658 15 15 16 16 MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data, 17 const utility::matrix& weights) 18 : DataLookup2D(), data_(&data), weights_(&weights), ref_count_weights_(NULL) 17 const utility::matrix& weights, 18 const bool own) 19 : DataLookup2D(own), data_(&data), weights_(&weights), 20 ref_count_weights_(NULL) 19 21 { 20 22 assert(data.rows()==weights.rows()); -
trunk/c++_tools/classifier/MatrixLookupWeighted.h
r656 r658 44 44 /// 45 45 MatrixLookupWeighted(const utility::matrix& matrix, 46 const utility::matrix& weights); 46 const utility::matrix& weights, 47 const bool owner=false); 47 48 48 49
Note: See TracChangeset
for help on using the changeset viewer.