int KNN_prune_noisy ( /////////////////////////////// // Parameters // /////////////////////////////// Pattern p, // source // Categories c, // source // long y, // source instance index // long k // k(!) // ) { if (y > p->ny) y = p->ny; // safety belt if (k > p->ny) k = p->ny; FeatureWeights fws = FeatureWeights_create(p->nx); if (fws) { long *indices = NUMlvector (0, p->ny - 1); // the coverage is not bounded by k but by n // long reachability = KNN_kNeighboursSkip(p, p, fws, y, k, indices, 0); .OS.081011 long reachability = KNN_kNeighboursSkip(p, p, fws, y, k, indices, y); long coverage = KNN_prune_kCoverage(p, c, y, k, indices); NUMlvector_free (indices, 0); forget(fws); if (!KNN_prune_superfluous(p, c, y, k, 0) && reachability > coverage) return(1); } return(0); }
long KNN_prune_kCoverage ( PatternList p, // source Categories c, // source long y, // source instance index long k, // k(!) long * indices // Out: kCoverage set ) { Melder_assert (y <= p->ny); Melder_assert (k > 0 && k <= p->ny); long cc = 0; autoFeatureWeights fws = FeatureWeights_create (p -> nx); autoNUMvector <long> tempindices (0L, p -> ny - 1); for (long yy = 1; yy <= p -> ny; yy ++) { if (y != yy && FeatureWeights_areFriends (c->at [y], c->at [yy])) { long n = KNN_kNeighboursSkip (p, p, fws.get(), yy, k, tempindices.peek(), y); while (n) { Melder_assert (n <= p -> ny); if (tempindices [-- n] == y) { indices [cc ++] = yy; break; } } } } return cc; }
int KNN_prune_superfluous ( /////////////////////////////// // Parameters // /////////////////////////////// Pattern p, // source // Categories c, // source // long y, // source instance index // long k, // k(!) // long skipper // Skipping instance skipper // ) { if (y > p->ny) y = p->ny; // safety belt if (k > p->ny) k = p->ny; FeatureWeights fws = FeatureWeights_create(p->nx); if (fws) { long *indices = NUMlvector (0, k - 1); long *freqindices = NUMlvector (0, k - 1); double *distances = NUMdvector (0, k - 1); double *freqs = NUMdvector (0, k - 1); // KNN_kNeighboursSkip(p, p, fws, y, k, indices, skipper); .OS.081011 -> if(!KNN_kNeighboursSkip(p, p, fws, y, k, indices, skipper)) return(0); // .OS.081011 <- long ncategories = KNN_kIndicesToFrequenciesAndDistances(c, k, indices, distances, freqs, freqindices); forget(fws); int result = FRIENDS(c->item[y], c->item[freqindices[KNN_max(freqs, ncategories)]]); NUMlvector_free (indices, 0); NUMlvector_free (freqindices, 0); NUMdvector_free (distances, 0); NUMdvector_free (freqs, 0); if (result) return 1; } return 0; }
long KNN_prune_kCoverage ( /////////////////////////////// // Parameters // /////////////////////////////// Pattern p, // source // Categories c, // source // long y, // source instance index // long k, // k(!) // long * indices // Out: kCoverage set // ) { Melder_assert(y <= p->ny); Melder_assert(k > 0 && k <= p->ny); long cc = 0; FeatureWeights fws = FeatureWeights_create(p->nx); if (fws) { long *tempindices = NUMlvector (0, p->ny - 1); for (long yy = 1; yy <= p->ny; yy++) { if (y != yy && FRIENDS(c->item[y], c->item[yy])) { // long n = KNN_kNeighboursSkip(p, p, fws, yy, k, tempindices, 0); .OS.081011 long n = KNN_kNeighboursSkip(p, p, fws, yy, k, tempindices, y); while (n) { Melder_assert (n <= p->ny); if (tempindices[--n] == y) { indices[cc++] = yy; break; } } } } NUMlvector_free (tempindices, 0); forget(fws); } return(cc); }
int KNN_prune_noisy ( PatternList p, // source Categories c, // source long y, // source instance index long k // k(!) ) { if (y > p -> ny) y = p -> ny; // safety belt if (k > p -> ny) k = p -> ny; autoFeatureWeights fws = FeatureWeights_create (p -> nx); autoNUMvector <long> indices (0L, p->ny - 1); // the coverage is not bounded by k but by n long reachability = KNN_kNeighboursSkip (p, p, fws.get(), y, k, indices.peek(), y); long coverage = KNN_prune_kCoverage (p, c, y, k, indices.peek()); if (! KNN_prune_superfluous (p, c, y, k, 0) && reachability > coverage) return 1; return 0; }
int KNN_prune_critical ( PatternList p, // source Categories c, // source long y, // source instance index long k // k(!) ) { if (y > p -> ny) y = p -> ny; // safety belt if (k > p -> ny) k = p -> ny; autoFeatureWeights fws = FeatureWeights_create (p -> nx); autoNUMvector <long> indices (0L, k - 1); long ncollected = KNN_kNeighboursSkip (p, p, fws.get(), y, k, indices.peek(), y); for (long ic = 0; ic < ncollected; ic ++) { if (! KNN_prune_superfluous (p, c, indices [ic], k, 0) || ! KNN_prune_superfluous (p, c, indices [ic], k, y)) { return 1; } } return 0; }
int KNN_prune_critical ( /////////////////////////////// // Parameters // /////////////////////////////// Pattern p, // source // Categories c, // source // long y, // source instance index // long k // k(!) // ) { if (y > p->ny) y = p->ny; // safety belt if (k > p->ny) k = p->ny; FeatureWeights fws = FeatureWeights_create(p->nx); if (fws) { long *indices = NUMlvector (0, k - 1); // long ncollected = KNN_kNeighboursSkip(p, p, fws, y, k, indices, 0); .OS.081011 long ncollected = KNN_kNeighboursSkip(p, p, fws, y, k, indices, y); for (long ic = 0; ic < ncollected; ic++) if (!KNN_prune_superfluous(p, c, indices[ic], k, 0) || !KNN_prune_superfluous(p, c, indices[ic], k, y)) { NUMlvector_free (indices, 0); forget(fws); return(1); } NUMlvector_free (indices, 0); } return(0); }
int KNN_prune_superfluous ( PatternList p, // source Categories c, // source long y, // source instance index long k, // k(!) long skipper // Skipping instance skipper ) { if (y > p -> ny) y = p -> ny; // safety belt if (k > p -> ny) k = p -> ny; autoFeatureWeights fws = FeatureWeights_create (p -> nx); autoNUMvector <long> indices (0L, k - 1); autoNUMvector <long> freqindices (0L, k - 1); autoNUMvector <double> distances (0L, k - 1); autoNUMvector <double> freqs (0L, k - 1); if (! KNN_kNeighboursSkip (p, p, fws.get(), y, k, indices.peek(), skipper)) return 0; long ncategories = KNN_kIndicesToFrequenciesAndDistances (c, k, indices.peek(), distances.peek(), freqs.peek(), freqindices.peek()); int result = FeatureWeights_areFriends (c->at [y], c->at [freqindices [KNN_max (freqs.peek(), ncategories)]]); if (result) return 1; return 0; }