【发布时间】:2014-11-30 18:43:35
【问题描述】:
我正在将 R 脚本迁移到 Java。 R 脚本使用 apcluster 库。我正在尝试使用 Sandia Cognitive Foundry AffinityPropagation 类重新创建相同的输出。但是我发现很难适当地调整 selfDivergence 值。
这是我的 R 和 Java 代码。
library(apcluster)
NgramAdjMatrix <- matrix(
c(0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 0.0,
2.0, 4.0, 0.0, 3.0, 6.0, 0.0, 4.0, 8.0, 0.0, 5.0, 10.0, 0.0, 6.0, 12.0),
nrow=7,
ncol=3,
byrow = T)
LatentClusters <- apcluster(negDistMat(r=2),NgramAdjMatrix,seed=1234)
representatives <- LatentClusters@exemplars
clustMembers <- LatentClusters@clusters
FinalNgramMatrix <- NgramAdjMatrix[representatives,]
上面的 R 脚本给出了这个输出,
[,1] [,2] [,3]
[1,] 0 1 2
[2,] 0 4 8
这是我的 Java 代码,
Vector[] data = new Vector[]{
new Vector3(0.0, 0.0, 0.0),
new Vector3(0.0, 1.0, 2.0),
new Vector3(0.0, 2.0, 4.0),
new Vector3(0.0, 3.0, 6.0),
new Vector3(0.0, 4.0, 8.0),
new Vector3(0.0, 5.0, 10.0),
new Vector3(0.0, 6.0, 12.0)
};
System.out.println(Arrays.toString(data));
AffinityPropagation<Vectorizable> instance
= new AffinityPropagation<>(
EuclideanDistanceSquaredMetric.INSTANCE, 6);
Collection<CentroidCluster<Vectorizable>> clusters = instance.learn(Arrays.asList(data));
clusters.stream().forEach((cluster) -> {
System.out.println(cluster.getCentroid() + "...");
});
上面的 Java 代码给出了这个输出,
<0.0, 1.0, 2.0>
<0.0, 2.0, 4.0>
<0.0, 5.0, 10.0>
输出不同,很大程度上取决于我的代码中设置为 6 的 selfDivergence 参数。
有没有办法让 Java 代码的行为与 R 代码一样?
【问题讨论】:
标签: java r machine-learning cluster-analysis