python 贝叶斯建模代码
时间: 2023-10-13 16:22:08 浏览: 111
Python 中的贝叶斯建模和概率编程
以下是一个简单的贝叶斯建模代码示例,用于分类文本数据:
```python
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
# 导入数据
data = pd.read_csv('data.csv', encoding='gb18030')
# 划分训练集和测试集
train_data = data.sample(frac=0.8, random_state=1)
test_data = data.drop(train_data.index)
# 特征提取
vectorizer = CountVectorizer(stop_words='english')
train_features = vectorizer.fit_transform(train_data['text'])
test_features = vectorizer.transform(test_data['text'])
# 建立模型
model = MultinomialNB()
model.fit(train_features, train_data['label'])
# 预测并评估模型
train_pred = model.predict(train_features)
test_pred = model.predict(test_features)
train_acc = accuracy_score(train_data['label'], train_pred)
test_acc = accuracy_score(test_data['label'], test_pred)
print('训练集准确率:', train_acc)
print('测试集准确率:', test_acc)
```
在这个例子中,我们使用`sklearn`库中的`CountVectorizer`来提取文本特征,使用`MultinomialNB`建立朴素贝叶斯模型,并使用`accuracy_score`函数评估模型准确率。
阅读全文