朴素贝叶斯分类mnist数据集python实现
时间: 2023-07-21 08:23:24 浏览: 129
下面是一个使用Python实现朴素贝叶斯分类器对MNIST数据集进行分类的简单示例:
首先,需要使用Python的NumPy库和Scikit-learn库加载MNIST数据集:
```python
from sklearn.datasets import fetch_openml
import numpy as np
mnist = fetch_openml('mnist_784')
X = mnist.data.astype('float32')
y = mnist.target.astype('int32')
```
然后,可以将数据集拆分为训练集和测试集:
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
接下来,可以使用Scikit-learn库中的多项式朴素贝叶斯分类器来训练模型:
```python
from sklearn.naive_bayes import MultinomialNB
nb = MultinomialNB()
nb.fit(X_train, y_train)
```
最后,可以使用测试集评估模型的性能:
```python
from sklearn.metrics import accuracy_score
y_pred = nb.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
完整代码如下:
```python
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import numpy as np
# 加载数据集
mnist = fetch_openml('mnist_784')
X = mnist.data.astype('float32')
y = mnist.target.astype('int32')
# 拆分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练模型
nb = MultinomialNB()
nb.fit(X_train, y_train)
# 评估模型
y_pred = nb.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
注意,上述示例中使用的是多项式朴素贝叶斯分类器,而不是高斯朴素贝叶斯分类器,因为像素值是离散的。如果将像素值视为连续变量,则应该使用高斯朴素贝叶斯分类器。
阅读全文