朴素贝叶斯分类器python
时间: 2023-09-13 17:07:45 浏览: 55
下面是一个简单的朴素贝叶斯分类器的python实现:
```python
import numpy as np
class NaiveBayes:
def fit(self, X, y):
self.classes = np.unique(y)
n_classes = len(self.classes)
n_features = X.shape[1]
self.mean = np.zeros((n_classes, n_features))
self.var = np.zeros((n_classes, n_features))
self.prior = np.zeros(n_classes)
for i, c in enumerate(self.classes):
X_c = X[y == c]
self.mean[i, :] = X_c.mean(axis=0)
self.var[i, :] = X_c.var(axis=0)
self.prior[i] = X_c.shape[0] / X.shape[0]
def predict(self, X):
posteriors = []
for i, c in enumerate(self.classes):
prior = np.log(self.prior[i])
likelihood = np.sum(np.log(self.pdf(X, self.mean[i, :], self.var[i, :])), axis=1)
posterior = prior + likelihood
posteriors.append(posterior)
return self.classes[np.argmax(posteriors, axis=0)]
def pdf(self, X, mean, var):
eps = 1e-4
numerator = np.exp(-0.5 * ((X - mean) ** 2) / (var + eps))
denominator = np.sqrt(2 * np.pi * (var + eps))
return numerator / denominator
```
这个实现包括两个方法:`fit`和`predict`。`fit`方法用于拟合训练数据,计算每个类别的均值和方差,并计算每个类别的先验概率。`predict`方法用于预测新数据的类别,计算每个类别的后验概率,然后选择具有最大后验概率的类别作为预测结果。
在`pdf`方法中,我们使用了一个小常数`eps`来避免分母为零的情况。这个常数的值可以根据具体应用进行调整。
阅读全文