Java实现的朴素贝叶斯算法示例

时间:2022-09-02 21:16:25

本文实例讲述了java实现的朴素贝叶斯算法。分享给大家供大家参考,具体如下:

对于朴素贝叶斯算法相信做数据挖掘和推荐系统的小伙们都耳熟能详了,算法原理我就不啰嗦了。我主要想通过java代码实现朴素贝叶斯算法,思想:

1. 用javabean +arraylist 对于训练数据存储

2. 对于样本数据训练

具体的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package nb;
/**
 * 训练样本的属性 javabean
 *
 */
public class javabean {
 int age;
 string income;
 string student;
 string credit_rating;
 string buys_computer;
 public javabean(){
 }
public javabean(int age,string income,string student,string credit_rating,string buys_computer){
  this.age=age;
  this.income=income;
  this.student=student;
  this.credit_rating=credit_rating;
  this.buys_computer=buys_computer;
}
public int getage() {
  return age;
}
public void setage(int age) {
  this.age = age;
}
public string getincome() {
  return income;
}
public void setincome(string income) {
  this.income = income;
}
public string getstudent() {
  return student;
}
public void setstudent(string student) {
  this.student = student;
}
public string getcredit_rating() {
  return credit_rating;
}
public void setcredit_rating(string credit_rating) {
  this.credit_rating = credit_rating;
}
public string getbuys_computer() {
  return buys_computer;
}
public void setbuys_computer(string buys_computer) {
  this.buys_computer = buys_computer;
}
@override
public string tostring() {
  return "javabean [age=" + age + ", income=" + income + ", student="
      + student + ", credit_rating=" + credit_rating + ", buys_computer="
      + buys_computer + "]";
}
}

算法实现的部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package nb;
import java.io.bufferedreader;
import java.io.file;
import java.io.filereader;
import java.util.arraylist;
public class testnb {
  /**data_length
   * 算法的思想
   */
  public static arraylist<javabean> list = new arraylist<javabean>();;
  static int data_length=0;
  public static void main(string[] args) {
    // 1.读取数据,放入list容器中
    file file = new file("e://test.txt");
    txt2string(file);
    //数据测试样本
    testdata(25,"medium","yes","fair");
  }
  // 读取样本数据
  public static void txt2string(file file) {
    try {
      bufferedreader br = new bufferedreader(new filereader(file));// 构造一个bufferedreader类来读取文件
      string s = null;
      while ((s = br.readline()) != null) {// 使用readline方法,一次读一行
        data_length++;
        splitt(s);
      }
      br.close();
    } catch (exception e) {
      e.printstacktrace();
    }
  }
  // 存入arraylist中
   public static void splitt(string str){
      string strr = str.trim();
      string[] abc = strr.split("[\\p{space}]+");
      int age=integer.parseint(abc[0]);
      javabean bean=new javabean(age, abc[1], abc[2], abc[3], abc[4]);
      list.add(bean);
    }
   // 训练样本,测试
   public static void testdata(int age,string a,string b,string c){
     //训练样本
     int number_yes=0;
     int bumber_no=0;
     // age情况 个数
     int num_age_yes=0;
     int num_age_no=0;
     // income
     int num_income_yes=0;
     int num_income_no=0;
     // student
     int num_student_yes=0;
     int num_stdent_no=0;
     //credit
     int num_credit_yes=0;
     int num_credit_no=0;
     //遍历list 获得数据
     for(int i=0;i<list.size();i++){
      javabean bb=list.get(i);
      if(bb.getbuys_computer().equals("yes")){ //yes
        number_yes++;
        if(bb.getincome().equals(a)){//income
          num_income_yes++;
        }
        if(bb.getstudent().equals(b)){//student
          num_student_yes++;
        }
        if(bb.getcredit_rating().equals(c)){//credit
          num_credit_yes++;
        }
        if(bb.getage()==age){//age
          num_age_yes++;
        }
      }else {//no
        bumber_no++;
        if(bb.getincome().equals(a)){//income
          num_income_no++;
        }
        if(bb.getstudent().equals(b)){//student
          num_stdent_no++;
        }
        if(bb.getcredit_rating().equals(c)){//credit
          num_credit_no++;
        }
        if(bb.getage()==age){//age
          num_age_no++;
        }
      }
     }
      system.out.println("购买的历史个数:"+number_yes);
      system.out.println("不买的历史个数:"+bumber_no);
      system.out.println("购买+age:"+num_age_yes);
      system.out.println("不买+age:"+num_age_no);
      system.out.println("购买+income:"+num_income_yes);
      system.out.println("不买+income:"+num_income_no);
      system.out.println("购买+stundent:"+num_student_yes);
      system.out.println("不买+student:"+num_stdent_no);
      system.out.println("购买+credit:"+num_credit_yes);
      system.out.println("不买+credit:"+num_credit_no);
      //// 概率判断
      double buy_yes=number_yes*1.0/data_length; // 买的概率
      double buy_no=bumber_no*1.0/data_length; // 不买的概率
      system.out.println("训练数据中买的概率:"+buy_yes);
      system.out.println("训练数据中不买的概率:"+buy_no);
      /// 未知用户的判断
      double nb_buy_yes=(1.0*num_age_yes/number_yes)*(1.0*num_income_yes/number_yes)*(1.0*num_student_yes/number_yes)*(1.0*num_credit_yes/number_yes)*buy_yes;
      double nb_buy_no=(1.0*num_age_no/bumber_no)*(1.0*num_income_no/bumber_no)*(1.0*num_stdent_no/bumber_no)*(1.0*num_credit_no/bumber_no)*buy_no;
      system.out.println("新用户买的概率:"+nb_buy_yes);
      system.out.println("新用户不买的概率:"+nb_buy_no);
      if(nb_buy_yes>nb_buy_no){
        system.out.println("新用户买的概率大");
      }else {
        system.out.println("新用户不买的概率大");
      }
   }
}

对于样本数据:

25  high    no  fair       no
25  high    no  excellent  no
33  high    no  fair       yes
41  medium  no  fair       yes
41  low     yes fair       yes
41  low     yes excellent  no
33  low     yes excellent  yes
25  medium  no  fair       no
25  low     yes fair       yes
41  medium  yes fair       yes
25  medium  yes excellent  yes
33  medium  no  excellent  yes
33  high    yes fair       yes
41  medium  no  excellent  no

对于未知用户的数据得出的结果:

购买的历史个数:9
不买的历史个数:5
购买+age:2
不买+age:3
购买+income:4
不买+income:2
购买+stundent:6
不买+student:1
购买+credit:6
不买+credit:2
训练数据中买的概率:0.6428571428571429
训练数据中不买的概率:0.35714285714285715
新用户买的概率:0.028218694885361547
新用户不买的概率:0.006857142857142858
新用户买的概率大

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/u011015260/article/details/51672467