Changeset 916
- Timestamp:
- Sep 30, 2007, 2:50:10 AM (16 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 24 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/configure.ac
r865 r916 67 67 if test "${enable_debug}" = "yes" ; then 68 68 CXXFLAGS="$CXXFLAGS -g -O" 69 CPPFLAGS="$CPPFLAGS -DYAT_DEBUG=1" 69 70 else 70 71 CXXFLAGS="$CXXFLAGS -O3" -
trunk/test/Makefile.am
r904 r916 32 32 distance_test \ 33 33 ensemble_test feature_selection_test fileutil_test inputranker_test \ 34 iterator_test kernel_test kernel_lookup_test \ 34 iterator_test kernel_test kernel_lookup_test \ 35 35 knn_test matrix_test matrix_lookup_test \ 36 36 nbc_test \ -
trunk/test/averager_test.cc
r887 r916 141 141 theplu::yat::utility::vector w(3,1); 142 142 theplu::yat::statistics::AveragerWeighted aw; 143 a w.add_values(x,w);143 add(aw, x.begin(), x.end(), w.begin()); 144 144 a.reset(); 145 a .add_values(x);145 add(a, x.begin(), x.end()); 146 146 const double tol=std::numeric_limits<double>().round_error(); 147 147 if (!equal(a,aw,tol,error)){ … … 166 166 aw2->reset(); 167 167 w*=17; 168 a w2->add_values(x,w);168 add(*aw2, x.begin(), x.end(), w.begin()); 169 169 if (!equal(aw,*aw2,tol,error)){ 170 170 *error << "error: AveragerWeighted rescaling weights " -
trunk/test/knn_test.cc
r903 r916 4 4 #include "yat/classifier/MatrixLookupWeighted.h" 5 5 #include "yat/statistics/euclidean_vector_distance.h" 6 #include "yat/statistics/pearson_vector_distance.h" 6 7 #include "yat/utility/matrix.h" 7 8 … … 42 43 utility::matrix weights(data.rows(),data.columns(),0.0); 43 44 utility::nan(data,weights); 44 45 45 46 classifier::MatrixLookupWeighted dataviewweighted(data,weights); 46 classifier::KNN<statistics::euclidean_vector_distance_tag> knn(dataviewweighted,targets); 47 classifier::KNN<statistics::pearson_vector_distance_tag> knn(dataviewweighted,targets); 48 *error << "Training KNN" << std::endl; 47 49 knn.train(); 48 50 -
trunk/test/ncc_test.cc
r901 r916 56 56 bool ok = true; 57 57 58 classifier::MatrixLookupWeighted ml(4,4); 59 std::vector<std::string> vec(4, "pos"); 60 vec[3]="bjds"; 61 classifier::Target target(vec); 62 statistics::vector_distance_lookup_weighted_ptr dist= 63 statistics::vector_distance<statistics::pearson_vector_distance_tag>; 64 classifier::NCC ncctmp(ml,target,dist); 65 *error << "training...\n"; 66 ncctmp.train(); 67 58 68 std::ifstream is("data/sorlie_centroid_data.txt"); 59 69 utility::matrix data(is,'\t'); … … 72 82 statistics::vector_distance<statistics::pearson_vector_distance_tag>; 73 83 classifier::NCC ncc(dataviewweighted,targets,distance); 84 *error << "training...\n"; 74 85 ncc.train(); 75 86 … … 103 114 } 104 115 116 *error << "prediction...\n"; 105 117 utility::matrix prediction; 106 118 ncc.predict(dataviewweighted,prediction); -
trunk/yat/classifier/DataLookup2D.cc
r865 r916 139 139 } 140 140 141 std::ostream& operator<<(std::ostream& s, const DataLookup2D& m) 142 { 143 s.setf(std::ios::dec); 144 s.precision(12); 145 for(size_t i=0, j=0; i<m.rows(); i++) 146 for (j=0; j<m.columns(); j++) { 147 s << m(i,j); 148 if (j<m.columns()-1) 149 s << s.fill(); 150 else if (i<m.rows()-1) 151 s << "\n"; 152 } 153 return s; 154 } 155 141 156 }}} // of namespace classifier, yat, and theplu -
trunk/yat/classifier/DataLookup2D.h
r865 r916 175 175 }; 176 176 177 /// 178 /// The output operator DataLookup2D 179 /// 180 std::ostream& operator<< (std::ostream& s, const DataLookup2D&); 181 177 182 }}} // of namespace classifier, yat, and theplu 178 183 -
trunk/yat/classifier/KNN.h
r904 r916 10 10 #include "yat/statistics/vector_distance.h" 11 11 #include "yat/utility/matrix.h" 12 12 #include "yat/utility/yat_assert.h" 13 14 #include <cmath> 13 15 #include <map> 14 16 … … 119 121 120 122 // matrix with training samples as rows and test samples as columns 121 utility::matrix* distances = new utility::matrix(data_.columns(),input.columns()); 123 utility::matrix* distances = 124 new utility::matrix(data_.columns(),input.columns()); 122 125 123 126 if(weighted_data && weighted_input) { … … 126 129 for(size_t j=0; j<input.columns(); j++) { 127 130 classifier::DataLookupWeighted1D test(*weighted_input,j,false); 131 yat_assert(training.size()==test.size()); 128 132 (*distances)(i,j)=statistics::vector_distance(training.begin(),training.end(),test.begin(),typename statistics::vector_distance_traits<Distance>::distance()); 133 yat_assert(!std::isnan((*distances)(i,j))); 129 134 } 130 135 } … … 186 191 utility::matrix* distances=calculate_distances(input); 187 192 188 // for each test sample (column in distances) find the closest training samples 189 prediction.clone(utility::matrix(target_.nof_classes(), input.columns(),0.0)); 193 // for each test sample (column in distances) find the closest 194 // training samples 195 prediction.clone(utility::matrix(target_.nof_classes(),input.columns(),0.0)); 190 196 for(size_t sample=0;sample<distances->columns();sample++) { 191 197 std::vector<size_t> k_index; -
trunk/yat/classifier/MatrixLookup.cc
r865 r916 217 217 218 218 219 std::ostream& operator<<(std::ostream& s, const MatrixLookup& m)220 {221 s.setf(std::ios::dec);222 s.precision(12);223 for(size_t i=0, j=0; i<m.rows(); i++)224 for (j=0; j<m.columns(); j++) {225 s << m(i,j);226 if (j<m.columns()-1)227 s << s.fill();228 else if (i<m.rows()-1)229 s << "\n";230 }231 return s;232 }233 234 219 235 220 -
trunk/yat/classifier/MatrixLookup.h
r865 r916 284 284 }; 285 285 286 ///287 /// The output operator MatrixLookup288 ///289 std::ostream& operator<< (std::ostream& s, const MatrixLookup&);290 291 286 }}} // of namespace classifier, yat, and theplu 292 287 -
trunk/yat/classifier/NCC.cc
r898 r916 130 130 for(size_t k=0; k<centroids_.columns();k++) { 131 131 DataLookupWeighted1D centroid(weighted_centroids,k,false); 132 133 assert(in.size()==centroid.size()); 132 134 prediction(k,j)=(*distance_)(in.begin(),in.end(),centroid.begin()); 133 135 } -
trunk/yat/regression/MultiDimensionalWeighted.cc
r865 r916 74 74 75 75 statistics::AveragerWeighted aw; 76 a w.add_values(y,w);76 add(aw, y.begin(), y.end(), w.begin()); 77 77 s2_ = chisquare_ / (aw.n()-fit_parameters_.size()); 78 78 covariance_ *= s2_; -
trunk/yat/statistics/Averager.cc
r865 r916 25 25 #include "Averager.h" 26 26 27 #include <cassert> 28 27 29 namespace theplu { 28 30 namespace yat { … … 46 48 void Averager::add(double d, u_long n) 47 49 { 50 assert(!std::isnan(d)); 48 51 n_ += n; 49 52 x_ += n*d; -
trunk/yat/statistics/Averager.h
r914 r916 67 67 /// 68 68 void add(double d, u_long n=1); 69 70 ///71 /// Adding each value in an array \a v \a n (default=1)72 /// number of times. The requirements for the type T of the73 /// array \a v are: operator[] returning an element and function74 /// size() returning the number of elements.75 ///76 ///77 template <typename T>78 void add_values(const T& v, u_long n=1);79 69 80 70 /** … … 205 195 } 206 196 207 // Template implementations208 template <typename T>209 void Averager::add_values(const T& v, u_long n)210 {211 for (size_t i=0; i<v.size(); i++)212 add(v[i],n);213 }214 215 197 }}} // of namespace statistics, yat, and theplu 216 198 -
trunk/yat/statistics/AveragerPairWeighted.cc
r899 r916 49 49 return; 50 50 } 51 assert(!std::isnan(x) && "x is nan"); 52 assert(!std::isnan(y) && "y is nan"); 53 assert(!std::isnan(wx) && "wx is nan"); 54 assert(!std::isnan(wy) && "wy is nan"); 51 55 double w=wx*wy; 52 56 x_.add(x,w); … … 84 88 double AveragerPairWeighted::correlation(void) const 85 89 { 86 return covariance() / ( x_.std()*y_.std() ); 90 return ( x_.variance()>0 && y_.variance()>0 ? 91 covariance() / sqrt(x_.variance()*y_.variance()) : 0 ); 87 92 } 88 93 -
trunk/yat/statistics/AveragerPairWeighted.h
r915 r916 30 30 31 31 #include "yat/utility/IteratorTraits.h" 32 #include "yat/utility/yat_assert.h" 32 33 33 34 #include <cmath> … … 183 184 void add(AveragerPairWeighted& ap, Iter1 first1, Iter1 last1, Iter2 first2) 184 185 { 185 for ( ; first1 != last1; ++first1, ++first2) 186 for ( ; first1 != last1; ++first1, ++first2) { 186 187 ap.add(utility::iterator_traits_data(first1), 187 188 utility::iterator_traits_data(first2), 188 189 utility::iterator_traits_weight(first1), 189 190 utility::iterator_traits_weight(first2)); 191 } 190 192 } 191 193 … … 197 199 const T4& wy) 198 200 { 199 for (size_t i=0; i<x.size(); i++) 201 for (size_t i=0; i<x.size(); ++i){ 202 yat_assert(!std::isnan(x[i])); 200 203 add(x[i],y[i],wx[i],wy[i]); 204 } 201 205 } 202 206 -
trunk/yat/statistics/AveragerWeighted.h
r915 r916 83 83 84 84 /// 85 /// Adding each value in an array \a x and corresponding value in86 /// weight array \a w.87 ///88 /// The requirements for the types T1 and T2 of the arrays \a x89 /// and \a w are: operator[] returning an element and function90 /// size() returning the number of elements.91 ///92 template <typename T1, typename T2>93 void add_values(const T1& x, const T2& w);94 95 ///96 85 /// @brief Calculate the weighted mean 97 86 /// … … 221 210 222 211 /** 223 \brief adding a range sof values to AveragerWeighted \a a212 \brief adding a range of values to AveragerWeighted \a a 224 213 */ 225 214 template <typename Iter> … … 230 219 } 231 220 232 // Template implementations 233 template <typename T1, typename T2> 234 void AveragerWeighted::add_values(const T1& x, const T2& w) 221 /** 222 \brief add values from two ranges to AveragerWeighted \a a 223 224 Add data from range [first1, last1) with their corresponding 225 weight in range [first2, first2 + distance(first, last) ). 226 227 Requirement: Iter1 and Iter2 are unweighted iterators. 228 */ 229 template <typename Iter1, typename Iter2> 230 void add(AveragerWeighted& a, Iter1 first1, Iter1 last1, Iter2 first2) 235 231 { 236 for (size_t i=0; i<x.size(); i++) 237 add(x[i],w[i]); 232 utility::check_iterator_is_unweighted(first1); 233 utility::check_iterator_is_unweighted(first2); 234 for ( ; first1 != last1; ++first1, ++first2) 235 a.add(*first1, *first2); 238 236 } 239 237 240 ///241 /// The AveragerWeighted output operator242 ///243 ///std::ostream& operator<<(std::ostream& s,const AveragerWeighted&);244 245 238 }}} // of namespace statistics, yat, and theplu 246 239 -
trunk/yat/statistics/vector_distance.h
r909 r916 71 71 return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance()); 72 72 } 73 74 73 75 74 }}} // of namespace statistics, yat, and theplu -
trunk/yat/utility/Iterator.h
r884 r916 24 24 02111-1307, USA. 25 25 */ 26 27 #include "yat_assert.h" 26 28 27 29 #include <iterator> … … 62 64 \return element 63 65 */ 64 return_type operator*(void) const { return container_->operator()(index_); } 66 return_type operator*(void) const 67 { yat_assert(index_<container_->size()); 68 return container_->operator()(index_); } 65 69 66 70 /** -
trunk/yat/utility/IteratorTraits.h
r914 r916 61 61 }; 62 62 63 64 65 /* 66 struct used to determine if a pair of iterators should be treated 67 as weighted 68 69 template <class T1, class T2> 70 struct weighted_iterator_traits2 { 71 typedef weighted_type type; 72 }; 73 74 // but specialized to return weighted type for some things 75 template <class T1, class T2> 76 struct weighted_iterator_traits2<yat::utility::IteratorWeighted<U,V> > { 77 typedef weighted_type type; 78 }; 79 */ 80 63 81 // check (at compile time) that iterator is unweighted. 64 82 inline void check_iterator_is_unweighted(unweighted_type x){} 65 83 template <class Iter> 66 84 void check_iterator_is_unweighted(Iter iter) 67 {check_iterator_unweighted(typename weighted_iterator_traits<Iter>::type());} 85 { check_iterator_is_unweighted(typename 86 weighted_iterator_traits<Iter>::type()); 87 } 68 88 69 89 … … 79 99 template <class Iter> 80 100 double iterator_traits_data(Iter iter) 81 { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); } 101 { return iterator_traits_data(iter, typename 102 weighted_iterator_traits<Iter>::type()); } 82 103 83 104 template <class Iter> … … 101 122 template <class Iter> 102 123 double iterator_traits_weight(Iter iter) 103 { return iterator_traits_data(iter, weighted_iterator_traits<Iter>::type()); } 124 { return iterator_traits_weight(iter, typename 125 weighted_iterator_traits<Iter>::type()); } 104 126 105 127 template <class Iter> -
trunk/yat/utility/IteratorWeighted.h
r909 r916 3 3 4 4 // $Id$ 5 6 #include "yat_assert.h" 5 7 6 8 #include <iterator> … … 41 43 \return element 42 44 */ 43 return_type operator*(void) const { return container_->operator()(index_); } 45 return_type operator*(void) const 46 { 47 yat_assert(index_<container_->size()); 48 return container_->operator()(index_); 49 } 44 50 45 51 /** 46 52 \return data 47 53 */ 48 return_type data(void) const { return container_->data(index_); } 54 return_type data(void) const 55 { yat_assert(index_<container_->size()); return container_->data(index_); } 49 56 50 57 /** 51 58 \return weight 52 59 */ 53 return_type weight(void) const { return container_->weight(index_); } 60 return_type weight(void) const 61 { yat_assert(index_<container_->size());return container_->weight(index_); } 54 62 55 63 -
trunk/yat/utility/Makefile.am
r908 r916 34 34 Alignment.h Exception.h FileUtil.h Iterator.h IteratorTraits.h \ 35 35 IteratorWeighted.h kNNI.h matrix.h NNI.h \ 36 PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h WeNNI.h 36 PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h WeNNI.h \ 37 yat_assert.h -
trunk/yat/utility/matrix.cc
r865 r916 406 406 { 407 407 assert(m_); 408 if (gsl_matrix_memcpy(m_, other.gsl_matrix_p())) 409 throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match"); 408 if (this!=&other) 409 if (gsl_matrix_memcpy(m_, other.gsl_matrix_p())) 410 throw utility::GSL_error("matrix::create_gsl_matrix_copy dimension mis-match"); 410 411 return *this; 411 412 } -
trunk/yat/utility/utility.h
r865 r916 64 64 bool is_nan(const std::string& s); 65 65 66 67 68 66 }}} // of namespace utility, yat, and theplu 69 67
Note: See TracChangeset
for help on using the changeset viewer.