Guest User

Untitled

a guest
May 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1.   // knn search.
  2.   arma::Mat<size_t> neighbors;
  3.   arma::mat distances;
  4.  
  5.   KNN knn(dataset);
  6.   knn.Search(N - 1, neighbors, distances);
  7.  
  8.   // Initialize impostors & targetNeighbors.
  9.   arma::mat impostors(N, N);
  10.   impostors.fill(arma::datum::inf);
  11.   arma::mat targetNeighors(k, N, arma::fill::zeros);
  12.  
  13.   size_t impCount = 0;
  14.  
  15.   // Generate k Target Neighbors and variable number of impostors that invade last element boundary
  16.   // for each data points.
  17.   for (size_t i = 0; i < N; i++)
  18.   {
  19.     for( size_t j=0, gen=0, imp = 0; j < N && gen < k; j++ )
  20.     {
  21.       if( labels.at( neighbors.col(i)[j] ) == labels[i] )
  22.       {
  23.         targetNeighors.col(i)[gen++] = neighbors.col(i)[j];
  24.       }
  25.       else
  26.       {
  27.         impostors.col(i)[imp++] = neighbors.col(i)[j];
  28.         impCount++;
  29.       }
  30.     }
  31.   }
  32.  
  33.   // Generate Triplets.
  34.   triplets = arma::mat(3, k * impCount , arma::fill::zeros);
  35.  
  36.   for( size_t i=0, r=0; i < N; i++ )
  37.   {
  38.     for( size_t j=0;j < k; j++)
  39.     {
  40.       for( size_t l=0; arma::is_finite(impostors.col(i)[l]); l++, r++ )
  41.       {
  42.           triplets(0, r) = i;
  43.           triplets(1, r) = targetNeighors.col(i)[j];
  44.           triplets(2, r) = impostors.col(i)[l];
  45.       }
  46.     }
  47.   }
Add Comment
Please, Sign In to add comment