朴素贝叶斯学习曲线代码
时间: 2023-11-12 14:52:28 浏览: 91
朴素贝叶斯分类器算法
以下是一个使用朴素贝叶斯算法绘制学习曲线的示例代码:
```python
import numpy np
import matplotlib.pyplot plt
from sklearn.datasets import_iris
from sklearn.model_selection import learning_curve
from sklearn.naive_bayes import GaussianNB
# 加载数据集
data = load_iris()
X = data.data
y = data.target
# 定义朴素贝叶斯分类器
naive_bayes = GaussianNB()
# 绘制学习曲线
train_sizes, train_scores, test_scores = learning_curve(naive_bayes, X, y, cv=5, train_sizes=np.linspace(0.1, 1.0, 10))
# 计算平均准确率和标准差
train_mean = np.mean(train_scores, axis=1)
train_std = np.std(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
test_std = np.std(test_scores, axis=1)
# 绘制学习曲线图
plt.figure()
plt.plot(train_sizes, train_mean, 'o-', color='r', label='Training Accuracy')
plt.plot(train_sizes, test_mean, 'o-', color='g', label='Validation Accuracy')
plt.fill_between(train_sizes, train_mean - train_std, train_mean + train_std, alpha=0.1, color='r')
plt.fill_between(train_sizes, test_mean - test_std, test_mean + test_std, alpha=0.1, color='g')
plt.xlabel('Training Set Size')
plt.ylabel('Accuracy')
plt.title('Learning Curve - Naive Bayes')
plt.legend(loc='best')
plt.show()
```
这段代码使用了Scikit-learn库中的`learning_curve`函数来计算训练集和验证集的准确率,并绘制了学习曲线图。你可以根据需要调整参数,并将其应用于你的数据集。
阅读全文