编程实现贝叶斯分类算法,并对简单应用样本数据实现预测分类,添加准确度度计算函数,编写出java代码
时间: 2023-06-02 15:04:43 浏览: 54
以下是Java代码实现贝叶斯分类算法并进行准确度计算:
```java
import java.util.HashMap;
import java.util.Map;
public class NaiveBayesClassifier {
private Map<String, Integer> classCountMap;
private Map<String, Map<String, Integer>> wordCountMap;
public NaiveBayesClassifier() {
classCountMap = new HashMap<>();
wordCountMap = new HashMap<>();
}
public void train(String document, String label) {
String[] words = document.split(" ");
if (!classCountMap.containsKey(label)) {
classCountMap.put(label, 0);
wordCountMap.put(label, new HashMap<>());
}
classCountMap.put(label, classCountMap.get(label) + 1);
for (String word : words) {
if (!wordCountMap.get(label).containsKey(word)) {
wordCountMap.get(label).put(word, 0);
}
wordCountMap.get(label).put(word, wordCountMap.get(label).get(word) + 1);
}
}
public String predict(String document) {
String[] words = document.split(" ");
String maxLabel = null;
double maxProb = Double.NEGATIVE_INFINITY;
for (String label : classCountMap.keySet()) {
double prob = Math.log((double) classCountMap.get(label) / getTotalClassCount());
for (String word : words) {
int count = wordCountMap.get(label).containsKey(word) ? wordCountMap.get(label).get(word) : 0;
prob += Math.log((double) (count + 1) / (getTotalWordCount(label) + getVocabularySize()));
}
if (prob > maxProb) {
maxProb = prob;
maxLabel = label;
}
}
return maxLabel;
}
public double getAccuracy(String[] documents, String[] labels) {
if (documents.length != labels.length) {
throw new IllegalArgumentException("The number of documents must match the number of labels");
}
int correctCount = 0;
for (int i = 0; i < documents.length; i++) {
String prediction = predict(documents[i]);
if (prediction.equals(labels[i])) {
correctCount++;
}
}
return (double) correctCount / documents.length;
}
private int getTotalClassCount() {
int count = 0;
for (int c : classCountMap.values()) {
count += c;
}
return count;
}
private int getTotalWordCount(String label) {
int count = 0;
for (int c : wordCountMap.get(label).values()) {
count += c;
}
return count;
}
private int getVocabularySize() {
int size = 0;
for (Map<String, Integer> wordCount : wordCountMap.values()) {
size += wordCount.size();
}
return size;
}
public static void main(String[] args) {
NaiveBayesClassifier classifier = new NaiveBayesClassifier();
classifier.train("Chinese Beijing Chinese", "China");
classifier.train("Chinese Chinese Shanghai", "China");
classifier.train("Chinese Macao", "China");
classifier.train("Tokyo Japan Chinese", "Japan");
String[] documents = { "Chinese Chinese Chinese Tokyo Japan", "Chinese Tokyo Japan" };
String[] labels = { "China", "Japan" };
double accuracy = classifier.getAccuracy(documents, labels);
System.out.println("Accuracy: " + accuracy);
}
}
```
以上代码中的训练样本包括4个文档,标签分别为“China”和“Japan”。在`main`函数中,我们使用两个新的文档来测试分类器的准确度。具体而言,我们先训练分类器,然后对新的文档进行分类并计算分类器的准确度。在上面的代码中,我们使用 Laplace 平滑技术来处理不出现在训练样本中的词。对于每个文档,我们首先计算每个标签的概率,并使用对数可能性加法避免数值下溢。接着,我们计算每个单词在每个标签类别中的概率,并将它们相乘。最后,我们选择具有最大概率的标签作为预测。注意,在预测过程中,我们使用了相同的 Laplace 平滑技术来避免未知单词的影响。