实现贝叶斯分类算法,对简单应用样本数据实现预测分类,并计算条件概率,预测精度的计算与评估,写出java代码
时间: 2023-06-01 14:06:37 浏览: 121
好的,以下是实现贝叶斯分类算法的Java代码示例:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class NaiveBayesClassifier {
private Map<String, Map<String, Integer>> frequencyTable;
private Map<String, Integer> classCountTable;
private Map<String, Double> classProbabilityTable;
private int totalDocCount;
public NaiveBayesClassifier() {
frequencyTable = new HashMap<>();
classCountTable = new HashMap<>();
classProbabilityTable = new HashMap<>();
totalDocCount = 0;
}
public void train(String[] documents, String[] labels) {
for (int i = 0; i < documents.length; i++) {
String document = documents[i];
String label = labels[i];
// 更新类别计数
classCountTable.put(label, classCountTable.getOrDefault(label, 0) + 1);
// 更新文档总数
totalDocCount++;
// 统计单词词频
String[] words = document.split(" ");
for (String word : words) {
Map<String, Integer> classFrequencyTable = frequencyTable.getOrDefault(word, new HashMap<>());
classFrequencyTable.put(label, classFrequencyTable.getOrDefault(label, 0) + 1);
frequencyTable.put(word, classFrequencyTable);
}
}
// 计算类别概率
for (String label : classCountTable.keySet()) {
int count = classCountTable.get(label);
double probability = (double) count / totalDocCount;
classProbabilityTable.put(label, probability);
}
}
public String classify(String document) {
String[] words = document.split(" ");
Map<String, Double> classScoreTable = new HashMap<>();
for (String label : classCountTable.keySet()) {
double score = classProbabilityTable.get(label);
for (String word : words) {
Map<String, Integer> classFrequencyTable = frequencyTable.getOrDefault(word, new HashMap<>());
double wordProb = (double) classFrequencyTable.getOrDefault(label, 0) / classCountTable.get(label);
score *= wordProb;
}
classScoreTable.put(label, score);
}
// 找到分数最大的类别
String maxClass = null;
double maxScore = Double.NEGATIVE_INFINITY;
for (String label : classScoreTable.keySet()) {
double score = classScoreTable.get(label);
if (score > maxScore) {
maxScore = score;
maxClass = label;
}
}
return maxClass;
}
public double evaluate(String[] documents, String[] labels) {
int correctCount = 0;
for (int i = 0; i < documents.length; i++) {
String document = documents[i];
String label = labels[i];
String predictedLabel = classify(document);
if (label.equals(predictedLabel)) {
correctCount++;
}
}
return (double) correctCount / documents.length;
}
public static void main(String[] args) {
String[] documents = {
"Chinese Beijing Chinese",
"Chinese Chinese Shanghai",
"Chinese Macao",
"Tokyo Japan Chinese"
};
String[] labels = {
"China",
"China",
"China",
"Japan"
};
NaiveBayesClassifier classifier = new NaiveBayesClassifier();
classifier.train(documents, labels);
String document = "Chinese Chinese Chinese Tokyo Japan";
String predictedLabel = classifier.classify(document);
System.out.println("Predicted label: " + predictedLabel);
double accuracy = classifier.evaluate(documents, labels);
System.out.println("Accuracy: " + accuracy);
}
}
该代码实现了贝叶斯分类算法,并对简单应用样本数据进行了预测分类,并计算了条件概率,预测精度的计算与评估。但请注意,该示例代码并不是针对特定应用场景的,需要根据使用场景进行相应的修改和优化。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)