Changeset 898
- Timestamp:
- Sep 26, 2007, 3:44:19 PM (16 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/iterator_test.cc
r883 r898 24 24 #include "yat/classifier/DataLookup1D.h" 25 25 26 #include "yat/classifier/DataLookupWeighted1D.h" 27 #include "yat/classifier/MatrixLookupWeighted.h" 26 28 #include "yat/utility/Iterator.h" 29 #include "yat/utility/IteratorWeighted.h" 30 #include "yat/utility/matrix.h" 27 31 #include "yat/utility/vector.h" 28 32 … … 63 67 std::sort(begin, end); 64 68 69 // test std algorithm on IteratorWeighted 70 utility::matrix m(1,3,1); 71 m(0,1)=2.0; 72 utility::matrix w(1,3,1); 73 classifier::MatrixLookupWeighted mw(m,w); 74 classifier::DataLookupWeighted1D aw(mw,0,true); 75 size_t nof1=std::count(aw.begin(),aw.end(),1.0); 76 if(nof1!=2) { 77 *message << "std algoritm with IteratorWeighted failed" << std::endl; 78 ok=false; 79 } 80 65 81 if (!ok) 66 82 *message << "iterator test failed" << std::endl; -
trunk/test/ncc_test.cc
r865 r898 28 28 #include "yat/classifier/Target.h" 29 29 #include "yat/utility/matrix.h" 30 #include "yat/statistics/PearsonDistance.h" 30 #include "yat/statistics/vector_distance_ptr.h" 31 #include "yat/statistics/pearson_vector_distance.h" 31 32 #include "yat/utility/utility.h" 32 33 … … 71 72 72 73 classifier::MatrixLookupWeighted dataviewweighted(data,weights); 73 statistics::PearsonDistance pearson; 74 classifier::NCC ncc(dataviewweighted,targets,pearson); 74 statistics::vector_distance_lookup_weighted_ptr distance= 75 statistics::vector_distance<statistics::pearson_vector_distance_tag>; 76 classifier::NCC ncc(dataviewweighted,targets,distance); 75 77 ncc.train(); 76 78 … … 127 129 128 130 129 // Testing IGP130 classifier:: MatrixLookup dataview(data);131 *error << "testing igp" << std::endl;132 classifier::IGP igp(dataview,targets,pearson);133 *error << igp.score() << std::endl;134 135 131 if(ok) 136 132 *error << "OK" << std::endl; -
trunk/test/vector_distance_test.cc
r897 r898 5 5 #include "yat/statistics/euclidean_vector_distance.h" 6 6 #include "yat/statistics/pearson_vector_distance.h" 7 #include "yat/statistics/vector_distance_ptr.h" 7 8 #include "yat/utility/matrix.h" 8 9 #include "yat/utility/vector.h" … … 14 15 15 16 using namespace theplu::yat; 17 18 19 // Function to test pointers to distance specialized for DataLookup1D::iterator 20 double f(statistics::vector_distance_lookup_weighted_ptr distance) { 21 utility::matrix m(2,3,1); 22 m(0,1)=2; 23 m(1,0)=0; 24 m(1,1)=0; 25 utility::matrix w(2,3,1); 26 w(0,0)=0; 27 classifier::MatrixLookupWeighted mw(m,w); 28 classifier::DataLookupWeighted1D aw(mw,0,true); 29 classifier::DataLookupWeighted1D bw(mw,1,true); 30 31 double dist=(*distance)(aw.begin(),aw.end(),bw.begin()); 32 return dist; 33 } 16 34 17 35 int main(const int argc,const char* argv[]) … … 63 81 64 82 dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(), 65 83 statistics::euclidean_vector_distance_tag()); 66 84 67 85 if(fabs(dist-2)>tolerance) { … … 79 97 80 98 81 // Test with reference to a vector_distance_tag82 st atistics::euclidean_vector_distance_tag tag;83 s tatistics::vector_distance_tag& tagref=tag;84 dist=statistics::vector_distance(aw.begin(),aw.end(),bw.begin(),tagref);85 *error << "Dist: " << dist << std::endl;99 // Test with std::vectors 100 std::vector<double> sa(3,1); 101 sa[1] = 2; 102 std::vector<double> sb(3,0); 103 sb[2] = 1; 86 104 105 dist=statistics::vector_distance(sa.begin(),sa.end(),sb.begin(), 106 statistics::euclidean_vector_distance_tag()); if(fabs(dist-2.23607)>tolerance) { 107 *error << "Error in vector_distance for std::vector: " << std::endl; 108 ok=false; 109 } 110 111 112 // Test with pointer to a vector_distance 113 statistics::vector_distance_lookup_weighted_ptr test_ptr= 114 statistics::vector_distance<statistics::euclidean_vector_distance_tag>; 115 dist=(*test_ptr)(aw.begin(),aw.end(),bw.begin()); 116 if(fabs(dist-2)>tolerance) { 117 *error << "Error when using pointer to vector_distance" << std::endl; 118 ok=false; 119 } 120 121 if(!ok) { 122 *error << "vector_distance_test failed" << std::endl; 123 } 87 124 if (error!=&std::cerr) 88 125 delete error; 89 90 126 if (ok=true) 91 127 return 0; -
trunk/yat/classifier/NCC.cc
r874 r898 27 27 #include "DataLookup1D.h" 28 28 #include "DataLookup2D.h" 29 #include "DataLookupWeighted1D.h" 29 30 #include "MatrixLookup.h" 30 31 #include "MatrixLookupWeighted.h" 31 32 #include "Target.h" 33 #include "yat/statistics/vector_distance.h" 34 #include "yat/statistics/euclidean_vector_distance.h" 35 #include "yat/utility/Iterator.h" 36 #include "yat/utility/IteratorWeighted.h" 32 37 #include "yat/utility/matrix.h" 33 38 #include "yat/utility/vector.h" 34 #include "yat/statistics/Distance.h"35 39 #include "yat/utility/stl_utility.h" 36 40 … … 45 49 46 50 NCC::NCC(const MatrixLookup& data, const Target& target, 47 const statistics:: Distance&distance)51 const statistics::vector_distance_lookup_weighted_ptr distance) 48 52 : SupervisedClassifier(target), distance_(distance), data_(data) 49 53 { … … 51 55 52 56 NCC::NCC(const MatrixLookupWeighted& data, const Target& target, 53 const statistics:: Distance&distance)57 const statistics::vector_distance_lookup_weighted_ptr distance) 54 58 : SupervisedClassifier(target), distance_(distance), data_(data) 55 59 { … … 65 69 return centroids_; 66 70 } 71 67 72 68 69 73 const DataLookup2D& NCC::data(void) const 74 { 70 75 return data_; 71 72 76 } 77 73 78 SupervisedClassifier* 74 79 NCC::make_classifier(const DataLookup2D& data, const Target& target) const … … 109 114 } 110 115 111 112 void NCC::predict(const utility::vector& input, const utility::vector& weights,113 utility::vector& prediction) const114 {115 prediction.clone(utility::vector(centroids_.columns()));116 117 // take care of nan's in centroids118 for(size_t j=0; j<centroids_.columns(); j++) {119 const utility::vector centroid(utility::vector(centroids_,j,false));120 utility::vector wc(centroid.size(),0);121 for(size_t i=0; i<centroid.size(); i++) {122 if(!std::isnan(centroid(i)))123 wc(i)=1.0;124 }125 prediction(j)=distance_(input,centroid,weights,wc);126 }127 }128 129 130 116 void NCC::predict(const DataLookup2D& input, 131 117 utility::matrix& prediction) const 132 118 { 133 prediction.clone(utility::matrix(centroids_.columns(), input.columns())); 134 // weighted case 135 const MatrixLookupWeighted* data =136 dynamic_cast<const MatrixLookupWeighted*>(&input);137 if (data) {138 for(size_t j=0; j<input.columns();j++) {139 utility::vector in(input.rows(),0);140 for(size_t i=0; i<in.size();i++)141 in(i)=data->data(i,j);142 utility::vector weights(in.size(),0);143 for(size_t i=0; i<in.size();i++)144 weights(i)=data->weight(i,j);145 utility::vector out;146 predict(in,weights,out);147 prediction.column(j,out);119 prediction.clone(utility::matrix(centroids_.columns(), input.columns())); 120 121 // Weighted case 122 const MatrixLookupWeighted* testdata = 123 dynamic_cast<const MatrixLookupWeighted*>(&input); 124 if (testdata) { 125 utility::matrix centroid_weights; 126 utility::nan(centroids_,centroid_weights); 127 MatrixLookupWeighted weighted_centroids(centroids_,centroid_weights); 128 for(size_t j=0; j<input.columns();j++) { 129 DataLookupWeighted1D in(*testdata,j,false); 130 for(size_t k=0; k<centroids_.columns();k++) { 131 DataLookupWeighted1D centroid(weighted_centroids,k,false); 132 prediction(k,j)=(*distance_)(in.begin(),in.end(),centroid.begin()); 133 } 148 134 } 149 return;150 135 } 151 // non-weighted case 152 const MatrixLookup* x = dynamic_cast<const MatrixLookup*>(&input); 153 if (!x){ 136 else { 154 137 std::string str; 155 138 str = "Error in NCC::predict: DataLookup2D of unexpected class."; 156 139 throw std::runtime_error(str); 157 140 } 158 for(size_t j=0; j<input.columns();j++) {159 utility::vector in(input.rows(),0);160 for(size_t i=0; i<in.size();i++)161 in(i)=(*data)(i,j);162 utility::vector weights(in.size(),1.0);163 utility::vector out;164 predict(in,weights,out);165 prediction.column(j,out);166 }167 141 } 168 169 170 // additional operators 171 172 // std::ostream& operator<< (std::ostream& s, const NCC& ncc) { 173 // std::copy(ncc.classes().begin(), ncc.classes().end(), 174 // std::ostream_iterator<std::map<double, u_int>::value_type> 175 // (s, "\n")); 176 // s << "\n" << ncc.centroids() << "\n"; 177 // return s; 178 // } 179 142 180 143 }}} // of namespace classifier, yat, and theplu -
trunk/yat/classifier/NCC.h
r874 r898 28 28 29 29 #include "yat/utility/matrix.h" 30 #include "yat/statistics/vector_distance_ptr.h" 30 31 #include "SupervisedClassifier.h" 31 32 … … 37 38 namespace utlitity { 38 39 class vector; 39 }40 41 namespace statistics {42 class Distance;43 40 } 44 41 … … 61 58 /// 62 59 /// Constructor taking the training data, the target vector, and 63 /// the distance measure as input.60 /// the distance measure tag as input. 64 61 /// 65 NCC(const MatrixLookup&, const Target&, const statistics::Distance&); 62 NCC(const MatrixLookup&, const Target&, 63 const statistics::vector_distance_lookup_weighted_ptr); 66 64 67 65 /// 68 66 /// Constructor taking the training data with weights, the target 69 /// vector, the distance measure, and a weight matrix for the 70 /// training data as input. 67 /// vector, the distance measure tag. 71 68 /// 72 NCC(const MatrixLookupWeighted&, const Target&, const statistics::Distance&); 69 NCC(const MatrixLookupWeighted&, const Target&, 70 const statistics::vector_distance_lookup_weighted_ptr); 73 71 74 72 virtual ~NCC(); … … 97 95 /// 98 96 void predict(const DataLookup2D&, utility::matrix&) const; 99 100 97 98 101 99 private: 102 100 utility::matrix centroids_; 103 const statistics:: Distance&distance_;101 const statistics::vector_distance_lookup_weighted_ptr distance_; 104 102 105 103 // data_ has to be of type DataLookup2D to accomodate both 106 104 // MatrixLookup and MatrixLookupWeighted 107 105 const DataLookup2D& data_; 108 109 ///110 /// Calculate the distance to each centroid for a test sample111 ///112 void predict(const utility::vector&, const utility::vector&,113 utility::vector&) const;114 106 115 107 }; -
trunk/yat/statistics/euclidean_vector_distance.h
r897 r898 50 50 double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2, 51 51 const euclidean_vector_distance_tag& disttype, 52 std::weighted_random_access_iterator_tag)52 utility::weighted_random_access_iterator_tag) 53 53 { 54 54 AveragerPairWeighted ap; -
trunk/yat/statistics/pearson_vector_distance.h
r897 r898 7 7 8 8 #include "AveragerPair.h" 9 #include "AveragerPairWeighted.h" 9 10 10 11 namespace theplu { … … 47 48 double vector_distance(Iter beg1,Iter end1, Iter beg2, 48 49 const pearson_vector_distance_tag& disttype, 49 std::weighted_random_access_iterator_tag)50 utility::weighted_random_access_iterator_tag) 50 51 { 51 52 AveragerPairWeighted ap; -
trunk/yat/statistics/vector_distance.h
r897 r898 6 6 #include <iterator> 7 7 8 #include "yat/classifier/DataLookup1D.h" 9 #include "yat/classifier/DataLookupWeighted1D.h" 10 8 11 namespace theplu { 9 12 namespace yat { 10 13 11 14 namespace statistics { 12 15 13 16 /// 14 17 /// For each measure to calculate distances between containers … … 52 55 typename std::iterator_traits<Iter1>::iterator_category()); 53 56 } 57 58 /// A partial specialization for DataLookupWeighted1D 59 template<typename Dist> 60 double vector_distance(const classifier::DataLookupWeighted1D::const_iterator& beg1,const classifier::DataLookupWeighted1D::const_iterator& end1,const classifier::DataLookupWeighted1D::const_iterator& beg2) 61 { 62 return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance()); 63 } 54 64 55 // Temporary function of where I end up when calling with a reference to distance_tag 56 template <typename Iter1, typename Iter2> 57 double vector_distance(Iter1 beg1, Iter1 end1, Iter2 beg2, 58 const vector_distance_tag& disttag) 65 /// A partial specialization for DataLookup1D 66 template<typename Dist> 67 double vector_distance(const classifier::DataLookup1D::const_iterator& beg1,const classifier::DataLookup1D::const_iterator& end1,const classifier::DataLookup1D::const_iterator& beg2) 59 68 { 60 return 0;61 } 69 return vector_distance(beg1,end1,beg2, typename statistics::vector_distance_traits<Dist>::distance()); 70 } 62 71 63 72 -
trunk/yat/utility/IteratorWeighted.h
r897 r898 7 7 #include <stddef.h> 8 8 9 namespace std {10 struct weighted_random_access_iterator_tag11 : public random_access_iterator_tag {12 };13 }9 // namespace std { 10 // struct weighted_random_access_iterator_tag 11 // : public random_access_iterator_tag { 12 // }; 13 // } 14 14 15 15 … … 17 17 namespace yat { 18 18 namespace utility { 19 20 struct weighted_random_access_iterator_tag 21 : public std::random_access_iterator_tag { 22 }; 19 23 20 24 /** … … 25 29 { 26 30 public: 27 typedef std::weighted_random_access_iterator_tag iterator_category;31 typedef weighted_random_access_iterator_tag iterator_category; 28 32 typedef double value_type; 29 33 typedef size_t difference_type;
Note: See TracChangeset
for help on using the changeset viewer.