朴素贝叶斯 代码实现

import java.util.Arrays;
import java.util.Scanner;
public class NaiveBayes  {
    public float sum=14;
    public static int na,yb;
    public static int a1,a2,b1,b2,c1,c2,d1,d2;
    public float x,y;
    public String []lei={"no","yes"}; //定义了买电脑的两种情况为一个数组
    public String classify[]={"ID","age","income","student","credit_rating","buys_computer"};
    public String []age={"youth","youth","middle_aged","senior","senior","senior","middle_aged",
                          "youth","youth","senior","youth","middle_aged","middle_aged","senior"};
    public String []income={"high","high","high","medium","low","low","low","medium","low","medium",
                          "medium","medium","high","medium"};
    public String []student={"no","no","no","no","yes","yes","yes","no","yes","yes","yes","no","yes","no"};
    public String []credit_rating={"fair","excellent","fair","fair","fair","excellent","excellent",
                          "fair","fair","fair","excellent","excellent","fair","excellent"};
    public String []buys_computer={"no","no","yes","yes","yes","no","yes","no","yes","yes","yes","yes",
                           "yes","no"};
    public String [][]data=new String[100][6];

    public NaiveBayes(){

        System.out.println("分类数据");
            for (int i=0,j=0;j                 data[i][j]=classify[j];
            System.out.println(Arrays.toString(data[0]));
            for (int k=1;k<=sum;k++) {
                data[k][0] = String.valueOf(k);//把int转化为String类型
                data[k][1]=age[k-1];
                data[k][2]=income[k-1];
                data[k][3]=student[k-1];
                data[k][4]=credit_rating[k-1];
                data[k][5]=buys_computer[k-1];
                System.out.println(Arrays.toString(data[k]));
            }

    }

    public void noandyes(){                     //实现buys_computer是no还是yes的统计
        for (int i=0;i             if(buys_computer[i]==lei[0])
                na++;
            else
                yb++;
    }
    private void Age(String s) {
        for (int i = 0; i < age.length; i++){
            if (age[i].equals(s) && buys_computer[i]==lei[0])
                a1++;
            else if(age[i].equals(s) && buys_computer[i]==lei[1])
                a2++;
        }
        System.out.println("P(age="+s+" | buys_computer="+lei[0]+")="+a1+"/"+na+"="+(float)a1/na);
        System.out.println("P(age="+s+" | buys_computer="+lei[1]+")="+a2+"/"+yb+"="+(float)a2/yb);
    }
    private void Income(String s){

        for (int i = 0; i < age.length; i++){
            if (income[i].equals(s) && buys_computer[i]==lei[0])
                b1++;
            else if(income[i].equals(s) && buys_computer[i]==lei[1])
                b2++;

        }
        System.out.println("P(income="+s+" | buys_computer="+lei[0]+")="+b1+"/"+na+"="+(float)b1/na);
        System.out.println("P(income="+s+" | buys_computer="+lei[1]+")="+b2+"/"+yb+"="+(float)b2/yb);
    }
    private void Student(String s){

        for (int i = 0; i < age.length; i++){
            if (student[i].equals(s) && buys_computer[i]==lei[0])
                c1++;
            else if(student[i].equals(s) && buys_computer[i]==lei[1])
                c2++;
        }
        System.out.println("P(student="+s+" | buys_computer="+lei[0]+")="+c1+"/"+na+"="+(float)c1/na);
        System.out.println("P(student="+s+" | buys_computer="+lei[1]+")="+c2+"/"+yb+"="+(float)c2/yb);
    }

    private void Credit_rating(String s){

        for (int i = 0; i < age.length; i++){
            if (credit_rating[i].equals(s) && buys_computer[i]==lei[0])
                d1++;
            else if(credit_rating[i].equals(s) && buys_computer[i]==lei[1])
                d2++;
        }
        System.out.println("P(credit_rating="+s+" | buys_computer="+lei[0]+")="+d1+"/"+this.na+"="+(float)d1/this.na);
        System.out.println("P(credit_rating="+s+" | buys_computer="+lei[1]+")="+d2+"/"+this.yb+"="+(float)d2/this.yb);
    }

    private void no(){   //计算X值在no的情况的概率
       x=((float)a1/na)*((float)b1/na)*((float)c1/na)*((float)d1/this.na);
       System.out.println("P(X |buys_computer="+lei[0]+")="+(float)a1/na+"*"+(float)b1/na+"*"+(float)c1/na+"*"+(float)d1/na+"="+x);

    }
    private void yes(){   //计算X值在yes的情况的概率
        y=((float)a2/yb)*((float)b2/yb)*((float)c2/yb)*((float)d2/yb);
        System.out.println("P(X |buys_computer="+lei[1]+")="+(float)a2/yb+"*"+(float)b2/yb+"*"+(float)c2/yb+"*"+(float)d2/yb+"="+y);

    }
    private void probability(){
        float a,b;
        System.out.println("P(X |buys_computer="+lei[0]+")*"+"P(buys_computer="+lei[0]+")");
        a=x*(na/sum);
        System.out.println("="+a);
        System.out.println("P(X |buys_computer="+lei[1]+")*"+"P(buys_computer="+lei[1]+")");
        b=y*(yb/sum);
        System.out.println("="+b);
        if(a>b)
            System.out.println(a+">"+b+"   X belongs to class buys_computer ="+lei[0]);
        else
            System.out.println(a+"<"+b+"   X belongs to class buys_computer ="+lei[1]);
    }
    public static void main(String []args){
        NaiveBayes N=new NaiveBayes();
        N.noandyes();
       Scanner t=new Scanner(System.in);
        String a=t.next();
        String b=t.next();
        String c=t.next();
        String d=t.next();
        N.Age(a);
        N.Income(b);
        N.Student(c);
        N.Credit_rating(d);
        System.out.println();
        N.no();
        N.yes();
        System.out.println();
        N.probability();

    }
}
 

你可能感兴趣的:(人工智能,人工智能)