Changeset 1587


Ignore:
Timestamp:
Oct 17, 2008, 5:31:42 PM (15 years ago)
Author:
Peter
Message:

closes #396

Location:
trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/NEWS

    r1488 r1587  
    55Version 0.5 (released DATE)
    66
     7  - MatrixLookupWeighted(const MatrixLookup&) modified (ticket:396)
    78  - Upgraded to GLPv3
    89  - Option::parse and OptionArg::do_parse takes references (ticket:418)
  • trunk/test/Makefile.am

    r1542 r1587  
    3737  index_test inputranker_test \
    3838  iterator_test kernel_lookup_test kernel_test \
    39   knn_test large_file_test matrix_lookup_test matrix_test \
     39  knn_test large_file_test matrix_lookup_test \
     40  matrix_lookup_weighted_test matrix_test \
    4041  matrix_weighted_test nbc_test \
    4142  ncc_test nni_test normalization_test pca_test \
     
    7576knn_test_SOURCES = knn_test.cc 
    7677large_file_test_SOURCES = large_file_test.cc
     78matrix_lookup_test_SOURCES = matrix_lookup_test.cc
     79matrix_lookup_weighted_test_SOURCES = matrix_lookup_weighted_test.cc
    7780matrix_test_SOURCES = matrix_test.cc
    7881matrix_weighted_test_SOURCES = matrix_weighted_test.cc
    79 matrix_lookup_test_SOURCES = matrix_lookup_test.cc
    8082nbc_test_SOURCES = nbc_test.cc
    8183ncc_test_SOURCES = ncc_test.cc
  • trunk/test/ncc_test.cc

    r1487 r1587  
    2828#include "yat/classifier/NCC.h"
    2929#include "yat/classifier/Target.h"
     30#include "yat/utility/DataIterator.h"
     31#include "yat/utility/DataWeight.h"
    3032#include "yat/utility/Matrix.h"
     33#include "yat/utility/MatrixWeighted.h"
    3134#include "yat/statistics/EuclideanDistance.h"
    3235#include "yat/statistics/PearsonDistance.h"
     
    100103  //////////////////////////////////////////////////////////////////////////
    101104  suite.err() << "test of predictions using unweighted training and weighted test data\n";
    102   utility::Matrix weights1(3,4,1.0);
    103   weights1(0,0)=weights1(1,1)=weights1(2,2)=weights1(1,3)=0.0;
    104   classifier::MatrixLookupWeighted mlw1(data1,weights1);
     105  utility::MatrixWeighted xw11(3,4);
     106  xw11(0,0)=xw11(1,1)=xw11(2,2)=xw11(1,3)=utility::DataWeight(0,0);
     107  std::copy(data1.begin(), data1.end(), utility::data_iterator(xw11.begin()));
     108  classifier::MatrixLookupWeighted mlw1(xw11);
     109  //classifier::MatrixLookupWeighted mlw1(data1,weights1);
    105110  ncc1.predict(mlw1,prediction1);
    106111  result1(0,2)=result1(0,3)=result1(1,0)=result1(1,1)=sqrt(15.0);
     
    116121  //////////////////////////////////////////////////////////////////////////
    117122  suite.err() << "test of predictions using nan centroids and unweighted test data\n";
    118   utility::Matrix weights2(3,4,1.0);
    119   weights2(1,0)=weights2(1,1)=0.0;
    120   classifier::MatrixLookupWeighted mlw2(data1,weights2);
     123  utility::MatrixWeighted xw12(3,4);
     124  xw12(1,0)=xw12(1,1)=utility::DataWeight(0,0);
     125  std::copy(data1.begin(), data1.end(), utility::data_iterator(xw12.begin()));
     126  classifier::MatrixLookupWeighted mlw2(xw12);
     127  //classifier::MatrixLookupWeighted mlw2(data1,weights2);
    121128  classifier::NCC<statistics::EuclideanDistance> ncc2;
    122129  ncc2.train(mlw2,target1);
     
    139146  suite.err() << "test of predictions using nan centroids and weighted test data\n";
    140147  suite.err() << "... using EuclideanDistance" << std::endl;
    141   weights1(0,0)=weights1(2,0)=0;
     148  xw11(0,0).weight() = xw11(2,0).weight()=0;
    142149  classifier::NCC<statistics::EuclideanDistance> ncc3;
    143150  ncc3.train(mlw2,target1);
     
    153160       suite.equal(prediction1(0,2),sqrt(27.0)) )) { 
    154161    suite.add(false);
     162    if (!std::isnan(prediction1(0,0)))
     163      suite.err() << "prediction1(0,0): " << prediction1(0,0) << "  "
     164                  << "expected NaN\n";
    155165    suite.err() << "Test failed: predictions incorrect" << std::endl;
    156166  }
     
    183193  ind[1]=3;
    184194  classifier::Target target2(target1,utility::Index(ind));
    185   classifier::MatrixLookupWeighted mlw3(data1,weights2,
     195  classifier::MatrixLookupWeighted mlw3(xw12,
    186196                                        utility::Index(data1.rows()),
    187197                                        utility::Index(ind));
     
    204214  suite.err() << "test with Sorlie data\n";
    205215  std::ifstream is(test::filename("data/sorlie_centroid_data.txt").c_str());
    206   utility::Matrix data(is,'\t');
     216  utility::MatrixWeighted data_weight(is,'\t');
    207217  is.close();
    208218
     
    211221  is.close();
    212222
    213   // Generate weight matrix with 0 for missing values and 1 for others.
    214   utility::Matrix weights(data.rows(),data.columns(),0.0);
    215   utility::nan(data,weights);
    216      
    217   classifier::MatrixLookupWeighted dataviewweighted(data,weights);
     223  classifier::MatrixLookupWeighted dataviewweighted(data_weight);
    218224  classifier::NCC<statistics::PearsonDistance> ncc;
    219225  suite.err() << "training...\n";
     
    272278    data1(i,3)=i+3;
    273279  }
     280  utility::MatrixWeighted xw(data1);
    274281  std::vector<std::string> vec1(4, "pos");
    275282  vec1[0]="neg";
     
    281288  utility::Matrix result1(2,4);
    282289
    283   utility::Matrix weights2(3,4,1.0);
    284   weights2(1,0)=weights2(1,1)=0.0;
    285 
    286   classifier::MatrixLookupWeighted mlw2(data1,weights2);
     290  xw(1,0).weight()=xw(1,1).weight()=0.0;
     291 
     292
     293  classifier::MatrixLookupWeighted mlw2(xw);
    287294  classifier::NCC<statistics::EuclideanDistance> ncc2;
    288295  ncc2.train(mlw2,target1);
  • trunk/yat/classifier/MatrixLookupWeighted.cc

    r1581 r1587  
    4141                                             const utility::Index& rows,
    4242                                             const utility::Index& columns)
    43     : column_index_(columns), row_index_(rows)
    44   {
    45     utility::Matrix* data = new utility::Matrix(m.rows(), m.columns());
    46     utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns());
    47     for (size_t i=0; i<m.rows(); ++i)
    48       for (size_t j=0; j<m.columns(); ++j) {
    49         (*data)(i,j) = m(i,j).data();
    50         (*weight)(i,j) = m(i,j).weight();
    51       }
    52     // smart pointers are taking ownership
    53     data_ = MatrixP(data);
    54     weights_ = MatrixP(weight);
     43    : column_index_(columns), data_(MatrixWP(&m,false)), row_index_(rows)
     44  {
     45    assert(validate());
    5546  }
    5647
     
    5950                                             bool owner)
    6051    : column_index_(utility::Index(m.columns())),
     52      data_(MatrixWP(&m,owner)),
    6153      row_index_(utility::Index(m.rows()))
    6254  {
    63     // Peter, remember to take care of ownership (but for now leave a leak)
    64 
    65     utility::Matrix* data = new utility::Matrix(m.rows(), m.columns());
    66     utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns());
    67     for (size_t i=0; i<m.rows(); ++i)
    68       for (size_t j=0; j<m.columns(); ++j) {
    69         (*data)(i,j) = m(i,j).data();
    70         (*weight)(i,j) = m(i,j).weight();
    71       }
    72   // smart pointers are taking ownership
    73     data_ = MatrixP(data);
    74     weights_ = MatrixP(weight);
    75   }
    76 
    77 
    78   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    79                                              const utility::Matrix& weights,
    80                                              const bool own)
    81     : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own))
    82   {
    83     assert(data.rows()==weights.rows());
    84     assert(data.columns()==weights.columns());
    85     row_index_ = utility::Index(data.rows());
    86     column_index_ = utility::Index(data.columns());
    8755    assert(validate());
    8856  }
     
    9058
    9159  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml)
    92     : column_index_(ml.column_index_), data_(ml.data_),
    93       row_index_(ml.row_index_),
    94       weights_(MatrixP(new utility::Matrix(data_->rows(),data_->columns(),1.0)))
     60    : column_index_(ml.column_index_),
     61      row_index_(ml.row_index_)
    9562  {
    96     assert(validate());
    97   }
    98  
    99 
    100   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    101                                              const utility::Matrix& weights,
    102                                              const utility::Index& row,
    103                                              const utility::Index& col)
    104     : column_index_(col), data_(MatrixP(&data, false)),
    105       row_index_(row), weights_(MatrixP(&weights, false))
    106   {
    107     assert(validate());
    108   }
    109  
    110 
    111 
    112   /*
    113   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    114                                              const utility::Matrix& weights,
    115                                              const utility::Index& index,
    116                                              const bool row)
    117     : data_(MatrixP(new utility::Matrix(data), false)),
    118       weights_(MatrixP(new utility::Matrix(weights), false))
    119   {
    120     assert(data.rows()==weights.rows());
    121     assert(data.columns()==weights.columns());
    122     if (row){
    123       row_index_=index;
    124       column_index_ = utility::Index(data.columns());
    125     }
    126     else{
    127       column_index_=index;
    128       row_index_ = utility::Index(data.rows());
    129     }
    130     assert(validate());
    131   }
    132   */
    133 
    134   /*
    135   MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv,
    136                                              const MatrixLookup& wv)
    137     : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_)
    138   {
    139   }
    140   */
    141 
     63    utility::MatrixWeighted* mw = new utility::MatrixWeighted(*ml.data_);
     64    data_ = MatrixWP(mw);
     65    assert(validate());
     66  }
     67 
    14268
    14369  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other)
    14470    : column_index_(other.column_index_), data_(other.data_),
    145       row_index_(other.row_index_), weights_(other.weights_)
     71      row_index_(other.row_index_)
    14672  {
    14773    assert(validate());
     
    15480                                             const utility::Index& col)
    15581    : column_index_(utility::Index(other.column_index_, col)),
    156       data_(other.data_), row_index_(utility::Index(other.row_index_, row)),
    157       weights_(other.weights_)
     82      data_(other.data_), row_index_(utility::Index(other.row_index_, row))
    15883  {
    15984    assert(validate());
     
    16590                                             const utility::Index& index,
    16691                                             bool row)
    167     : data_(other.data_),
    168       weights_(other.weights_)
     92    : data_(other.data_)
    16993  {
    17094    if (row){
     
    186110                                             const double weight)
    187111    : column_index_(utility::Index(std::vector<size_t>(rows, 0))),
    188       data_(MatrixP(new utility::Matrix(1,1,value))),
    189       row_index_(utility::Index(std::vector<size_t>(columns, 0))),
    190       weights_(MatrixP(new utility::Matrix(1,1,weight)))
     112      data_(MatrixWP(new utility::MatrixWeighted(1,1,value, weight))),
     113      row_index_(utility::Index(std::vector<size_t>(columns, 0)))
    191114  {
    192115    assert(validate());
     
    195118 
    196119  MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep)
    197     : data_(MatrixP(new utility::Matrix(is,sep)))
     120    : data_(MatrixWP(new utility::MatrixWeighted(is,sep)))
    198121  {
    199122    column_index_ = utility::Index(data_->columns());
    200123    row_index_ = utility::Index(data_->rows());
    201     utility::Matrix weights;
    202     utility::nan(*data_,weights);
    203     // Peter, should be possible to avoid this copying
    204     weights_= MatrixP(new utility::Matrix(weights));
    205124    assert(validate());
    206125  }
     
    247166    assert(row_index_[row]<data_->rows());
    248167    assert(column_index_[column]<data_->columns());
    249     return (*data_)(row_index_[row], column_index_[column]);
     168    return (*this)(row, column).data();
    250169  }
    251170
     
    290209      if (column_index_[i]>=data_->columns())
    291210        return false;
    292     for (size_t i=0; i<row_index_.size(); ++i)
    293       if (row_index_[i]>=weights_->rows())
    294         return false;
    295     for (size_t i=0; i<column_index_.size(); ++i)
    296       if (column_index_[i]>=weights_->columns())
    297         return false;
    298211    return true;
    299212  }
     
    304217    assert(row<rows());
    305218    assert(column<columns());
    306     assert(row_index_[row]<weights_->rows());
    307     assert(column_index_[column]<weights_->columns());
    308     return (*weights_)(row_index_[row], column_index_[column]);
     219    return (*this)(row, column).weight();
    309220  }
    310221
     
    321232  MatrixLookupWeighted::operator()(const size_t row, const size_t column) const
    322233  {
    323     return utility::DataWeight(data(row, column), weight(row,column));
     234    return (*data_)(row_index_[row], column_index_[column]);
    324235  }
    325236
     
    333244      row_index_=other.row_index_;
    334245      data_ = other.data_;
    335       weights_ = other.weights_;
    336246    }
    337247    assert(validate());
  • trunk/yat/classifier/MatrixLookupWeighted.h

    r1581 r1587  
    7171  class MatrixLookupWeighted
    7272  {
    73  
    7473  public:
    7574    /**
     
    119118                                  bool owner=false);
    120119
    121     ///
    122     /// Constructor creating a lookup into the entire \a matrix and \a
    123     /// weights.
    124     ///
    125     /// @note If @a matrix or @a weights goes out of scope or is
    126     /// deleted, the MatrixLookupWeighted becomes invalid and the
    127     /// result of further use is undefined.
    128     ///
    129     /// \deprecated
    130     MatrixLookupWeighted(const utility::Matrix& matrix,
    131                          const utility::Matrix& weights,
    132                          const bool owner=false);// YAT_DEPRECATE;
    133    
    134120    /**
    135121       Constructor creating a MatrixLookupWeighted from a MatrixLookup. A
     
    138124       \note no check for nan is performed.
    139125       
    140        @note If underlying utility::Matrix goes out of scope or
    141        is deleted, the MatrixLookupWeighted becomes invalid and the
    142        result of further use is undefined.
     126       @note from yat 0.5 data is copied and further modifications in
     127       \a matrix will not be reflected in MatrixLookupWeighted.
    143128    */
    144129    explicit MatrixLookupWeighted(const MatrixLookup& matrix);
    145130
    146 
    147     ///
    148     /// Constructor creating a lookup into a sub-matrix of @a matrix.
    149     /// The @a row and @a column define what sub-matrix to look into,
    150     /// in other words, the created MatrixLookupWeighted will fullfill
    151     /// the following:
    152     /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])
    153     /// weights(row[i],column[j]). This also means that number of
    154     /// rows in created MatrixLookupWeighted is equal to size of
    155     /// @a row, and number of columns is equal to size of @a column.
    156     ///
    157     /// @note If @a matrix or @a weights goes out of scope or is deleted, the
    158     /// MatrixLookupWeighted becomes invalid and the result of further use is
    159     /// undefined.
    160     ///
    161     /// \deprecated
    162     MatrixLookupWeighted(const utility::Matrix& matrix,
    163                          const utility::Matrix& weights,
    164                          const utility::Index& row,
    165                          const utility::Index& column);// YAT_DEPRECATE;
    166 
    167     ///
    168     /// Constructor creating a lookup into a sub-matrix of @a matrix.
    169     ///
    170     /// If @a row_vectors is true the new MatrixLookupWeighted will be
    171     /// consist of the row vectors defined by @a index. This means
    172     /// that the created MatrixLookupWeighted will fullfill:
    173     /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])
    174     ///
    175     ///
    176     /// If @a row_vectors is false the new MatrixLookupWeighted will be consist
    177     /// of the rolumn vectors defined by @a index. This means that the
    178     /// created MatrixLookupWeighted will fullfill:
    179     ///
    180     ///
    181     /// @note If @a matrix or @a weights goes out of scope or is
    182     /// deleted, the MatrixLookupWeighted becomes invalid and the
    183     /// result of further use is undefined.
    184     ///
    185     /*
    186     MatrixLookupWeighted(const utility::Matrix& matrix,
    187                          const utility::Matrix& weights,
    188                          const utility::Index& index,
    189                          const bool row_vectors);
    190     */
    191131
    192132    ///
     
    266206    /// matrix.
    267207    ///
    268     /// @see matrix(istream&) for details.
     208    /// @see utility::MatrixWeighted(istream&) for details.
    269209    ///
    270210    MatrixLookupWeighted(std::istream&, char sep='\0');
     
    356296   
    357297  private:
    358     typedef utility::SmartPtr<const utility::Matrix> MatrixP;
     298    typedef utility::SmartPtr<const utility::MatrixWeighted> MatrixWP;
    359299    utility::Index column_index_;
    360     MatrixP data_;
     300    MatrixWP data_;
    361301    utility::Index row_index_;
    362     MatrixP weights_;
    363302
    364303    // for assertions
Note: See TracChangeset for help on using the changeset viewer.