Repository navigation
Expand file tree
/
Copy pathNaiveBayesTool.java
More file actions
209 lines (180 loc) · 4.63 KB
/
Copy pathNaiveBayesTool.java
File metadata and controls
209 lines (180 loc) · 4.63 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package DataMining_NaiveBayes;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* 朴素贝叶斯算法工具类
*
* @author lyq
*
*/
public class NaiveBayesTool {
// 类标记符,这里分为2类,YES和NO
private String YES = "Yes";
private String NO = "No";
// 已分类训练数据集文件路径
private String filePath;
// 属性名称数组
private String[] attrNames;
// 训练数据集
private String[][] data;
// 每个属性的值所有类型
private HashMap<String, ArrayList<String>> attrValue;
public NaiveBayesTool(String filePath) {
this.filePath = filePath;
readDataFile();
initAttrValue();
}
/**
* 从文件中读取数据
*/
private void readDataFile() {
File file = new File(filePath);
ArrayList<String[]> dataArray = new ArrayList<String[]>();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
String[] tempArray;
while ((str = in.readLine()) != null) {
tempArray = str.split(" ");
dataArray.add(tempArray);
}
in.close();
} catch (IOException e) {
e.getStackTrace();
}
data = new String[dataArray.size()][];
dataArray.toArray(data);
attrNames = data[0];
/*
* for(int i=0; i<data.length;i++){ for(int j=0; j<data[0].length; j++){
* System.out.print(" " + data[i][j]); }
*
* System.out.print("\n"); }
*/
}
/**
* 首先初始化每种属性的值的所有类型,用于后面的子类熵的计算时用
*/
private void initAttrValue() {
attrValue = new HashMap<>();
ArrayList<String> tempValues;
// 按照列的方式,从左往右找
for (int j = 1; j < attrNames.length; j++) {
// 从一列中的上往下开始寻找值
tempValues = new ArrayList<>();
for (int i = 1; i < data.length; i++) {
if (!tempValues.contains(data[i][j])) {
// 如果这个属性的值没有添加过,则添加
tempValues.add(data[i][j]);
}
}
// 一列属性的值已经遍历完毕,复制到map属性表中
attrValue.put(data[0][j], tempValues);
}
}
/**
* 在classType的情况下,发生condition条件的概率
*
* @param condition
* 属性条件
* @param classType
* 分类的类型
* @return
*/
private double computeConditionProbably(String condition, String classType) {
// 条件计数器
int count = 0;
// 条件属性的索引列
int attrIndex = 1;
// yes类标记符数据
ArrayList<String[]> yClassData = new ArrayList<>();
// no类标记符数据
ArrayList<String[]> nClassData = new ArrayList<>();
ArrayList<String[]> classData;
for (int i = 1; i < data.length; i++) {
// data数据按照yes和no分类
if (data[i][attrNames.length - 1].equals(YES)) {
yClassData.add(data[i]);
} else {
nClassData.add(data[i]);
}
}
if (classType.equals(YES)) {
classData = yClassData;
} else {
classData = nClassData;
}
// 如果没有设置条件则,计算的是纯粹的类事件概率
if (condition == null) {
return 1.0 * classData.size() / (data.length - 1);
}
// 寻找此条件的属性列
attrIndex = getConditionAttrName(condition);
for (String[] s : classData) {
if (s[attrIndex].equals(condition)) {
count++;
}
}
return 1.0 * count / classData.size();
}
/**
* 根据条件值返回条件所属属性的列值
*
* @param condition
* 条件
* @return
*/
private int getConditionAttrName(String condition) {
// 条件所属属性名
String attrName = "";
// 条件所在属性列索引
int attrIndex = 1;
// 临时属性值类型
ArrayList<String[]> valueTypes;
for (Map.Entry entry : attrValue.entrySet()) {
valueTypes = (ArrayList<String[]>) entry.getValue();
if (valueTypes.contains(condition)
&& !((String) entry.getKey()).equals("BuysComputer")) {
attrName = (String) entry.getKey();
}
}
for (int i = 0; i < attrNames.length - 1; i++) {
if (attrNames[i].equals(attrName)) {
attrIndex = i;
break;
}
}
return attrIndex;
}
/**
* 进行朴素贝叶斯分类
*
* @param data
* 待分类数据
*/
public String naiveBayesClassificate(String data) {
// 测试数据的属性值特征
String[] dataFeatures;
// 在yes的条件下,x事件发生的概率
double xWhenYes = 1.0;
// 在no的条件下,x事件发生的概率
double xWhenNo = 1.0;
// 最后也是yes和no分类的总概率,用P(X|Ci)*P(Ci)的公式计算
double pYes = 1;
double pNo = 1;
dataFeatures = data.split(" ");
for (int i = 0; i < dataFeatures.length; i++) {
// 因为朴素贝叶斯算法是类条件独立的,所以可以进行累积的计算
xWhenYes *= computeConditionProbably(dataFeatures[i], YES);
xWhenNo *= computeConditionProbably(dataFeatures[i], NO);
}
pYes = xWhenYes * computeConditionProbably(null, YES);
pNo = xWhenNo * computeConditionProbably(null, NO);
return (pYes > pNo ? YES : NO);
}
}