Python实现支持向量机(SVM) MNIST数据集

Python实现支持向量机(SVM) MNIST数据集

SVM的原理这里不讲,大家自己可以查阅相关资料。

下面是利用sklearn库进行svm训练MNIST数据集,准确率可以达到90%以上。


from sklearn import svm
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=False
  • 6
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是使用R语言读取MNIST数据集并利用支持向量机实现MNIST识别分类的代码: 首先需要安装以下几个包:`reticulate`, `e1071`, `raster`, `magrittr`。 ```R # 加载所需包 library(reticulate) library(e1071) library(raster) library(magrittr) # 设置Python环境 use_python("/usr/bin/python3") # 读取MNIST数据集 train_images <- readBin("train-images-idx3-ubyte", "raw", n = 60000 * 28 * 28, endian = "big") train_labels <- readBin("train-labels-idx1-ubyte", "raw", n = 60000, endian = "big") test_images <- readBin("t10k-images-idx3-ubyte", "raw", n = 10000 * 28 * 28, endian = "big") test_labels <- readBin("t10k-labels-idx1-ubyte", "raw", n = 10000, endian = "big") # 将数据集转换为矩阵形式 train_images <- matrix(train_images, nrow = 60000, ncol = 28 * 28, byrow = TRUE) test_images <- matrix(test_images, nrow = 10000, ncol = 28 * 28, byrow = TRUE) # 将标签转换为数字形式 train_labels <- as.integer(train_labels) test_labels <- as.integer(test_labels) # 数据归一化 train_images <- train_images / 255 test_images <- test_images / 255 # 创建SVM模型并训练 svm_model <- svm(train_images, train_labels, type = "C-classification", kernel = "linear") # 预测测试集 predicted_labels <- predict(svm_model, test_images) # 计算准确率 accuracy <- mean(predicted_labels == test_labels) cat("Accuracy:", accuracy, "\n") ``` 注意:在运行代码之前,需要先下载MNIST数据集,并将其放置在当前工作目录下。可以从以下链接下载: 训练集: - train-images-idx3-ubyte.gz: http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz - train-labels-idx1-ubyte.gz: http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz 测试集: - t10k-images-idx3-ubyte.gz: http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz - t10k-labels-idx1-ubyte.gz: http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz 下载完成后,需要解压缩文件并重命名为对应的文件名。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值