贝叶斯分类算法算法python
时间: 2024-12-26 17:19:39 浏览: 3
### 贝叶斯分类算法 Python 实现及教程
#### 使用 `sklearn` 库实现朴素贝叶斯分类器
为了快速构建并测试朴素贝叶斯模型,可以借助 `scikit-learn` 这一强大的库。下面展示了一个简单例子,说明如何加载数据集、预处理以及创建和评估一个高斯分布下的朴素贝叶斯分类器。
```python
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
# 加载新闻组数据集作为示例
data = fetch_20newsgroups(subset='all')
X_train_raw, X_test_raw, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3)
# 将文本转化为词频矩阵
vectorizer = CountVectorizer()
X_train_counts = vectorizer.fit_transform(X_train_raw)
X_test_counts = vectorizer.transform(X_test_raw)
# 创建多项式朴素贝叶斯分类器实例
clf = MultinomialNB().fit(X_train_counts, y_train)
# 预测新样本所属类别
predicted = clf.predict(X_test_counts)
print(f'Accuracy: {accuracy_score(y_test, predicted):.4f}')
print(classification_report(y_test, predicted))
```
这段代码展示
阅读全文