Search in sources :

Example 1 with Neighbor

use of smile.neighbor.Neighbor in project smile by haifengl.

the class RNNSearchDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    logNSlider.setEnabled(false);
    dimensionSlider.setEnabled(false);
    radiusField.setEnabled(false);
    logN = logNSlider.getValue();
    dimension = dimensionSlider.getValue();
    System.out.println("Generating dataset...");
    int n = (int) Math.pow(10, logN);
    double[][] data = new double[n][];
    for (int i = 0; i < n; i++) {
        data[i] = new double[dimension];
        for (int j = 0; j < dimension; j++) {
            data[i][j] = Math.random();
        }
    }
    int[] perm = Math.permutate(n);
    System.out.println("Building searching data structure...");
    long time = System.currentTimeMillis();
    LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
    int naiveBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    KDTree<double[]> kdtree = new KDTree<>(data, data);
    int kdtreeBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    CoverTree<double[]> cover = new CoverTree<>(data, new EuclideanDistance());
    int coverBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    LSH<double[]> lsh = new LSH<>(dimension, 5, (int) Math.log2(dimension), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        lsh.put(data[i], data[i]);
    }
    int lshBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    MPLSH<double[]> mplsh = new MPLSH<>(dimension, 3, (int) Math.log2(n), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        mplsh.put(data[i], data[i]);
    }
    double[][] train = new double[1000][];
    for (int i = 0; i < train.length; i++) {
        train[i] = data[perm[i]];
    }
    mplsh.learn(kdtree, train, radius);
    int mplshBuild = (int) (System.currentTimeMillis() - time);
    System.out.println("Perform 1000 searches...");
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        naive.range(data[perm[i]], radius, neighbors);
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        kdtree.range(data[perm[i]], radius, neighbors);
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        cover.range(data[perm[i]], radius, neighbors);
    }
    int coverSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        lsh.range(data[perm[i]], radius, neighbors);
    }
    int lshSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        ArrayList<Neighbor<double[], double[]>> neighbors = new ArrayList<>();
        mplsh.range(data[perm[i]], radius, neighbors, 0.95, 10);
    }
    int mplshSearch = (int) (System.currentTimeMillis() - time);
    canvas.removeAll();
    double[] buildTime = { naiveBuild, kdtreeBuild, coverBuild, lshBuild, mplshBuild };
    PlotCanvas build = BarPlot.plot(buildTime, label);
    build.setTitle("Build Time");
    canvas.add(build);
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch, lshSearch, mplshSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time");
    canvas.add(search);
    validate();
    startButton.setEnabled(true);
    logNSlider.setEnabled(true);
    dimensionSlider.setEnabled(true);
    radiusField.setEnabled(false);
}
Also used : MPLSH(smile.neighbor.MPLSH) LSH(smile.neighbor.LSH) CoverTree(smile.neighbor.CoverTree) ArrayList(java.util.ArrayList) Neighbor(smile.neighbor.Neighbor) EuclideanDistance(smile.math.distance.EuclideanDistance) KDTree(smile.neighbor.KDTree) MPLSH(smile.neighbor.MPLSH) LinearSearch(smile.neighbor.LinearSearch) PlotCanvas(smile.plot.PlotCanvas)

Example 2 with Neighbor

use of smile.neighbor.Neighbor in project smile by haifengl.

the class ApproximateStringSearchDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    knnField.setEnabled(false);
    if (data == null) {
        System.out.print("Loading dataset...");
        List<String> words = new ArrayList<>();
        try {
            FileInputStream stream = new FileInputStream(smile.data.parser.IOUtils.getTestDataFile("index.noun"));
            BufferedReader input = new BufferedReader(new InputStreamReader(stream));
            String line = input.readLine();
            while (line != null) {
                if (!line.startsWith(" ")) {
                    String[] w = line.split("\\s");
                    words.add(w[0].replace('_', ' '));
                }
                line = input.readLine();
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        data = words.toArray(new String[1]);
        System.out.println(words.size() + " words");
        System.out.println("Building searching data structure...");
        long time = System.currentTimeMillis();
        naive = new LinearSearch<>(data, new EditDistance(50, true));
        int naiveBuild = (int) (System.currentTimeMillis() - time) / 1000;
        time = System.currentTimeMillis();
        bktree = new BKTree<>(new EditDistance(50, true));
        bktree.add(data);
        int bktreeBuild = (int) (System.currentTimeMillis() - time) / 1000;
        time = System.currentTimeMillis();
        cover = new CoverTree<>(data, new EditDistance(50, true));
        int coverBuild = (int) (System.currentTimeMillis() - time) / 1000;
        double[] buildTime = { naiveBuild, bktreeBuild, coverBuild };
        PlotCanvas build = BarPlot.plot(buildTime, label);
        build.setTitle("Build Time");
        canvas.add(build);
        validate();
    }
    int[] perm = Math.permutate(data.length);
    System.out.println("Perform 1000 searches...");
    long time = System.currentTimeMillis();
    List<Neighbor<String, String>> neighbors = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        naive.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time) / 1000;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        bktree.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time) / 1000;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        cover.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int coverSearch = (int) (System.currentTimeMillis() - time) / 1000;
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time of k = " + knn);
    canvas.add(search);
    if (canvas.getComponentCount() > 3)
        canvas.setLayout(new GridLayout(2, 2));
    validate();
    startButton.setEnabled(true);
    knnField.setEnabled(true);
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Neighbor(smile.neighbor.Neighbor) FileInputStream(java.io.FileInputStream) GridLayout(java.awt.GridLayout) BufferedReader(java.io.BufferedReader) EditDistance(smile.math.distance.EditDistance) PlotCanvas(smile.plot.PlotCanvas)

