使用python编写一个贝叶斯分类中的朴素贝叶斯分类算法
时间: 2024-05-01 17:21:22 浏览: 102
好的,以下是基于Python的朴素贝叶斯分类算法实现:
```python
import math
class NaiveBayes:
def __init__(self):
self.classes = None
self.class_priors = None
self.conditional_probs = None
def fit(self, X, y):
self.classes = list(set(y))
self.class_priors = {c: 0 for c in self.classes}
self.conditional_probs = {}
# 计算类别先验概率
for c in y:
self.class_priors[c] += 1
for c in self.classes:
self.class_priors[c] /= len(y)
# 计算条件概率
for c in self.classes:
self.conditional_probs[c] = {}
for i in range(len(X[0])):
self.conditional_probs[c][i] = {}
for xi in set([x[i] for x in X]):
self.conditional_probs[c][i][xi] = 0
for xi in set([x[i] for x in X]):
xi_count = 0
for j in range(len(X)):
if y[j] == c and X[j][i] == xi:
xi_count += 1
for i in range(len(X[0])):
self.conditional_probs[c][i][xi] = xi_count / self.class_priors[c] / len(X)
def predict(self, X):
y_pred = []
for x in X:
prob_max = -1
y_pred_i = -1
for c in self.classes:
prob = math.log(self.class_priors[c])
for i in range(len(x)):
prob += math.log(self.conditional_probs[c][i][x[i]])
if prob > prob_max:
prob_max = prob
y_pred_i = c
y_pred.append(y_pred_i)
return y_pred
```
其中,`fit()` 和 `predict()` 分别是训练和预测函数。在 `fit()` 函数中,首先计算了每个类别的先验概率,然后计算了每个特征值在给定类别下的条件概率。在 `predict()` 函数中,根据贝叶斯公式计算待预测样本属于每个类别的后验概率,并选取概率最大的类别作为预测结果。
使用朴素贝叶斯分类器进行分类的步骤如下:
```python
# 假设已有训练集 X_train 和标签 y_train,测试集 X_test
nb = NaiveBayes()
nb.fit(X_train, y_train)
y_pred = nb.predict(X_test)
```
其中,训练集和测试集均为二维数组,每行代表一个样本,每列代表一个特征。标签为一维数组,长度与样本数相同。
阅读全文