数据挖掘贝叶斯_第1页
数据挖掘贝叶斯_第2页
数据挖掘贝叶斯_第3页
数据挖掘贝叶斯_第4页
数据挖掘贝叶斯_第5页
免费预览已结束,剩余11页可下载查看

下载本文档

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

文档简介

1、数据挖掘贝叶斯 数据挖掘贝叶斯(Bayes)算法java实现 注:本算法的实现仅仅适用于小规模数据集的试验与测试,不适合用于工程应用 算法假定训练数据各属性列的值均是离散类型的。若是非离散类型的数据,需要首先进行数据的预处理,将非离散型的数据离散化。 算法中用法到了DecimalCaculate类,该类是java中BigDecimal类的扩展,用于高精度浮点数的运算。该类的实现同本人转载的一篇博文:对BigDecimal常用方法的归类中的Arith类相同。 算法实现的代码如下 view plaincopy to clipboardprint? 1. package Bayes; 2. impo

2、rt java.util.ArrayList; 3. import java.util.HashMap; 4. import java.util.Map; 5. import util.DecimalCalculate; 6. /* 7. * 贝叶斯主体类 8. * author Rowen 9. * 443773264 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. * mail luowen3405 * blog * data 2021.03.15 */ public class Bayes /* * 将原训练元组按类

3、别划分 * param datas 训练元组 * return Map类别,属于该类别的训练元组 */ MapString, ArrayListArrayListString datasOfClass(ArrayListArrayListString datas) MapString, ArrayListArrayListString map = new HashMapString, ArrayListArrayListString(); ArrayListString t = null; String c = ; for (int i = 0; i datas.size(); i+) 25.

4、 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. t = datas.get(i); c = t.get(t.size() - 1); if (map.containsKey(c) map.get(c).add(t); else ArrayListArrayListString nt = new Arr

5、ayListArrayListString(); nt.add(t); map.put(c, nt); return map; /* * 在训练数据的基础上预报测试元组的类别 * param datas 训练元组 * param testT 测试元组 * return 测试元组的类别 */ public String predictClass(ArrayListArrayListString datas, ArrayListString testT) MapString, ArrayListArrayListString doc = this.datasOfClass(datas); Obje

6、ct classes = doc.keySet().toArray(); double maxP = 0.00; int maxPIndex = -1; for (int i = 0; i doc.size(); i+) String c = classesi.toString(); ArrayListArrayListString d = doc.get(c); double pOfC = DecimalCalculate.div(d.size(), datas.size(), 3); for (int j = 0; j testT.size(); j+) double pv = this.

7、pOfV(d, testT.get(j), j); pOfC = DecimalCalculate.mul(pOfC, pv); if(pOfC maxP) maxP = pOfC; maxPIndex = i; return classesmaxPIndex.toString(); /* * 计算指定属性列上指定值消失的概率 * param d 属于某一类的训练元组 * param value 列值 * param index 属性列索引 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. * return 概率 *

8、/ private double pOfV(ArrayListArrayListString d, String value, int index) double p = 0.00; int count = 0; int total = d.size(); ArrayListString t = null; for (int i = 0; i total; i+) if(d.get(i).get(index).equals(value) count+; p = DecimalCalculate.div(count, total, 3); return p; 1. package Bayes;

9、2. import java.io.BufferedReader; 3. import java.io.IOException; 4. import java.io.InputStreamReader; 5. import java.util.ArrayList; 6. import java.util.StringTokenizer; 7. /* 8. * 贝叶斯算法测试类 9. * author Rowen 10. * 443773264 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

10、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. * blog * data 2021.03.15 */ public class TestBayes /* * 读取测试元组 * return 一条测试元组 * throws IOException */ public ArrayListString readTestData() throws IOException ArrayListString candAttr = new ArrayList

11、String(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in); String str = ; while (!(str = reader.readLine().equals() StringTokenizer tokenizer = new StringTokenizer(str); while (tokenizer.hasMoreTokens() candAttr.add(tokenizer.nextToken(); return candAttr; /* * 读取训练元组 * re

12、turn 训练元组集合 * throws IOException */ public ArrayListArrayListString readData() throws IOException ArrayListArrayListString datas = new ArrayListArrayListString(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in); String str = ; while (!(str = reader.readLine().equals() Str

13、ingTokenizer tokenizer = new StringTokenizer(str); ArrayListString s = new ArrayListString(); while (tokenizer.hasMoreTokens() s.add(tokenizer.nextToken(); datas.add(s); return datas; public static void main(String args) TestBayes tb = new TestBayes(); 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68.

14、 69. 70. 71. 72. ArrayListString testT = null; Bayes bayes = new Bayes(); try System.out.println(请输入训练数据); datas = tb.readData(); while (true) System.out.println(请输入测试元组); testT = tb.readTestData(); String c = bayes.predictClass(datas, testT); System.out.println(The class is: + c); catch (IOExceptio

15、n e) e.printStackTrace(); 1. youth high no fair no 2. youth high no excellent no 3. middle_aged high no fair yes 4. senior medium no fair yes 5. senior low yes fair yes 6. senior low yes excellent no 7. middle_aged low yes excellent yes 8. youth medium no fair no 9. youth low yes fair yes 11. 12. 13

16、. 14. youth medium yes excellent yes middle_aged medium no excellent yes middle_aged high yes fair yes senior medium no excellent no 1. 请输入测试元组 2. youth high no fair 3. The class is: no 4. 请输入测试元组 5. youth high no excellent 6. The class is: no 7. 请输入测试元组 8. middle_aged high no fair 9. The class is:

17、yes 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 请输入测试元组 senior medium no fair The class is: yes 请输入测试元组 senior low yes fair The class is: yes 请输入测试元组 senior low yes excellent The class is: yes 请输入测试元组 middle_aged low yes excellent The class is: yes 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 请输入测试元组 youth medium no fair The class is: no 请输入测试元组 youth low yes fair The class is: yes 请输入测试元组 senior medium yes fair The class is: yes 请输入测试元组 youth medium ye

温馨提示

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

评论

0/150

提交评论