用python写一个找好瓜的朴素贝叶斯代码
时间: 2024-04-30 17:19:23 浏览: 107
好的,以下是一个简单的朴素贝叶斯分类器的代码,用于判断西瓜是否好瓜。
```python
import pandas as pd
import numpy as np
class NaiveBayes:
def __init__(self):
self.prior = {}
self.conditional_probabilities = {}
self.labels = []
def fit(self, X_train, y_train):
# 计算先验概率
n_samples = len(y_train)
for label in np.unique(y_train):
self.prior[label] = len(y_train[y_train == label]) / n_samples
# 计算条件概率
self.labels = np.unique(y_train)
for feature_idx in range(X_train.shape[1]):
for label in self.labels:
X_train_c = X_train[y_train == label]
self.conditional_probabilities[(feature_idx, label)] = {}
for feature_val in np.unique(X_train[:, feature_idx]):
self.conditional_probabilities[(feature_idx, label)][feature_val] = \
len(X_train_c[X_train_c[:, feature_idx] == feature_val]) / len(X_train_c)
def predict(self, X_test):
y_pred = []
for sample in X_test:
probabilities = {}
for label in self.labels:
probabilities[label] = self.prior[label]
for feature_idx, feature_val in enumerate(sample):
if (feature_idx, label) in self.conditional_probabilities:
probabilities[label] *= self.conditional_probabilities[(feature_idx, label)][feature_val]
y_pred.append(max(probabilities, key=probabilities.get))
return y_pred
```
这个朴素贝叶斯分类器的实现假设特征之间互相独立,因此它被称为朴素贝叶斯分类器。在这个例子中,我们使用了一个具有两个特征的西瓜数据集。您可以根据需要更改数据集和特征数量。
阅读全文