【发布时间】:2017-05-31 13:11:44
【问题描述】:
我正在调试一个库和另一个涉及计算 k 最近邻居的实现。我用一个我难以理解的例子来阐述这个问题。
首先我将通过一个玩具示例来解释演示,然后显示将导致问题的输出。
任务
这里的演示读取一个包含 10 个二维数据点的 csv 文件。任务是找出所有数据点与第一个数据点的距离,并以非递减顺序列出所有点和与第一个数据点的距离。
基本上,这是基于 kNN 的算法的一个组件,当我执行 Java 版本(库的组件)和用 R 编写它时,我发现了差异。为了演示差异,请考虑以下代码。
代码 1:Java + WEKA
以下代码使用 Java 和 WEKA。我使用LinearNNSearch 来计算最近的邻居。使用它的原因是LinearNNSearch 用于我正在调试和/或与 R 代码比较的特定库中。
import weka.core.converters.CSVLoader;
import weka.core.Instances;
import weka.core.DistanceFunction;
import weka.core.EuclideanDistance;
import weka.core.Instances;
import weka.core.neighboursearch.LinearNNSearch;
import java.io.File;
class testnn
{
public static void main (String args[]) throws Exception
{
// Load csv
CSVLoader loader = new CSVLoader ();
loader.setSource (new File (args[0]));
Instances df = loader.getDataSet ();
// Set the LinearNNSearch object
EuclideanDistance dist_obj = new EuclideanDistance ();
LinearNNSearch lnn = new LinearNNSearch ();
lnn.setDistanceFunction(dist_obj);
lnn.setInstances(df);
lnn.setMeasurePerformance(false);
// Compute the K-nearest neighbours of the first datapoint (index 0).
Instances knn_pts = lnn.kNearestNeighbours (df.instance (0), df.numInstances ());
// Get the distances.
double [] dist_arr = lnn.getDistances ();
// Print
System.out.println ("Points sorted in increasing order from ");
System.out.println (df.instance (0));
System.out.println ("V1,\t" + "V2,\t" + "dist");
for (int j = 0; j < knn_pts.numInstances (); j++)
{
System.out.println (knn_pts.instance (j) + "," + dist_arr[j]);
}
}
}
代码 2:R
为了计算我使用dist 的距离。使用daisy 也会得到相同的答案。
// Read file
df <- read.csv ("dat.csv", header = TRUE);
// All to all distances, and select distances of points from first datapoint (index 1)
dist_mat <- as.matrix (dist (df, diag=TRUE, upper=TRUE, method="euclidean"));
first_pt_to_all <- dist_mat[,1];
// Sort the datapoints and also record the ordering
sorted_order <- sort (first_pt_to_all, index.return = TRUE, decreasing = FALSE);
// Prepare dataset with the datapoints ordered in the non-decreasing order of the distance from the first datapoint
df_sorted <- cbind (df[sorted_order$ix[-1],], dist = sorted_order$x[-1]);
// Print
print ("Points sorted in increasing order from ");
print (df[1,]);
print (df_sorted);
输出
为了便于比较,我将两个输出并排放置。两个表都以非递减顺序显示点数。
- 左侧表由 R 生成,R 输出中最左侧的列表示原始数据点索引。
- 右侧表格由 Java + WEKA 生成。
问题
距离明显不同,一些数据点的排序也不同。
可视化
我已经绘制了 10 个点并根据它们的排序顺序对它们进行了编号,由图中的数字表示。
- 黑色文本表示从R生成的排序数据集中绘制的点
- 红色文本表示从Java + WEKA生成的排序数据集中绘制的点
因此 4、5 和 6 不同。如果两个数据点等距,那么这将解释不同的排序,但没有两个点与第一个数据点等距。
数据集
“V1”、“V2” 0.560954,0.313231 0.330345,-0.137681 0.870481,-0.302856 1.038752,0.396173 0.866816,0.476897 1.113799,0.42203 1.053889,0.486349 0.416051,-0.338858 1.386459,0.425101 0.262637,0.554558问题
- 为什么dist列的距离不同,导致最近邻点的排序不同?
- 您是否可以在代码或我使用库的方式中发现任何错误?我是否正确使用了这些(尤其是 WEKA)?
如果有不清楚的地方或了解更多信息,请发表评论。
【问题讨论】:
-
很容易看出R距离是正确的。例如,仅使用您的测试点和列表中的第一个点: p1 = c(0.560954, 0.313231); p2 = c(0.866816, 0.476897); sqrt(sum((p1 - p2) * (p1 - p2))); [1] 0.3468979
-
@G5W R 距离是正确的,毫无疑问。尽管问题仍然存在,但 WEKA 有什么问题?还是用错了?