svm识别minist数据集python代码
时间: 2023-08-16 12:04:32 浏览: 234
以下是使用 SVM 对 MNIST 数据集进行分类的 Python 代码示例:
```python
import numpy as np
from sklearn import datasets, svm
from sklearn.model_selection import train_test_split
# 加载 MNIST 数据集
digits = datasets.load_digits()
# 分割数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42)
# 创建 SVM 分类器
clf = svm.SVC(kernel='linear', C=1, gamma='scale')
# 训练 SVM 分类器
clf.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = clf.predict(X_test)
# 计算模型准确率
accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)
```
在这段代码中,我们首先导入必要的库,然后加载 MNIST 数据集并将其分割为训练集和测试集。接下来,我们创建一个 SVM 分类器,并使用训练集训练它。然后,我们在测试集上进行预测,并计算模型的准确率。最后,我们将准确率打印出来。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)