设计并实现基于Python的朴素贝叶斯算法
时间: 2023-12-29 09:05:20 浏览: 77
以下是基于Python的朴素贝叶斯算法的实现:
```python
import math
class NaiveBayes:
def __init__(self):
self.classes = None
self.class_probabilities = None
self.word_counts = None
self.word_probabilities = None
def fit(self, X, y):
self.classes = set(y)
self.class_probabilities = {}
self.word_counts = {}
self.word_probabilities = {}
# 计算每个类别的概率
for c in self.classes:
self.class_probabilities[c] = y.count(c) / len(y)
# 计算每个单词在每个类别中出现的次数
for i in range(len(X)):
for word in X[i].split():
if word not in self.word_counts:
self.word_counts[word] = {}
for c in self.classes:
self.word_counts[word][c] = 0
self.word_counts[word][y[i]] += 1
# 计算每个单词在每个类别中出现的概率
for word in self.word_counts:
self.word_probabilities[word] = {}
for c in self.classes:
self.word_probabilities[word][c] = (self.word_counts[word][c] + 1) / (sum(self.word_counts[word].values()) + len(self.word_counts))
def predict(self, X):
result = []
for x in X:
probabilities = {}
for c in self.classes:
probabilities[c] = math.log(self.class_probabilities[c])
for word in x.split():
if word in self.word_probabilities:
probabilities[c] += math.log(self.word_probabilities[word][c])
result.append(max(probabilities, key=probabilities.get))
return result
```
上述代码实现了一个朴素贝叶斯分类器,其中`fit`方法用于训练模型,`predict`方法用于预测新的数据。
阅读全文