我正在调试库和另一个涉及计算k-最近邻居的实现。我正在用一个我难以理解的例子来构思这个问题。
首先,我将解释用玩具示例展示的东西,然后显示将导致问题的输出。
这里的演示读取了一个包含10个二维数据点的csv文件。任务是找到所有数据点与第一个数据点的距离,并以非递减顺序列出所有点和距第一个数据点的距离。
基本上,这是基于kNN的算法的一个组件,当我执行Java版本(库的组件)和用R写入时,我发现存在差异。为了证明这种差异,请考虑以下代码。
以下代码使用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]);
}
}
}
计算我使用的距离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 Java + WEKA [1] "Points sorted in increasing order from " Points sorted in increasing order from V1 V2 1 0.560954 0.313231 0.560954,0.313231 V1 V2 dist V1, V2, dist 5 0.866816 0.476897 0.3468979 0.866816,0.476897,0.3280721928065624 10 0.262637 0.554558 0.3837079 0.262637,0.554558,0.37871658916675316 4 1.038752 0.396173 0.4849436 1.038752,0.396173,0.43517244797543775 2 0.330345 -0.137681 0.5064604 1.053889,0.486349,0.4795184359817083 7 1.053889 0.486349 0.5224507 1.113799,0.42203,0.506782009966262 6 1.113799 0.422030 0.5634490 0.330345,-0.137681,0.5448256434359463 8 0.416051 -0.338858 0.6679947 0.416051,-0.338858,0.7411841020052856 3 0.870481 -0.302856 0.6894709 0.870481,-0.302856,0.7425541767563134 9 1.386459 0.425101 0.8330507 1.386459,0.425101,0.7451474897289354
距离明显不同,一些数据点排序也不同。
我绘制了10个点并根据它们的排序顺序对它们进行了编号,由图中的数字表示。
因此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
评论某些内容是否不清楚或有更多信息。
答案 0 :(得分:1)
如评论中所述,R距离是正确的。问题是WEKA默认值。你用过:
EuclideanDistance dist_obj = new EuclideanDistance ();
WEKA中的欧几里德距离具有默认参数。其中一个是DontNormalize=FALSE
,即默认情况下,WEKA在计算距离之前对数据进行标准化。我在java中帮助不大,所以我将在R中执行此操作。如果您缩放数据以使每个变量的最小值为零且最大值为1,您将获得WEKA提供的距离度量。
NData = Data
NData[,1] = (NData[,1]-min(NData[,1]))/(max(NData[,1])-min(NData[,1]))
NData[,2] = (NData[,2]-min(NData[,2]))/(max(NData[,2])-min(NData[,2]))
dist(NData)
这些距离与您为WEKA展示的距离相匹配。要获得与R相同的结果,请查看WEKA中的EuclideanDistance参数。