Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // knn search.
- arma::Mat<size_t> neighbors;
- arma::mat distances;
- KNN knn(dataset);
- knn.Search(N - 1, neighbors, distances);
- // Initialize impostors & targetNeighbors.
- arma::mat impostors(N, N);
- impostors.fill(arma::datum::inf);
- arma::mat targetNeighors(k, N, arma::fill::zeros);
- size_t impCount = 0;
- // Generate k Target Neighbors and variable number of impostors that invade last element boundary
- // for each data points.
- for (size_t i = 0; i < N; i++)
- {
- for( size_t j=0, gen=0, imp = 0; j < N && gen < k; j++ )
- {
- if( labels.at( neighbors.col(i)[j] ) == labels[i] )
- {
- targetNeighors.col(i)[gen++] = neighbors.col(i)[j];
- }
- else
- {
- impostors.col(i)[imp++] = neighbors.col(i)[j];
- impCount++;
- }
- }
- }
- // Generate Triplets.
- triplets = arma::mat(3, k * impCount , arma::fill::zeros);
- for( size_t i=0, r=0; i < N; i++ )
- {
- for( size_t j=0;j < k; j++)
- {
- for( size_t l=0; arma::is_finite(impostors.col(i)[l]); l++, r++ )
- {
- triplets(0, r) = i;
- triplets(1, r) = targetNeighors.col(i)[j];
- triplets(2, r) = impostors.col(i)[l];
- }
- }
- }
Add Comment
Please, Sign In to add comment