请用Python实现朴素贝叶斯算法并展示结果
时间: 2024-09-30 16:08:24 浏览: 29
朴素贝叶斯分类是一种基于概率的机器学习算法,在Python中可以使用sklearn库方便地实现。以下是使用sklearn库中的`MultinomialNB`类来实现朴素贝叶斯分类的一个简单例子:
```python
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
# 加载数据集(这里以新闻组数据为例)
data = fetch_20newsgroups(subset='train', categories=['alt.atheism', 'sci.space'])
X = data.data
y = data.target
# 分割数据为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 文本向量化,将文本转换为词频矩阵
vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)
# 创建朴素贝叶斯模型并训练
nb_classifier = MultinomialNB()
nb_classifier.fit(X_train_vec, y_train)
# 预测测试集结果
y_pred = nb_classifier.predict(X_test_vec)
# 打印预测准确率
accuracy = (y_pred == y_test).mean() * 100
print(f"朴素贝叶斯分类器的准确率为: {accuracy:.2f}%")
阅读全文