Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.09 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4. import au.com.bytecode.opencsv.CSVReader;
  5.  
  6.  
  7.  
  8. // This program reads the dataset, and the new instances.
  9. // Note that an instance is a patient in this example.
  10. // Then the program will compute and display the distances between the new
  11. // instance / patient and all the instances / patients from the dataset, based on symptoms.
  12. // The distance between two instances is defined here as the number of attribute value disparities.
  13. // For instance the distance between two patients is 2 if one has fever and the other doesn't,
  14. // and one has headache and the other doesn't, and all their other symptoms are the same.
  15.  
  16. public class example1 {
  17.  
  18.     //here one specifies the file containing the dataset in comma separated values format CSV
  19.  
  20.     private static final String datasetfile="diagnoses.csv";
  21.     static String [] nextLine;
  22.     static String[][] data= new String[100][7]; //this array will contain the dataset
  23.     static String[]  newinstance=new String[7]; //this array will contain a new instance / new patient symptoms
  24.     static int[] distance= new int[100];        //this array will contain the distance from the new instance to each instance in the dataset
  25.     static int min = 0;
  26.     static int index = 1;
  27.     static String x,y;
  28.     static int numberOFinstances;
  29.  
  30.     public static void KNearestNeighbour(int k){
  31.        
  32.         /*
  33.          * K Nearest Neighbour Algorithm
  34.          *
  35.          */
  36.        
  37.          
  38.         //loop through how many nearest neighbour's we want where k is the limit.
  39.         for(int j=0;j<k;j++){
  40.             //get the min value.
  41.             min =  distance[1];
  42.             //loop through all of the distances in the array.
  43.         for(int i=1;i<numberOFinstances+1;i++){
  44.  
  45.             //if current value of array is less than min.
  46.             if(distance[i]<min){
  47.  
  48.                 //assign min to current array value.
  49.                 min=distance[i];
  50.  
  51.                 //record index so we know where we found it.
  52.                 index=i;
  53.  
  54.             }
  55.            
  56.            
  57.        
  58.         }
  59.         //Put a random high number so we don't get the same minimum
  60.         distance[index]=999;
  61.             //Print Distance
  62.             System.out.println("\nDistance is "+min);
  63.             //Print Diagnosis
  64.             System.out.println("Diagnosis is "+data[index][6]);
  65.             //Print patient using the index we found the minimum distance at.
  66.             System.out.println("Close distance to Patient"+index+"\n");
  67.         }
  68.        
  69.     }
  70.    
  71.     public static void NearestNeighbour(){
  72.        
  73.         /*
  74.          * Nearest Neighbour Algorithm
  75.          *
  76.          */
  77.        
  78.         //first element of array as min.
  79.         min =  distance[1];
  80.         for(int k=2;k<numberOFinstances;k++){
  81.  
  82.             //if current value of array is less than min
  83.             if(distance[k]<min){
  84.                 //assign min to current array value
  85.                 min=distance[k];
  86.                 //record index 
  87.                 index=k;
  88.        
  89.             }
  90.         }
  91.  
  92.             //Print Distance
  93.             System.out.println("\nDistance is "+min);
  94.             //Print Diagnosis
  95.             System.out.println("Diagnosis is "+data[index][6]);
  96.             //print the patient using the index we found the min value.
  97.             System.out.println("Nearest Neighbour is Patient "+index);
  98.     }
  99.    
  100.     public static void main(String[] args) throws IOException {
  101.  
  102.         //one declares variables and arrays to be used
  103.  
  104.         CSVReader reader = new CSVReader(new FileReader(datasetfile)); //this is used to read from CSV file
  105.        
  106.  
  107.         // prepare to read the dataset in array data
  108.         // and then to read a new instance / patient symptos from the keyboard
  109.  
  110.         int j,i=0;
  111.         while ((nextLine = reader.readNext()) != null) { //while the line inputed is not empty do
  112.  
  113.             for(j=0;j<=6;j++)
  114.                 data[i][j]=nextLine[j];
  115.  
  116.             i++;    // this increses i with 1
  117.         }
  118.  
  119.  
  120.         //in variable i we counted the number of instances in the dataset plus 1 because
  121.         //the attribute names were also included at the top of the dataset
  122.  
  123.          numberOFinstances = i-1;
  124.  
  125.  
  126.         //this prepares a loop to input a variable number of new instances and compute thier distances
  127.         //to the dataset instances
  128.  
  129.         Scanner in =new Scanner(System.in);  // prepare to read new instance from keyboard
  130.  
  131.         boolean finished=false;   // the next loop should execute until new instances finish
  132.  
  133.         while (!finished) {
  134.  
  135.             // Read a new instance (patient symptoms) and put it in an array called newinstance
  136.  
  137.             System.out.println();
  138.             System.out.println("Input the new patient's 5 symptoms regarding");
  139.             System.out.println("Sore Throat, Fever, Swollen Glands, Congestion, Headache");
  140.             System.out.println("Input Yes or No, ONE PER LINE, CASE SENSITIVE!:");
  141.             System.out.println();
  142.  
  143.  
  144.             /*if(in.nextLine().isEmpty())
  145.                     System.out.println("Empty: PLease enter Yes or No.");
  146.                     System.exit(0);
  147.              */
  148.             for(j=1;j<=5;j++){
  149.                 String read = in.nextLine();
  150.                 if(read.isEmpty()){
  151.                         System.err.println("Please enter either yes or no.");
  152.                         j
  153.                         --;
  154.                 }
  155.                 newinstance[j]=read;
  156.             }
  157.             // Compute the distances from the new instance to each instance in the dataset
  158.             // For example distance[8] should contain the distance from the new instance to instance 8
  159.             // Start with value 0 for all these distances first.
  160.  
  161.             for(i=1; i<= numberOFinstances; i++)
  162.                 distance[i]=0;
  163.  
  164.             System.out.println();
  165.  
  166.             for(i=1; i<= numberOFinstances; i++) {
  167.                 for(j=1;j<=5;j++) {
  168.                     x=newinstance[j];
  169.                     y=data[i][j];
  170.                      if (x.compareTo(y)!=0)  //if x and y do not coincide
  171.                         distance[i]++;      //then add 1 to the distance
  172.                 }
  173.  
  174.                 // Now the computed distances are ready, do display them
  175.  
  176.                 System.out.println("The distance between the new patient and patient"+i+" is "+distance[i]);
  177.             }
  178.             System.out.println("Calculating Nearest Neighbour");
  179.             //Nearest Neighbour
  180.             NearestNeighbour();
  181.             System.out.println("\n Calculating K Nearest Neighbour Where K=3");
  182.             //K Nearest Neighbour - 3 Closest Matches.
  183.             KNearestNeighbour(3);
  184.             // Ask user if new instances are to be inputed.
  185.             System.out.println();
  186.             System.out.print("Any more new patients? y/n: ");
  187.             x=in.nextLine();
  188.  
  189.             //If the input from user is not "y" (no more new instances) then one should finish the loop.
  190.  
  191.             if (x.compareTo("y")!=0){
  192.                 finished=true;
  193.                 System.out.println();
  194.                 System.out.println("GOODBYE .........");
  195.             }
  196.         }
  197.     }
  198. }
Add Comment
Please, Sign In to add comment