贝叶斯分类python代码
时间: 2023-11-05 15:05:46 浏览: 94
好的,以下是一个简单的贝叶斯分类的 Python 代码示例:
```python
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
# 训练数据
train_data = ['I love this sandwich', 'This is an amazing place', 'I feel very good about these beers']
train_labels = ['positive', 'positive', 'positive']
# 测试数据
test_data = ['This is my best work', 'What an awesome view', 'I do not like this restaurant']
test_labels = ['positive', 'positive', 'negative']
# 特征提取
vectorizer = CountVectorizer()
train_features = vectorizer.fit_transform(train_data)
test_features = vectorizer.transform(test_data)
# 训练模型
clf = MultinomialNB()
clf.fit(train_features, train_labels)
# 预测
predictions = clf.predict(test_features)
# 输出结果
for i, prediction in enumerate(predictions):
print('Prediction for "{}": {}'.format(test_data[i], prediction))
```
阅读全文