【问题标题】:Incorrect answer of Euclidean Distance Java欧几里得距离Java的错误答案
【发布时间】:2017-03-30 11:56:03
【问题描述】:

我正在编写一个程序来计算 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()
{  
    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 分钟内完成了。所以对我来说它的形成方式放轻松

【问题讨论】:

    标签: java euclidean-distance


    【解决方案1】:

    您的输入法错误地分配了两次y1,而不是将上次读取的值分配给z1

    【讨论】:

    • 哦,是的!那行得通!谢啦。现在我为这样一个小错误感到愚蠢。
    • 公式有问题吗?
    猜你喜欢
    • 2014-11-06
    • 2013-03-02
    • 2015-07-15
    • 2014-02-04
    • 1970-01-01
    • 2018-01-28
    • 2021-10-01
    相关资源
    最近更新 更多