Changeset 1142 for trunk/test


Ignore:
Timestamp:
Feb 25, 2008, 3:32:35 PM (14 years ago)
Author:
Markus Ringnér
Message:

Refs #335, fixed for NCC, working on KNN

Location:
trunk/test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/knn_test.cc

    r1121 r1142  
    124124  ////////////////////////////////////////////////////////////////
    125125  *error << "test of predictions using weighted training and test data\n";
     126  *error << "... uniform neighbor weighting" << std::endl;
    126127  weights1(0,1)=0;
    127128  utility::Matrix weights2(3,4,1.0);
     
    146147  // A test of reciprocal ranks weighting with training and test both weighted
    147148  ////////////////////////////////////////////////////////////////
     149  *error << "... reciprokal rank neighbor weighting" << std::endl;
    148150  utility::Matrix data2(data1);
    149151  data2(1,3)=7;
     
    166168  }
    167169
    168 
     170 
    169171  ////////////////////////////////////////////////////////////////
    170172  // A test of reciprocal distance weighting with training and test both weighted
    171173  ////////////////////////////////////////////////////////////////
     174  *error << "... reciprokal distance neighbor weighting" << std::endl;
    172175  classifier::KNN<statistics::EuclideanDistance,classifier::KNN_ReciprocalDistance>
    173176    knn4(mlw2,target1);
     
    175178  knn4.train();
    176179  knn4.predict(mlw3,prediction1);
    177   slack = deviation(prediction1,result1);
    178   if (!std::isinf(prediction1(0,0)) && !std::isinf(prediction1(0,1)) &&
    179       !std::isinf(prediction1(1,2)) &&
    180       fabs(prediction1(1,3)-(1.0/3.67423461417))>slack_bound &&
    181       fabs(prediction1(1,0)-(1.0/2.82842712475+1.0/2.44948974278))>slack_bound){
    182     *error << "Difference to expected prediction too large\n";
    183     ok = false;
    184   }
     180  if (!(std::isinf(prediction1(0,0)) && std::isinf(prediction1(0,1)) &&
     181        std::isinf(prediction1(1,2)) &&
     182        fabs(prediction1(1,3)-(1.0/3.67423461417))<slack_bound &&
     183        fabs(prediction1(1,0)-(1.0/2.82842712475+1.0/2.44948974278))<slack_bound)){
     184    *error << "Difference to expected prediction too large\n";
     185    ok = false;
     186  }
     187
     188
     189  ////////////////////////////////////////////////////////////////
     190  // A test of when a class has no training samples, should give nan
     191  // in predictions. Also tests that k is reduced if not enough
     192  // training samples.
     193  ////////////////////////////////////////////////////////////////
     194  //Keep only the second class in the training samples
     195  std::vector<size_t> ind(2,2);
     196  ind[1]=3;
     197  classifier::Target target2(target1,utility::Index(ind));
     198  classifier::MatrixLookupWeighted mlw4(data1,weights2,utility::Index(ind),false);
     199  classifier::KNN<statistics::EuclideanDistance> knn5(mlw4,target2);
     200  knn5.k(3);
     201  knn5.train();
     202  knn5.predict(mlw3,prediction1);
     203  if (!(std::isnan(prediction1(0,0)) && std::isnan(prediction1(0,1)) &&
     204        std::isnan(prediction1(0,2)) && std::isnan(prediction1(0,3)) &&
     205        fabs(prediction1(1,0)-2.0)<slack_bound &&
     206        fabs(prediction1(1,1)-2.0)<slack_bound &&
     207        fabs(prediction1(1,2)-2.0)<slack_bound &&
     208        fabs(prediction1(1,3)-2.0)<slack_bound)) {
     209    *error << "Difference to expected prediction too large\n";
     210    ok = false;
     211  }
     212
     213  ////////////////////////////////////////////////////////////////
     214  // A test of when a test sample has no variables with non-zero
     215  // weights in common with training samples: should give nan distance
     216  ////////////////////////////////////////////////////////////////
     217  *error << "test of predictions with nan distances\n";
     218  weights1.all(1);
     219  weights1(1,0)=weights1(1,1)=weights1(2,0)=weights1(2,1)=0.0;
     220  weights2.all(1);
     221  weights2(0,0)=0.0;
     222  classifier::KNN<statistics::EuclideanDistance> knn6(mlw1,target1);
     223  knn6.k(3);
     224  knn6.train();
     225  knn6.predict(mlw3,prediction1);
     226  // std::cout << "train:\n" << mlw1 << std::endl;
     227  // std::cout << "test:\n" << mlw3 << std::endl;
     228  // std::cout << "pred:\n" << prediction1 << std::endl;
     229
    185230
    186231  if(!ok) {
  • trunk/test/ncc_test.cc

    r1132 r1142  
    159159
    160160  //////////////////////////////////////////////////////////////////////////
     161  // A test of predictions when a centroid has nan for all variables that a
     162  // test sample has non-zero weights for.
     163  //////////////////////////////////////////////////////////////////////////
     164  *error << "test of predictions using nan centroids and weighted test data\n";
     165  *error << "... using EuclideanDistance" << std::endl;
     166  weights1(0,0)=weights1(2,0)=0;
     167  classifier::NCC<statistics::EuclideanDistance> ncc3(mlw2,target1);
     168  ncc3.train();
     169  ncc3.predict(mlw1,prediction1);
     170  if(!std::isnan(ncc3.centroids()(1,0))) {
     171    ok=false;
     172    *error << "Training failed: expected nan in centroid" << std::endl;
     173  }
     174  if(!(std::isnan(prediction1(0,0)) &&
     175       fabs(prediction1(1,0)-sqrt(3.0))<slack_bound &&
     176       fabs(prediction1(0,1)-sqrt(3.0))<slack_bound &&
     177       fabs(prediction1(1,1)-sqrt(15.0))<slack_bound &&
     178       fabs(prediction1(0,2)-sqrt(27.0))<slack_bound)) { 
     179    ok=false;
     180    *error << "Test failed: predictions incorrect" << std::endl;
     181  }
     182  *error << "... using PearsonDistance" << std::endl;;
     183  classifier::NCC<statistics::PearsonDistance> ncc4(mlw2,target1);
     184  ncc4.train();
     185  ncc4.predict(mlw1,prediction1);
     186  if(!std::isnan(ncc4.centroids()(1,0))) {
     187    ok=false;
     188    *error << "Training failed: expected nan in centroid" << std::endl;
     189  }
     190  if(!(std::isnan(prediction1(0,0)) &&
     191       std::isnan(prediction1(0,2)) &&
     192       std::isnan(prediction1(1,0)) &&
     193       fabs(prediction1(0,1))<slack_bound &&
     194       fabs(prediction1(1,2))<slack_bound &&
     195       fabs(prediction1(1,3))<slack_bound && 
     196       fabs(prediction1(0,3)-2.0)<slack_bound &&
     197       fabs(prediction1(1,1)-2.0)<slack_bound)) {
     198    ok=false;
     199    *error << "Test failed: predictions incorrect" << std::endl;
     200  }
     201
     202
     203  //////////////////////////////////////////////////////////////////////////
    161204  // A test of predictions using Sorlie data
    162205  //////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.