Java 欧几里德距离的回答不正确
我正在编制一个程序,用4个测试数据值计算4个训练数据值的欧氏距离。对于KNN,我需要找到我用Java编程的欧几里德距离,我从结果中得到了一个错误的答案Java 欧几里德距离的回答不正确,java,euclidean-distance,Java,Euclidean Distance,我正在编制一个程序,用4个测试数据值计算4个训练数据值的欧氏距离。对于KNN,我需要找到我用Java编程的欧几里德距离,我从结果中得到了一个错误的答案 /** * * */ public class KNN { double w1,x1,y1,z1; double w2,x2,y2,z2; Scanner n=new Scanner(System.in); ////3 preset values of the Test Data/////// void Test() {
/**
*
*
*/
public class KNN {
double w1,x1,y1,z1;
double w2,x2,y2,z2;
Scanner n=new Scanner(System.in);
////3 preset values of the Test Data///////
void Test()
{
int x;
System.out.println("Enter 1 for TD 1 2 for TD 2 and 3 for TD 3");
x=n.nextInt();
if(x==1){
w2=89;
x2=34;
y2=27;
z2=38;}
else if(x==2)
{
w2=58;
x2=87;
y2=35;
z2=38;}
else if(x==3)
{
w2=34;
x2=61;
y2=49;
z2=68;}
}
//////Input values of Training Data/////
void input()
{
System.out.println("Enter Feature 1 of Training Data ");
w1=n.nextDouble();
System.out.println("Enter Feature 2 of Training Data ");
x1=n.nextDouble();
System.out.println("Enter Feature 3 of Training Data ");
y1=n.nextDouble();
System.out.println("Enter Feature 4 of Training Data ");
y1=n.nextDouble();
}
///////Formula for the Euclidean Distance for Training and Test data//////
double formula()
{ double r1,r2,r3,r4;
double r5;
r1=(w2-w1);
r2=(x2-x1);
r3=(y2-y1);
r4=(z2-z1);
r5=(r1*r1)+(r2*r2)+(r3*r3)+(r4*r4);
System.out.println(r5);
return Math.sqrt(r5) ;
}
我得到的答案是测试值,在这种情况下是w1,x1,y1,z1
分别是89,34,27和38,我使用的训练数据是69,72,72和29
用计算器我得到的答案是62.84
但从代码的结果来看,是给了我一个答案
Start program? 1 for yes and anything else for cancel
1
Enter 1 for TD 1 2 for TD 2 and 3 for TD 3
1
Enter Feature 1 of Training Data
69
Enter Feature 2 of Training Data
72
Enter Feature 3 of Training Data
72
Enter Feature 4 of Training Data
29
3292.0
57.37595315112421
Enter 1 for TD 1 2 for TD 2 and 3 for TD 3
请注意,这个程序是为了完成作业,所以我在5分钟内完成了。因此,对它的格式化方式,请对我宽容一点。您的输入方法错误地将
y1
赋值两次,而不是将上次读取的值赋值给z1
哦,是的!。成功了!。谢谢你,伙计。现在我对这样一个小错误感到很愚蠢。这个公式有问题吗?