이 기사에서는 Java로 구현 된 순진한 Bayes 알고리즘에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
데이터 마이닝 및 추천 시스템을 수행하는 젊은이들은 순진한 Bayes 알고리즘에 익숙하다고 생각하므로 알고리즘 원칙에 대해서는 이야기하지 않을 것입니다. 주로 Java 코드를 통해 순진한 Bayes 알고리즘을 구현하고 싶습니다. 아이디어는 다음과 같습니다.
1. 데이터 스토리지 교육을 위해 Javabean +Arraylist를 사용하십시오
2. 샘플 데이터 훈련의 경우
특정 코드는 다음과 같습니다.
패키지 nb;/** * 훈련 샘플의 속성 javabean * */public class javabean {int age; 문자열 소득; 문자열 학생; 문자열 Credit_rating; 문자열 buys_computer; public javabean () {} public javabean (int 연령, 문자열 소득, 문자열 학생, 문자열 credit_rating, string buys_computer) {this.age = age; this. income = 소득; this.student = 학생; this.credit_rating = credit_rating; this.buys_computer = buys_computer;} public int getage () {return age;} public void setage (int age) {this.ge.age = age = age = age = age = age = age} public void setomcome (} public void setomome (} public void setomcome (문자열 소득) {void void setstud (} public student getStudent) {} 이. this.buys_computer = buys_computer;}@atriadepublic string toString () {return "javabean [age =" + age + ", 소득 =" + 소득 + ", student =" + student + ", credit_rating =" + credit_rating + ", buys_computer =" + buys_computer + ";알고리즘 구현 부분 :
패키지 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. 데이터를 읽고 목록 컨테이너 파일에 넣습니다. 파일 = 새 파일 ( "e : //test.txt"); txt2string (파일); // 데이터 테스트 샘플 testData (25, "medium", "yes", "fair"); } // 샘플 데이터 읽기 public static void txt2string (file file) {try {bufferedReader br = new bufferedReader (new FilEreader (file)); // 파일을 읽기 위해 BufferedReader 클래스를 작성하여 s = null; while ((s = br.readline ())! = null) {// readline 메소드를 사용하여 시간 데이터 _length ++; 분할 (들); } br.close (); } catch (예외 e) {e.printstacktrace (); }} // arraylist에 저장 공용 정적 무효 분할 (String str) {String strr = str.trim (); 문자열 [] 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); } // 훈련 샘플, 테스트 공개 정적 무효 TestData (int Age, String A, String B, String C) {// 샘플 int Number_Yes = 0; int buct_no = 0; // 연령 수의 수 int num_age_yes = 0; int num_age_no = 0; // 소득 INT NUM_INCOME_YES = 0; int num_income_no = 0; // 학생 int num_student_yes = 0; int num_stdent_no = 0; // 신용 int num_credit_yes = 0; int num_credit_no = 0; // (int i = 0; i <list.size (); i ++) {javabean bb = list.get (i); if (bb.getBuys_computer (). equals ( "yes")) {// 예 번호 _yes ++; if (bb.getOncome (). Equals (a)) {// 소득 Num_Income_yes ++; } if (bb.getStudent (). Equals (b)) {// Student num_student_yes ++; } if (bb.getCredit_rating (). equals (c)) {// redure num_credit_yes ++; } if (bb.getage () == age) {// age num_age_yes ++; }} else {// bumber_no ++ 없음; if (bb.getOncome (). Equals (a)) {// 소득 Num_Income_no ++; } if (bb.getStudent (). Equals (b)) {// Student num_stdent_no ++; } if (bb.getCredit_rating (). equals (c)) {// redure num_credit_no ++; } if (bb.getage () == age) {// age num_age_no ++; }}}} system.out.println ( "구매 수 :"+number_yes); System.out.println ( "구매되지 않은 기록 수 :"+bumber_no); System.out.println ( "Buy+age :"+num_age_yes); System.out.println ( "구매+소득 :"+num_age_no); System.out.println ( "구매+소득 :"+num_income_yes); System.out.println ( "구매+소득 :"+num_income_yes); System.out.println ( "Buy+Stundent :"+num_student_yes); System.out.println ( "구매+학생 :"+num_stdent_no); System.out.println ( "Buy+Stundent :"+num_stdent_no); System.out.println ( "구매+크레딧 :"+num_credit_yes); System.out.println ( "구매+크레딧 :"+num_credit_no); //// 확률 판단 Double Buy_yes = number_yes*1.0/data_length; // 확률 구매 이중 구매 _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)*(1.0*num_credit_yes/number_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 높은 공정 번호
25 High No No 탁월한 아니요
33 높은 공정한 예
41 중간 공정한 예
41 LOW YES FAIR 예
41 낮은 예, 우수한 아니요
33 낮은 예 탁월한 예
25 매체 없음 공정 번호
25 LOW YES FAIR 예
41 Medium Yes Fair Yes
25 중간 예, 우수한 예
33 중간은 훌륭한 예입니다
33 높은 예 공정 예
41 매체가 우수한 아니오
알 수없는 사용자 데이터 결과 :
구매 수 : 9
구매하지 않은 역사 없음 : 5
구매 +연령 : 2
구매하지 마십시오 +나이 : 3
구매 +소득 : 4
구매하지 마십시오 +소득 : 2
구매 +스 텐트 : 6
구매하지 마십시오 +학생 : 1
구매 + 신용 : 6
구매하지 마십시오 + 신용 : 2
교육 데이터 구매 확률 : 0.6428571428571429
교육 데이터에서 구매할 확률 : 0.35714285714285715
신규 사용자 구매 확률 : 0.028218694885361547
신규 사용자가 구매하지 않는 확률 : 0.006857142857142858
신규 사용자는 구매 가능성이 높습니다
Java 알고리즘에 대한 자세한 내용은이 사이트에 관심이있는 독자들이 주제를 볼 수 있습니다. "Java 데이터 구조 및 알고리즘 자습서", "Java Operation Dom Node Tips 요약", "Java 파일 및 디렉토리 작동 팁 요약"및 "Java Cache Operation Tips의 요약"을 볼 수 있습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.