NAIVE BAYESIAN CLASSIFICATION

JAVA PROGRAM CODE :

import java.io.*;
import java.math.*;
class Bayes
{       BufferedReader sc=new BufferedReader(newInputStreamReader(System.in));
        int i;
        double y1,y2;
        double c=0.0,c1=0.0,c3=0.0,c4,c5=0.0,c6;
  String a[][]={{"y","h","n","f","n"},{"y","h","n","e","n"},{"m_a","h","n","f","y"},{"s","m","n","f","y"},{"s","l","y","f","y"},{"s","l","y","e","n"},{"m_a","l","y","e","y"},{"y","m","n","f","n"},{"y","l","y","f","y"},{"s","m","y","f","y"},{"y","m","y","e","y"},{"m_a","m","n","e","y"},{"m_a","h","y","f","y"},{"s","m","n","e","n"},};
         void start1() throws Exception
        {
                System.out.println("Enter the conditions to see whether can buy computer or not ");
                System.out.println("Enter age : ");
                String x1=sc.readLine();
                System.out.println("Enter income : ");
                String x2=sc.readLine();
                System.out.println("Enter student y or n: ");
                String x3=sc.readLine();
                System.out.println("Enter credit rating: ");
                String x4=sc.readLine();
                entropy();
                double a1=atto(x1,0);
                System.out.println("P("+x1+"/y) = " +a1);
                double a2=atto(x2,1);
                System.out.println("P("+x2+"/y) = " +a2);
                double a3=atto(x3,2);
                System.out.println("P("+x3+"/y) = " +a3);
                double a4=atto(x4,3);
                System.out.println("P("+x4+"/y) = " +a4);
                double b1=atto1(x1,0);
                System.out.println("P("+x1+"/n) = " +b1);
                double b2=atto1(x2,1);
                System.out.println("P("+x2+"/n) = " +b2);
                double b3=atto1(x3,2);
                System.out.println("P("+x3+"/n) = " +b3);
                double b4=atto1(x4,3);
                System.out.println("P("+x4+"/n) = " +b4);
                double p1=(a1*a2*a3*a4);
                System.out.println("P(X/y) = " +p1);
                double p2=(b1*b2*b3*b4);
                System.out.println("P(X/n) = " +p2);
                p1=(p1*y1);
                System.out.println("P(y/X) = " +p1);
                p2=(p2*y2);
                System.out.println("P(n/X) = " +p2);
                if(p1>p2)
                {     System.out.println("You Can buy computer");
                }
                else
                {      System.out.println("You Can't buy computer");
                }
        }
        void entropy()
        {      c1=0.0;
                for(i=0;i<14 data-blogger-escaped-i="" data-blogger-escaped-o:p="">
                {
                        if(a[i][4].equals("y"))
                        c++;
                }
                c1=14-c;
                y1=(c/14.0);
                y2=(c1/14.0);
                System.out.println("P(y):"+ y1);
                System.out.println("P(n):"+ y2);
        }
        double atto(String at,int z)
        {      c3=0.0;
                for(i=0;i<14 data-blogger-escaped-i="" data-blogger-escaped-o:p="">
                {
                        if(a[i][z].equals(at))
                        {
                                if(a[i][4].equals("y"))
                                        c3++;
                        }
                }
                return((c3/c));
        }
        double atto1(String at1,int z1)
        {      c4=0.0;
                for(i=0;i<14 data-blogger-escaped-i="" data-blogger-escaped-o:p="">
                {
                        if(a[i][z1].equals(at1))
                        {
                                if(a[i][4].equals("n"))
                                        c4++;
                        }      
                }
                return((c4/c1));
        }
}
class Bayesmain1
{       public static void main(String args[]) throws Exception
        {      Bayes d=new Bayes();
                d.start1();
        }

}

OUTPUT :


No comments :

Post a Comment