Java Code For Bayesian Classifier

Java Code For Bayesian Classifier
















import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
public class Bayesian {
    public static double getProbability(String value){
        String file = "data1.txt";
        String line = null;
        int i = 0, c = 0, tot = 0;      
        try{
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);          
            while((line=br.readLine())!=null){
                i++;
                if(i == 1)
                continue;                              
                                tot++;
                String colData[] = line.split(" ");              
                if(colData[5].equals(value)){
                    c++;                  
                }            }          
            br.close();
                }
        catch(Exception e){
            System.out.println(e);
        }             
                return((double)c/tot);      
    }  
    public static int getCount(String value){
                String file = "data1.txt";
        String line = null;
        int i = 0, c = 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[5].equals(value)){
                    c++;                  
                }            }
            br.close();
                }
        catch(Exception e){
            System.out.println(e);
        }
                return(c);      
    }  
     public static double getConditionalProbability(String unknown[], String value, int count, double pc){
                String file = "data1.txt";
        String line = null;
        int i = 0, c[] = new int[4];               
                double p = 1;    
        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(" ");              
                                for(int j =0; j<4; j++){
                                    if(colData[j+1].equals(unknown[j]) && colData[5].equals(value)){
                                            c[j]++;                  
                                    }                                }
            }
            br.close();
                }
        catch(Exception e){
            System.out.println(e);
        }
                for(int j = 0; j<4; j++){
                                p = p*((double)c[j]/count);
                }    
                return(p*pc);
    }  
    public static void main(String[] args) {      
        Scanner scan = new Scanner(System.in);
        String attributes[] = {"id", "age", "income", "student", "credit", "computer_buyer"};
        String unknown[] = new String[4];
        System.out.println("Enter unknown sample's details:");
        System.out.println("age:");
        unknown[0] = scan.next();
        System.out.println("income");
        unknown[1] = scan.next();
        System.out.println("student:");
        unknown[2] = scan.next();
        System.out.println("credit:");
        unknown[3] = scan.next();
        double pc1 = getProbability("yes");
        double pc2 = getProbability("no");
        int no_yes = getCount("yes");
        int no_no = getCount("no");
        System.out.println("pc1: "+ pc1 +" pc2:" + pc2);
        System.out.println("yes: "+ no_yes +" no" + no_no);
                double pc1x = getConditionalProbability(unknown, "yes", no_yes, pc1);
                double pc2x = getConditionalProbability(unknown, "no", no_no, pc2);
                if(pc1x > pc2x){
                                System.out.println("The given sample belong to class yes");
                }
                else{
                                System.out.println("The given sample belong to class no");
                }      
    }






0 comments :