Java實(shí)現(xiàn)的樸素貝葉斯算法示例
本文實(shí)例講述了Java實(shí)現(xiàn)的樸素貝葉斯算法。分享給大家供大家參考,具體如下:
對(duì)于樸素貝葉斯算法相信做數(shù)據(jù)挖掘和推薦系統(tǒng)的小伙們都耳熟能詳了,算法原理我就不啰嗦了。我主要想通過(guò)java代碼實(shí)現(xiàn)樸素貝葉斯算法,思想:
1. 用javabean +Arraylist 對(duì)于訓(xùn)練數(shù)據(jù)存儲(chǔ)
2. 對(duì)于樣本數(shù)據(jù)訓(xùn)練
具體的代碼如下:
package NB;
/**
* 訓(xùn)練樣本的屬性 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 + "]";
}
}
算法實(shí)現(xiàn)的部分:
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.讀取數(shù)據(jù),放入list容器中
File file = new File("E://test.txt");
txt2String(file);
//數(shù)據(jù)測(cè)試樣本
testData(25,"Medium","Yes","Fair");
}
// 讀取樣本數(shù)據(jù)
public static void txt2String(File file) {
try {
BufferedReader br = new BufferedReader(new FileReader(file));// 構(gòu)造一個(gè)BufferedReader類(lèi)來(lái)讀取文件
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);
}
// 訓(xùn)練樣本,測(cè)試
public static void testData(int age,String a,String b,String c){
//訓(xùn)練樣本
int number_yes=0;
int bumber_no=0;
// age情況 個(gè)數(shù)
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 獲得數(shù)據(jù)
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("購(gòu)買(mǎi)的歷史個(gè)數(shù):"+number_yes);
System.out.println("不買(mǎi)的歷史個(gè)數(shù):"+bumber_no);
System.out.println("購(gòu)買(mǎi)+age:"+num_age_yes);
System.out.println("不買(mǎi)+age:"+num_age_no);
System.out.println("購(gòu)買(mǎi)+income:"+num_income_yes);
System.out.println("不買(mǎi)+income:"+num_income_no);
System.out.println("購(gòu)買(mǎi)+stundent:"+num_student_yes);
System.out.println("不買(mǎi)+student:"+num_stdent_no);
System.out.println("購(gòu)買(mǎi)+credit:"+num_credit_yes);
System.out.println("不買(mǎi)+credit:"+num_credit_no);
//// 概率判斷
double buy_yes=number_yes*1.0/data_length; // 買(mǎi)的概率
double buy_no=bumber_no*1.0/data_length; // 不買(mǎi)的概率
System.out.println("訓(xùn)練數(shù)據(jù)中買(mǎi)的概率:"+buy_yes);
System.out.println("訓(xùn)練數(shù)據(jù)中不買(mǎi)的概率:"+buy_no);
/// 未知用戶(hù)的判斷
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("新用戶(hù)買(mǎi)的概率:"+nb_buy_yes);
System.out.println("新用戶(hù)不買(mǎi)的概率:"+nb_buy_no);
if(nb_buy_yes>nb_buy_no){
System.out.println("新用戶(hù)買(mǎi)的概率大");
}else {
System.out.println("新用戶(hù)不買(mǎi)的概率大");
}
}
}
對(duì)于樣本數(shù)據(jù):
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
對(duì)于未知用戶(hù)的數(shù)據(jù)得出的結(jié)果:
購(gòu)買(mǎi)的歷史個(gè)數(shù):9
不買(mǎi)的歷史個(gè)數(shù):5
購(gòu)買(mǎi)+age:2
不買(mǎi)+age:3
購(gòu)買(mǎi)+income:4
不買(mǎi)+income:2
購(gòu)買(mǎi)+stundent:6
不買(mǎi)+student:1
購(gòu)買(mǎi)+credit:6
不買(mǎi)+credit:2
訓(xùn)練數(shù)據(jù)中買(mǎi)的概率:0.6428571428571429
訓(xùn)練數(shù)據(jù)中不買(mǎi)的概率:0.35714285714285715
新用戶(hù)買(mǎi)的概率:0.028218694885361547
新用戶(hù)不買(mǎi)的概率:0.006857142857142858
新用戶(hù)買(mǎi)的概率大
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Python實(shí)現(xiàn)的knn算法示例
- python使用KNN算法手寫(xiě)體識(shí)別
- 以Python代碼實(shí)例展示kNN算法的實(shí)際運(yùn)用
- kNN算法python實(shí)現(xiàn)和簡(jiǎn)單數(shù)字識(shí)別的方法
- Java實(shí)現(xiàn)的傅里葉變化算法示例
- Java實(shí)現(xiàn)五子棋AI算法
- 使用棧的迷宮算法java版代碼
- Java實(shí)現(xiàn)走迷宮回溯算法
- Java實(shí)現(xiàn)Floyd算法求最短路徑
- JAVA實(shí)現(xiàn)感知器算法
- Java實(shí)現(xiàn)的KNN算法示例
相關(guān)文章
java統(tǒng)計(jì)字符串中重復(fù)字符出現(xiàn)次數(shù)的方法
這篇文章主要介紹了java統(tǒng)計(jì)字符串中重復(fù)字符出現(xiàn)次數(shù)的方法,涉及java針對(duì)字符串的遍歷與判斷相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
String類(lèi)下compareTo()與compare()方法比較
這篇文章主要介紹了String類(lèi)下compareTo()與compare()方法比較的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解java為什么不允許類(lèi)多重繼承卻允許接口多重繼承
這篇文章主要介紹了java為什么不允許類(lèi)多重繼承卻允許接口多重繼承,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
簡(jiǎn)單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
這篇文章主要介紹了簡(jiǎn)單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

