贝叶斯分类器代码实现python
时间: 2023-11-09 12:03:23 浏览: 92
贝叶斯分类算法python实现
以下是一个简单的贝叶斯分类器的代码实现,使用Python的scikit-learn库:
```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']
# 特征提取
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)
print(predictions)
```
阅读全文