python手写代码实现朴素贝叶斯算法
时间: 2023-08-05 07:09:46 浏览: 105
好的,下面是 Python 实现朴素贝叶斯算法的代码:
```python
import numpy as np
class NaiveBayes:
def __init__(self):
self.classes = None
self.class_probabilities = None
self.mean = None
self.variance = None
def fit(self, X, y):
# 获取不同的类别
self.classes = np.unique(y)
n_classes = len(self.classes)
# 计算每个类别的先验概率
self.class_probabilities = np.zeros(n_classes)
for i in range(n_classes):
self.class_probabilities[i] = np.sum(y == self.classes[i]) / len(y)
# 计算每个特征在每个类别下的均值和方差
n_features = X.shape[1]
self.mean = np.zeros((n_classes, n_features))
self.variance = np.zeros((n_classes, n_features))
for i in range(n_classes):
X_i = X[y == self.classes[i]]
self.mean[i, :] = X_i.mean(axis=0)
self.variance[i, :] = X_i.var(axis=0)
def predict(self, X):
# 计算每个类别下的后验概率
posteriors = np.zeros((len(X), len(self.classes)))
for i in range(len(self.classes)):
class_prior = np.log(self.class_probabilities[i])
class_mean = self.mean[i]
class_variance = self.variance[i]
numerator = np.exp(-((X - class_mean) ** 2) / (2 * class_variance))
denominator = np.sqrt(2 * np.pi * class_variance)
posterior = class_prior + np.sum(np.log(numerator / denominator), axis=1)
posteriors[:, i] = posterior
# 返回概率最大的类别
return self.classes[np.argmax(posteriors, axis=1)]
```
其中 `fit()` 方法用于训练模型,`predict()` 方法用于预测新数据的类别。在 `fit()` 方法中,我们首先计算了每个类别的先验概率,然后计算了每个特征在每个类别下的均值和方差。在 `predict()` 方法中,我们计算了每个类别下的后验概率,并返回概率最大的类别。
阅读全文