编写python程序实现朴素贝叶斯算法
时间: 2023-07-09 22:44:54 浏览: 107
好的,以下是实现朴素贝叶斯算法的Python代码:
```python
import math
class NaiveBayesClassifier:
def __init__(self):
self.classes = {}
self.class_probabilities = {}
def fit(self, X, y):
# 计算每个类别的出现次数
for i in range(len(y)):
if y[i] not in self.classes:
self.classes[y[i]] = []
self.classes[y[i]].append(X[i])
# 计算每个类别的概率
total_samples = len(y)
for class_name in self.classes:
class_samples = len(self.classes[class_name])
self.class_probabilities[class_name] = class_samples / total_samples
def predict(self, X):
predictions = []
for x in X:
max_probability = -1
most_likely_class = None
# 对于每个类别,计算概率并找出概率最大的类别
for class_name in self.classes:
class_probability = self.class_probabilities[class_name]
likelihood = 1.0
for i in range(len(x)):
feature = x[i]
feature_occurrences = self.classes[class_name].count(feature)
feature_probability = feature_occurrences / len(self.classes[class_name])
likelihood *= feature_probability
probability = class_probability * likelihood
if probability > max_probability:
max_probability = probability
most_likely_class = class_name
predictions.append(most_likely_class)
return predictions
```
示例用法:
```python
X_train = [[1, 'S'], [1, 'M'], [1, 'M'], [1, 'S'], [1, 'S'], [2, 'S'], [2, 'M'], [2, 'M'], [2, 'L'], [2, 'L'], [3, 'L'], [3, 'M'], [3, 'M'], [3, 'L'], [3, 'L']]
y_train = ['-', '-', '+', '+', '-', '-', '-', '+', '+', '+', '+', '+', '+', '+', '-']
X_test = [[2, 'S'], [1, 'S']]
y_test = ['-', '-']
# 初始化并训练分类器
clf = NaiveBayesClassifier()
clf.fit(X_train, y_train)
# 使用分类器进行预测
predictions = clf.predict(X_test)
# 输出预测结果
print(predictions) # ['-', '-']
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)