Karlhigley LSH ANN model for finding nearest neighbors giving null results

119 views Asked by At

I want to find the nearest neighbors to each of my points and I tried it using karlhigley ANN model. Here is the piece of code

List<Tuple2<Object, SparseVector>> svList = new ArrayList<>();
        svList.add(new Tuple2<Object, SparseVector>(3L,
                (Vectors.sparse(20, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
                        new double[] { 5.0f, 3.0f, 4.0f, 5.0f, 5.0f, 1.0f, 5.0f, 3.0f, 4.0f, 5.0f, 5.0f, 3.0f, 4.0f,
                                5.0f, 5.0f, 1.0f, 5.0f, 3.0f, 4.0f, 5.0f })
                        .toSparse())));
        svList.add(new Tuple2<Object, SparseVector>(4L,
                (Vectors.sparse(20, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
                        new double[] { 1.0f, 2.0f, 1.0f, 5.0f, 1.0f, 5.0f, 1.0f, 4.0f, 1.0f, 3.0, 1.0f, 2.0f, 1.0f,
                                5.0f, 1.0f, 5.0f, 1.0f, 4.0f, 1.0f, 3.0f })
                        .toSparse())));
        svList.add(new Tuple2<Object, SparseVector>(6L,
                (Vectors.sparse(20, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
                        new double[] { 5.0f, 3.0f, 4.0f, 1.0f, 5.0f, 4.0f, 1.0f, 3.0f, 4.0f, 5.0f, 5.0f, 3.0f, 4.0f,
                                1.0f, 5.0f, 4.0f, 1.0f, 3.0f, 4.0f, 5.0f })
                        .toSparse())));

RDD<Tuple2<Object, SparseVector>> points = sc.parallelize(svList).rdd();

ANNModel annModel =
                new ANN(20, "cosine")
                .setTables(1)
                .setSignatureLength(20).setRandomSeed(3)
                .train(points,StorageLevel.MEMORY_AND_DISK());

JavaRDD<Tuple2<Object, Tuple2<Object, Object>[]>> neighbors2 = annModel.neighbors(3).toJavaRDD();

The JavaRDD neighbors2 gives me all the neighbors and their scores as null. Can anyone help me out in understanding where am I implementing wrong and how to do it the right way?

This is how I am printing the output

neighbors2.foreach(f->{
            for(int i=0;i<f._2.length;i++){
                System.out.println(f._1+"====="+f._2[i]._1+"---"+f._2[i]._2);
            }
        });
1

There are 1 answers

0
Goutham Panneeru On

Solution:

The print statement was wrong. It has to be:

System.out.println(f._1()+"====="+f._2()[i]._1()+"---"+f._2()[i]._2());

Reason: Due to typecasting issues between f._2 and f._2(). This post will explain it in detail: Java Tuple2 difference between using accessor method and calling the variable directly