Example 3 with Neighbor

use of smile.neighbor.Neighbor in project smile by haifengl.

the class KNNDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    logNSlider.setEnabled(false);
    dimensionSlider.setEnabled(false);
    knnField.setEnabled(false);
    logN = logNSlider.getValue();
    dimension = dimensionSlider.getValue();
    System.out.println("Generating dataset...");
    int n = (int) Math.pow(10, logN);
    double[][] data = new double[n][];
    for (int i = 0; i < n; i++) {
        data[i] = new double[dimension];
        for (int j = 0; j < dimension; j++) {
            data[i][j] = Math.random();
        }
    }
    int[] perm = Math.permutate(n);
    System.out.println("Building searching data structure...");
    long time = System.currentTimeMillis();
    LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
    int naiveBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    KDTree<double[]> kdtree = new KDTree<>(data, data);
    int kdtreeBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    CoverTree<double[]> cover = new CoverTree<>(data, new EuclideanDistance());
    int coverBuild = (int) (System.currentTimeMillis() - time);
    System.out.println("Perform 1000 searches...");
    double radius = 0.0;
    List<Neighbor<double[], double[]>[]> answers = new ArrayList<>(1000);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        answers.add(naive.knn(data[perm[i]], knn));
        for (int j = 0; j < answers.get(i).length; j++) {
            radius += answers.get(i)[j].distance;
        }
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time);
    radius /= 1000 * knn;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        kdtree.knn(data[perm[i]], knn);
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        cover.knn(data[perm[i]], knn);
    }
    int coverSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    LSH<double[]> lsh = new LSH<>(dimension, 5, (int) Math.log2(dimension), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        lsh.put(data[i], data[i]);
    }
    int lshBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    MPLSH<double[]> mplsh = new MPLSH<>(dimension, 2, (int) Math.log2(n), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        mplsh.put(data[i], data[i]);
    }
    double[][] train = new double[1000][];
    for (int i = 0; i < train.length; i++) {
        train[i] = data[perm[i]];
    }
    mplsh.learn(kdtree, train, 1.5 * radius);
    int mplshBuild = (int) (System.currentTimeMillis() - time);
    double lshRecall = 0.0;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        Neighbor<double[], double[]>[] neighbors = lsh.knn(data[perm[i]], knn);
        int hit = 0;
        for (int p = 0; p < knn && answers.get(i)[p] != null; p++) {
            for (int q = 0; q < knn && neighbors[q] != null; q++) {
                if (answers.get(i)[p].index == neighbors[q].index) {
                    hit++;
                    break;
                }
            }
        }
        lshRecall += 1.0 * hit / knn;
    }
    int lshSearch = (int) (System.currentTimeMillis() - time);
    lshRecall /= 1000;
    System.out.format("The recall of LSH is %.1f%%\n", lshRecall * 100);
    double mplshRecall = 0.0;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        Neighbor<double[], double[]>[] neighbors = mplsh.knn(data[perm[i]], knn, 0.95, 10);
        int hit = 0;
        for (int p = 0; p < knn && answers.get(i)[p] != null; p++) {
            for (int q = 0; q < knn && neighbors[q] != null; q++) {
                if (answers.get(i)[p].index == neighbors[q].index) {
                    hit++;
                    break;
                }
            }
        }
        mplshRecall += 1.0 * hit / knn;
    }
    int mplshSearch = (int) (System.currentTimeMillis() - time);
    mplshRecall /= 1000;
    System.out.format("The recall of MPLSH is %.1f%%\n", mplshRecall * 100);
    canvas.removeAll();
    double[] buildTime = { naiveBuild, kdtreeBuild, coverBuild, lshBuild, mplshBuild };
    PlotCanvas build = BarPlot.plot(buildTime, label);
    build.setTitle("Build Time");
    canvas.add(build);
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch, lshSearch, mplshSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time");
    canvas.add(search);
    validate();
    startButton.setEnabled(true);
    logNSlider.setEnabled(true);
    dimensionSlider.setEnabled(true);
    knnField.setEnabled(true);
}
Also used : MPLSH(smile.neighbor.MPLSH) LSH(smile.neighbor.LSH) CoverTree(smile.neighbor.CoverTree) ArrayList(java.util.ArrayList) Neighbor(smile.neighbor.Neighbor) EuclideanDistance(smile.math.distance.EuclideanDistance) KDTree(smile.neighbor.KDTree) MPLSH(smile.neighbor.MPLSH) LinearSearch(smile.neighbor.LinearSearch) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

ArrayList (java.util.ArrayList)3 Neighbor (smile.neighbor.Neighbor)3 PlotCanvas (smile.plot.PlotCanvas)3 EuclideanDistance (smile.math.distance.EuclideanDistance)2 CoverTree (smile.neighbor.CoverTree)2 KDTree (smile.neighbor.KDTree)2 LSH (smile.neighbor.LSH)2 LinearSearch (smile.neighbor.LinearSearch)2 MPLSH (smile.neighbor.MPLSH)2 GridLayout (java.awt.GridLayout)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 EditDistance (smile.math.distance.EditDistance)1