我想找到每个点的最近邻居,我尝试使用karlhigley ANN模型。这是一段代码
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();
JavaRDD neighbors2给了我所有的邻居和他们的分数为null。任何人都可以帮助我理解我在哪里实施错误以及如何以正确的方式做到这一点?
这就是我打印输出的方式
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);
}
});
答案 0 :(得分:1)
解决方案:
印刷声明错了。它必须是:
System.out.println(f._1()+"====="+f._2()[i]._1()+"---"+f._2()[i]._2());
原因: 由于f._2和f._2()之间的类型转换问题。 这篇文章将详细解释: Java Tuple2 difference between using accessor method and calling the variable directly