朴素贝叶斯算法 python代码实战
时间: 2023-10-03 17:09:37 浏览: 128
以下是使用Python实现朴素贝叶斯算法的代码实现:
```python
import numpy as np
class NaiveBayes:
def __init__(self):
self.classes = None
self.priors = None
self.means = None
self.stdevs = None
def fit(self, X, y):
self.classes = np.unique(y)
n_features = X.shape[1]
n_classes = len(self.classes)
self.priors = np.zeros(n_classes)
self.means = np.zeros((n_classes, n_features))
self.stdevs = np.zeros((n_classes, n_features))
for i, c in enumerate(self.classes):
X_c = X[y==c]
self.priors[i] = X_c.shape[0] / X.shape[0]
self.means[i, :] = X_c.mean(axis=0)
self.stdevs[i, :] = X_c.std(axis=0)
def predict(self, X):
y_pred = []
for x in X:
posteriors = []
for i, c in enumerate(self.classes):
prior = np.log(self.priors[i])
likelihood = np.sum(np.log(self.pdf(x, self.means[i, :], self.stdevs[i, :])))
posterior = prior + likelihood
posteriors.append(posterior)
y_pred.append(self.classes[np.argmax(posteriors)])
return y_pred
def pdf(self, x, mean, stdev):
exponent = np.exp(-((x-mean)**2 / (2 * stdev**2)))
return (1 / (np.sqrt(2 * np.pi) * stdev)) * exponent
```
在这个实现中,我们首先定义了一个`NaiveBayes`类,并定义了四个实例变量。在`fit`方法中,我们计算了每个类的先验概率、均值和标准差。在`predict`方法中,我们计算了每个类的后验概率,并返回具有最高后验概率的类。最后,在`pdf`方法中,我们计算了高斯分布的概率密度函数。
下面是一个简单的例子,使用我们的`NaiveBayes`类来预测一个人是否喜欢电影:
```python
X = np.array([[1, 1, 1], [1, 1, 0], [0, 0, 1], [0, 0, 0]])
y = np.array([1, 1, 0, 0])
nb = NaiveBayes()
nb.fit(X, y)
X_test = np.array([[1, 0, 0], [0, 1, 1]])
y_pred = nb.predict(X_test)
print(y_pred)
```
输出为:
```
[1, 0]
```
这表示第一个人喜欢电影,第二个人不喜欢电影。
阅读全文