数据挖掘贝叶斯.docx_第1页
数据挖掘贝叶斯.docx_第2页
数据挖掘贝叶斯.docx_第3页
数据挖掘贝叶斯.docx_第4页
数据挖掘贝叶斯.docx_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

数据挖掘贝叶斯(Bayes)算法java实现 注:本算法的实现仅仅适用于小规模数据集的实验与测试,不适合用于工程应用算法假定训练数据各属性列的值均是离散类型的。若是非离散类型的数据,需要首先进行数据的预处理,将非离散型的数据离散化。算法中使用到了DecimalCaculate类,该类是java中BigDecimal类的扩展,用于高精度浮点数的运算。该类的实现同本人转载的一篇博文:对BigDecimal常用方法的归类中的Arith类相同。算法实现的代码如下view plaincopy to clipboardprint?1. packageBayes;2. importjava.util.ArrayList;3. importjava.util.HashMap;4. importjava.util.Map;5. importutil.DecimalCalculate;6. /*7. *贝叶斯主体类8. *authorRowen9. *qq44377326410. *11. */luowen340512. *data2011.03.1513. */14. publicclassBayes15. /*16. *将原训练元组按类别划分17. *paramdatas训练元组18. *returnMap19. */20. MapString,ArrayListArrayListdatasOfClass(ArrayListArrayListdatas)21. MapString,ArrayListArrayListmap=newHashMapString,ArrayListArrayList();22. ArrayListt=null;23. Stringc=;24. for(inti=0;idatas.size();i+)25. t=datas.get(i);26. c=t.get(t.size()-1);27. if(map.containsKey(c)28. map.get(c).add(t);29. else30. ArrayListArrayListnt=newArrayListArrayList();31. nt.add(t);32. map.put(c,nt);33. 34. 35. returnmap;36. 37. 38. /*39. *在训练数据的基础上预测测试元组的类别40. *paramdatas训练元组41. *paramtestT测试元组42. *return测试元组的类别43. */44. publicStringpredictClass(ArrayListArrayListdatas,ArrayListtestT)45. MapString,ArrayListArrayListdoc=this.datasOfClass(datas);46. Objectclasses=doc.keySet().toArray();47. doublemaxP=0.00;48. intmaxPIndex=-1;49. for(inti=0;idoc.size();i+)50. Stringc=classesi.toString();51. ArrayListArrayListd=doc.get(c);52. doublepOfC=DecimalCalculate.div(d.size(),datas.size(),3);53. for(intj=0;jmaxP)58. maxP=pOfC;59. maxPIndex=i;60. 61. 62. returnclassesmaxPIndex.toString();63. 64. /*65. *计算指定属性列上指定值出现的概率66. *paramd属于某一类的训练元组67. *paramvalue列值68. *paramindex属性列索引69. *return概率70. */71. privatedoublepOfV(ArrayListArrayListd,Stringvalue,intindex)72. doublep=0.00;73. intcount=0;74. inttotal=d.size();75. ArrayListt=null;76. for(inti=0;itotal;i+)77. if(d.get(i).get(index).equals(value)78. count+;79. 80. 81. p=DecimalCalculate.div(count,total,3);82. returnp;83. 84. 算法测试类:view plaincopy to clipboardprint?1. packageBayes;2. importjava.io.BufferedReader;3. importjava.io.IOException;4. importjava.io.InputStreamReader;5. importjava.util.ArrayList;6. importjava.util.StringTokenizer;7. /*8. *贝叶斯算法测试类9. *authorRowen10. *qq44377326411. *12. */luowen340513. *data2011.03.1514. */15. publicclassTestBayes16. /*17. *读取测试元组18. *return一条测试元组19. *throwsIOException20. */21. publicArrayListreadTestData()throwsIOException22. ArrayListcandAttr=newArrayList();23. BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in);24. Stringstr=;25. while(!(str=reader.readLine().equals()26. StringTokenizertokenizer=newStringTokenizer(str);27. while(tokenizer.hasMoreTokens()28. candAttr.add(tokenizer.nextToken();29. 30. 31. returncandAttr;32. 33. 34. /*35. *读取训练元组36. *return训练元组集合37. *throwsIOException38. */39. publicArrayListArrayListreadData()throwsIOException40. ArrayListArrayListdatas=newArrayListArrayList();41. BufferedReaderreader=newBufferedReader(newInputStreamReader(System.in);42. Stringstr=;43. while(!(str=reader.readLine().equals()44. StringTokenizertokenizer=newStringTokenizer(str);45. ArrayLists=newArrayList();46. while(tokenizer.hasMoreTokens()47. s.add(tokenizer.nextToken();48. 49. datas.add(s);50. 51. returndatas;52. 53. 54. publicstaticvoidmain(Stringargs)55. TestBayestb=newTestBayes();56. ArrayListArrayListdatas=null;57. ArrayListtestT=null;58. Bayesbayes=newBayes();59. try60. System.out.println(请输入训练数据);61. datas=tb.readData();62. while(true)63. System.out.println(请输入测试元组);64. testT=tb.readTestData();65. Stringc=bayes.predictClass(datas,testT);66. System.out.println(Theclassis:+c);67. 68. catch(IOExceptione)69. e.printStackTrace();70. 71. 72. 训练数据:view plaincopy to clipboardprint?1. youthhighnofairno2. youthhighnoexcellentno3. middle_agedhighnofairyes4. seniormediumnofairyes5. seniorlowyesfairyes6. seniorlowyesexcellentno7. middle_agedlowyesexcellentyes8. youthmediumnofairno9. youthlowyesfairyes10. seniormediumyesfairyes11. youthmediumyesexcellentyes12. middle_agedmediumnoexcellentyes13. middle_agedhighyesfairyes14. seniormediumnoexcellentno对原训练数据进行测试,测试如果如下:view plaincopy to clipboardprint?1. 请输入测试元组2. youthhighnofair3. Theclassis:no4. 请输入测试元组5. youthhighnoexcellent6. Theclassis:no7. 请输入测试元组8. middle_agedhighnofair9. Theclassis:yes10. 请输入测试元组11. seniormediumnofair12. Theclassis:yes13. 请输入测试元组14. seniorlowyesfair15. Theclassis:yes16. 请输入测试元组17. seniorlowyesexcellent18. Theclassis:yes19. 请输入测试元组20. middle_agedlowyesexcellent21. Theclassis:yes22. 请输入测试元组23. youthmediumnofair24. Theclassis:no25. 请输入测试元组26. youthlowyesfair27. Theclassis:yes28. 请输入测试元组29. seniormediumyesfair30. Theclassis:yes31. 请输入测试元组32. youthmediumyesexcellent33. Theclassis:yes34. 请输入测试元组35. middle_agedmediumnoexcellent36. Theclassis:yes37. 请输入测试元组38. middle_agedhighyesfair39. Theclassis:yes40. 请输入测试元组41. seniormediumnoexcellent42. Thecl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论