贝叶斯算法python代码 不要用scikit-learn
时间: 2024-10-29 11:06:34 浏览: 19
贝叶斯定理是一种统计推断方法,它在机器学习中常用于概率分类问题。在Python中,我们可以直接编写朴素贝叶斯算法的代码,而不需要依赖Scikit-learn库。以下是基于特征向量和概率计算的简单朴素贝叶斯分类器示例:
```python
import numpy as np
class NaiveBayesClassifier:
def __init__(self):
self.classes = None
self.class_probs = {}
self.feature_probs = {}
def fit(self, X, y):
self.classes = np.unique(y)
for class_ in self.classes:
# 计算每个类别的样本数
count_class = (y == class_).sum()
# 计算每类的概率 P(class)
self.class_probs[class_] = count_class / len(y)
# 对于每一特征,计算属于该类别的条件下该特征出现的概率 P(feature|class)
feature_counts = np.zeros(X.shape[1])
for i in range(count_class):
index = np.where(y[i] == class_)[0][0]
feature_counts += X[index]
self.feature_probs[class_] = feature_counts / count_class
def predict(self, X):
predictions = []
for x in X:
posterior_class_probs = {
class_: self.class_probs[class_] * np.prod(
[self.feature_probs[class_][feature] for feature in x if feature != 0]
)
for class_ in self.classes
}
predictions.append(max(posterior_class_probs, key=posterior_class_probs.get))
return predictions
# 使用示例
X = ... # 特征数据
y = ... # 标签数据
clf = NaiveBayesClassifier()
clf.fit(X, y)
predictions = clf.predict(X)
```
阅读全文