利用R语言使用支持向量机实现MNIST手写数据集识别
时间: 2024-06-08 22:09:29 浏览: 103
机器学习:支持向量机实现手写识别
MNIST手写数字数据集是一个经典的图像识别数据集,其中包含了60,000张训练图像和10,000张测试图像,每个图像都是28x28像素的灰度图像。支持向量机(Support Vector Machine, SVM)是一种常见的分类模型,可以用于图像分类任务。在R语言中,我们可以使用`e1071`包中的`svm`函数来实现支持向量机的训练和预测。
首先,我们需要下载并导入MNIST数据集。可以通过以下代码实现:
```r
library(RCurl)
library(ggplot2)
# 下载训练集和测试集
train_url <- "http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"
test_url <- "http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
train_file <- basename(train_url)
test_file <- basename(test_url)
if (!file.exists(train_file)) download.file(train_url, train_file)
if (!file.exists(test_file)) download.file(test_url, test_file)
# 读取训练集和测试集的图像数据
train_images <- readBin(train_file, "raw", n = 16 * 16^6, size = 1)
test_images <- readBin(test_file, "raw", n = 16 * 16^6, size = 1)
train_images <- as.matrix(read.table(text = train_images, sep = " ", fill = TRUE))
test_images <- as.matrix(read.table(text = test_images, sep = " ", fill = TRUE))
# 将图像数据转化为28x28的矩阵
train_images <- array(train_images, dim = c(60000, 28, 28))
test_images <- array(test_images, dim = c(10000, 28, 28))
```
接下来,我们需要将图像数据转化为特征向量。一种常见的方法是将每个28x28的图像转化为一个784维的向量,即将图像中的每个像素值作为一个特征。可以通过以下代码实现:
```r
# 将图像数据转化为特征向量
train_features <- matrix(train_images, nrow = 60000, ncol = 784)
test_features <- matrix(test_images, nrow = 10000, ncol = 784)
```
然后,我们需要将标签数据转化为分类变量。MNIST数据集中的标签是0到9之间的数字,可以使用`factor`函数将其转化为分类变量。可以通过以下代码实现:
```r
# 读取训练集和测试集的标签数据
train_labels_url <- "http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"
test_labels_url <- "http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"
train_labels_file <- basename(train_labels_url)
test_labels_file <- basename(test_labels_url)
if (!file.exists(train_labels_file)) download.file(train_labels_url, train_labels_file)
if (!file.exists(test_labels_file)) download.file(test_labels_url, test_labels_file)
# 将标签数据转化为分类变量
train_labels <- readBin(train_labels_file, "raw", n = 6e4, size = 1)
test_labels <- readBin(test_labels_file, "raw", n = 1e4, size = 1)
train_labels <- as.numeric(train_labels)
test_labels <- as.numeric(test_labels)
train_labels <- factor(train_labels, levels = 0:9)
test_labels <- factor(test_labels, levels = 0:9)
```
现在我们可以使用`svm`函数训练支持向量机模型。可以通过以下代码实现:
```r
# 训练支持向量机模型
model <- svm(train_features, train_labels, cost = 100, gamma = 0.01)
# 预测测试集的标签
pred_labels <- predict(model, test_features)
```
最后,可以使用`caret`包中的`confusionMatrix`函数计算模型的准确率和混淆矩阵。可以通过以下代码实现:
```r
library(caret)
# 计算准确率和混淆矩阵
cm <- confusionMatrix(pred_labels, test_labels)
accuracy <- cm$overall["Accuracy"]
print(paste("Accuracy:", accuracy))
print(cm$table)
```
完整的代码如下:
```r
library(RCurl)
library(ggplot2)
library(e1071)
library(caret)
# 下载训练集和测试集
train_url <- "http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"
test_url <- "http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
train_file <- basename(train_url)
test_file <- basename(test_url)
if (!file.exists(train_file)) download.file(train_url, train_file)
if (!file.exists(test_file)) download.file(test_url, test_file)
# 读取训练集和测试集的图像数据
train_images <- readBin(train_file, "raw", n = 16 * 16^6, size = 1)
test_images <- readBin(test_file, "raw", n = 16 * 16^6, size = 1)
train_images <- as.matrix(read.table(text = train_images, sep = " ", fill = TRUE))
test_images <- as.matrix(read.table(text = test_images, sep = " ", fill = TRUE))
# 将图像数据转化为28x28的矩阵
train_images <- array(train_images, dim = c(60000, 28, 28))
test_images <- array(test_images, dim = c(10000, 28, 28))
# 将图像数据转化为特征向量
train_features <- matrix(train_images, nrow = 60000, ncol = 784)
test_features <- matrix(test_images, nrow = 10000, ncol = 784)
# 读取训练集和测试集的标签数据
train_labels_url <- "http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"
test_labels_url <- "http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"
train_labels_file <- basename(train_labels_url)
test_labels_file <- basename(test_labels_url)
if (!file.exists(train_labels_file)) download.file(train_labels_url, train_labels_file)
if (!file.exists(test_labels_file)) download.file(test_labels_url, test_labels_file)
# 将标签数据转化为分类变量
train_labels <- readBin(train_labels_file, "raw", n = 6e4, size = 1)
test_labels <- readBin(test_labels_file, "raw", n = 1e4, size = 1)
train_labels <- as.numeric(train_labels)
test_labels <- as.numeric(test_labels)
train_labels <- factor(train_labels, levels = 0:9)
test_labels <- factor(test_labels, levels = 0:9)
# 训练支持向量机模型
model <- svm(train_features, train_labels, cost = 100, gamma = 0.01)
# 预测测试集的标签
pred_labels <- predict(model, test_features)
# 计算准确率和混淆矩阵
cm <- confusionMatrix(pred_labels, test_labels)
accuracy <- cm$overall["Accuracy"]
print(paste("Accuracy:", accuracy))
print(cm$table)
```
运行以上代码可以得到MNIST手写数字数据集的支持向量机分类器准确率和混淆矩阵。
阅读全文