2014-02-21 130 views
0

您好傢伙我寫了一個KNN分類器程序,其中我必須根據雙變量的距離對對象進行排序,但我無法獲得正確的輸出。這裏是我的代碼:無法基於雙重屬性對對象進行排序

import java.util.*; 
import java.io.*; 

class Distance{ 
int index; 
double distance; 

public Distance(int index, double distance){ 
    this.index = index; 
    this.distance = distance; 
} 
} 

class KNN{ 

public static int getCount(){ 
    String file = "data2.txt"; 

    String line = null; 

    int i = 0; 

    try{ 
     FileReader fr = new FileReader(file); 
     BufferedReader br = new BufferedReader(fr); 
     while((line=br.readLine())!=null){ 
      i++; 
     } 
     br.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
    } 
    i = i-1; 
    return i; 
} 

public static double[] setGenderValues(int k){ 
    double g[] = new double[k]; 
    String file = "data2.txt"; 
    String line = null; 
    int i = 0; 

    try{ 
     FileReader fr = new FileReader(file); 

     BufferedReader br = new BufferedReader(fr); 

     while((line=br.readLine())!=null){ 
      i++; 
      if(i == 1) 
      continue; 

      String colData[] = line.split(" "); 

      if(colData[1].equals("f")){ 
       g[i-2] = 1; 
      } 
      else if(colData[1].equals("m")){ 
       g[i-2] = 0; 
      } 
     } 

     br.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
    } 
    return g; 
} 

public static double getMinHeight(double h[]){ 
    double min = h[0]; 
    for(int i = 1; i<h.length; i++){ 
     if(h[i] < min){ 
      min = h[i]; 
     } 
    } 
    return min; 

} 

public static double getMaxHeight(double h[]){ 
    double max = h[0]; 
    for(int i = 1; i<h.length; i++){ 
     if(h[i] > max){ 
      max = h[i]; 
     } 
    } 
    return max; 
} 

public static double[] setHeightValues(int k){ 
    String file = "data2.txt"; 
    double h[] = new double[k]; 
    String line = null; 
    int i = 0; 

    try{ 
     FileReader fr = new FileReader(file); 

     BufferedReader br = new BufferedReader(fr); 

     while((line=br.readLine())!=null){ 
      i++; 
      if(i == 1) 
      continue; 
      String colData[] = line.split(" "); 
      h[i-2] = Double.parseDouble(colData[2]); 

     } 


     br.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
    } 

    return h; 
} 

public static double[] setClassValues(int k){ 
    String file = "data2.txt"; 

    String line = null; 
    double c[] = new double[k]; 
    int i = 0; 

    try{ 
     FileReader fr = new FileReader(file); 

     BufferedReader br = new BufferedReader(fr); 

     while((line=br.readLine())!=null){ 
      i++; 
      if(i == 1) 
      continue; 
      String colData[] = line.split(" "); 

      if(colData[3].equals("tall")){ 
       c[i-2] = 3; 
      } 
      else if(colData[3].equals("medium")){ 
       c[i-2] = 2; 
      } 
      else if(colData[3].equals("short")){ 
       c[i-2] = 1; 
      } 
     } 


     br.close(); 
    } 
    catch(Exception e){ 
     System.out.println(e); 
    } 
    return c; 
} 

public static void main(String args[]){ 
    Scanner scan = new Scanner(System.in); 
    double u[] = new double[2]; 
    System.out.println("Enter gender; 1 for female, 0 for male"); 
    u[0] = scan.nextDouble(); 
    System.out.println("Enter height:"); 
    u[1] = scan.nextDouble(); 

    int k = getCount(); 

    //now, we normalize gender 
    double g[] = setGenderValues(k); 

    double h[] = setHeightValues(k); 

    double vmin = getMinHeight(h); 
    double vmax = getMaxHeight(h); 

    //now, we normalize height 
    for(int i = 0; i < k; i++){ 
     h[i] = (h[i] - vmin)/(vmax - vmin); 
    } 
    u[1] = (u[1] - vmin)/(vmax - vmin); 

    //now, we set class values 
    double c[] = setClassValues(k); 

    /*System.out.println("gender height class"); 
    for(int i = 0;i< 10; i++){ 
     System.out.println(g[i]+" "+h[i]+" "+c[i]); 
    }*/ 

    int n = (int)Math.sqrt(k); 

    Distance d[] = new Distance[k]; 

    for(int i=0; i<k; i++){ 
     double distance = (u[0]-g[i])*(u[0]-g[i]) + (u[1]-h[i])*(u[1]-h[i]); 
     distance = Math.sqrt(distance); 
     d[i] = new Distance(i , distance); 
    } 

    for(int i =0; i<d.length; i++){ 
     System.out.println(d[i].index + " " + d[i].distance); 
    } 

    Distance temp; 
    for(int i=0; i<k-1; i++){ 
     for(int j=0; j<k-1; j++){ 
      if(d[j].distance > d[j+1].distance){ 
       temp = d[j]; 
       d[j] = d[j+1]; 
       d[j+1] = d[j]; 
      } 
     } 

    } 

    int count_tall = 0, count_short = 0, count_medium = 0; 
    System.out.println("sorted:"); 
    for(int i =0; i<d.length; i++){ 
     System.out.println(d[i].index + " " + d[i].distance); 
    } 
} 
} 

這是我的文本文件: 爲person_id性別身高類 圖1f 1.6短 2米2.0高 3米1.85介質 4 F 1.9介質 5米1.7短 6 ˚F1.8介質 7 F 1.95介質 8 F 1.75介質 9米2.2高大 10米2.1高

回答

0

閱讀Javadoc on the Comparable interface。如果你不知道界面是什麼,那就Google吧。

class Distance之後加implements Comparable

向班級中添加兩種方法。一個是boolean compareTo(Distance d) {},遵循Javadoc中有關Comparable的說明。另一個將是boolean equals(Distance d) {}。所有你必須添加的是你的雙重比較的邏輯。

現在,您的對象可以與>,<==進行比較。它們也可以按照他們以前不能做的方式進行排序和操作。

我會給你一個提示 - 你compareTo()邏輯是return this.distance.compareTo(d.distance);

+0

非常感謝你 –

相關問題