代码实现朴素贝叶斯分类
时间: 2023-11-30 13:02:14 浏览: 61
以下是Python实现的朴素贝叶斯分类器的示例代码:
```python
import math
class NaiveBayes:
def __init__(self):
self.classes = None
self.class_probabilities = None
self.conditional_probabilities = None
def fit(self, X, y):
self.classes = set(y)
self.class_probabilities = {}
self.conditional_probabilities = {}
# 计算每个类别的概率
for c in self.classes:
self.class_probabilities[c] = sum(1 for i in y if i == c) / len(y)
# 计算每个属性在每个类别下的条件概率
for i in range(len(X[0])):
for c in self.classes:
key = (i, c)
self.conditional_probabilities[key] = {}
for xi in set([x[i] for x, yi in zip(X, y) if yi == c]):
self.conditional_probabilities[key][xi] = sum(1 for x, yi in zip(X, y) if x[i] == xi and yi == c) / sum(1 for yi in y if yi == c)
def predict(self, X):
y_pred = []
for x in X:
probabilities = {}
for c in self.classes:
probabilities[c] = math.log(self.class_probabilities[c])
for i, xi in enumerate(x):
if (i, c) in self.conditional_probabilities:
if xi in self.conditional_probabilities[(i, c)]:
probabilities[c] += math.log(self.conditional_probabilities[(i, c)][xi])
else:
probabilities[c] += math.log(1e-10)
y_pred.append(max(probabilities, key=probabilities.get))
return y_pred
```
这个朴素贝叶斯分类器实现了`fit`和`predict`两个方法。`fit`方法用于训练模型,接受训练数据`X`和标签`y`作为输入。`predict`方法用于预测新的数据,接受测试数据`X`作为输入,返回预测结果。
阅读全文