R - Image Plot MNIST 数据集
R - Image Plot MNIST dataset
我的数据集是来自 Kaggle
的 MNIST
我正在尝试使用 image
函数来可视化训练集中的第一个数字。不幸的是我收到以下错误:
>image(1:28, 1:28, im, col=gray((0:255)/255))
Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) :
'z' must be numeric or logical
添加一些代码:
rawfile<-read.csv("D://Kaggle//MNIST//train.csv",header=T) #Reading the csv file
im<-matrix((rawfile[1,2:ncol(rawfile)]), nrow=28, ncol=28) #For the 1st Image
image(1:28, 1:28, im, col=gray((0:255)/255))
Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) :
'z' must be numeric or logical
目前你的即时通讯是一个字符矩阵。您需要将其转换为数字矩阵,例如通过发布 im_numbers <- apply(im, 2, as.numeric)
.
然后您可以发出 image(1:28, 1:28, im_numbers, col=gray((0:255)/255))
.
做图像(1:28,1:28,im_numbers,col=gray((255:0)/255))为白色背景上的黑色数字... =]
我一直在尝试使用 graphics::image
函数绘制相同的数据集。但是,由于矩阵往往以图形未正确对齐的方式填充,因此我编写了一个函数来为给定的观察绘制正确的图:
#Function to visualize a number
img <- function(data, row_index){
#Obtaining the row as a numeric vector
r <- as.numeric(d[row_index, 2:785])
#Creating a empty matrix to use
im <- matrix(nrow = 28, ncol = 28)
#Filling properly the data into the matrix
j <- 1
for(i in 28:1){
im[,i] <- r[j:(j+27)]
j <- j+28
}
#Plotting the image with the label
image(x = 1:28,
y = 1:28,
z = im,
col=gray((0:255)/255),
main = paste("Number:", d[row_index, 1]))
}
我写它是因为我在试图找到一种正确绘制它的方法时遇到了困难,并且由于我没有找到它,所以我在这里分享这个函数供其他人使用。
这是一个小程序,可以可视化 Keras 包中的前 36 个 MNIST 数字。
library(keras)
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
# visualize the digits
par(mfcol=c(6,6))
par(mar=c(0, 0, 3, 0), xaxs='i', yaxs='i')
for (idx in 1:36) {
im <- x_train[idx,,,1]
im <- t(apply(im, 2, rev))
image(1:28, 1:28, im, col=gray((0:255)/255),
xaxt='n', main=paste(y_train[idx]))
}
剧情是这样的:
我的数据集是来自 Kaggle
的 MNIST我正在尝试使用 image
函数来可视化训练集中的第一个数字。不幸的是我收到以下错误:
>image(1:28, 1:28, im, col=gray((0:255)/255))
Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) :
'z' must be numeric or logical
添加一些代码:
rawfile<-read.csv("D://Kaggle//MNIST//train.csv",header=T) #Reading the csv file
im<-matrix((rawfile[1,2:ncol(rawfile)]), nrow=28, ncol=28) #For the 1st Image
image(1:28, 1:28, im, col=gray((0:255)/255))
Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) :
'z' must be numeric or logical
目前你的即时通讯是一个字符矩阵。您需要将其转换为数字矩阵,例如通过发布 im_numbers <- apply(im, 2, as.numeric)
.
然后您可以发出 image(1:28, 1:28, im_numbers, col=gray((0:255)/255))
.
做图像(1:28,1:28,im_numbers,col=gray((255:0)/255))为白色背景上的黑色数字... =]
我一直在尝试使用 graphics::image
函数绘制相同的数据集。但是,由于矩阵往往以图形未正确对齐的方式填充,因此我编写了一个函数来为给定的观察绘制正确的图:
#Function to visualize a number
img <- function(data, row_index){
#Obtaining the row as a numeric vector
r <- as.numeric(d[row_index, 2:785])
#Creating a empty matrix to use
im <- matrix(nrow = 28, ncol = 28)
#Filling properly the data into the matrix
j <- 1
for(i in 28:1){
im[,i] <- r[j:(j+27)]
j <- j+28
}
#Plotting the image with the label
image(x = 1:28,
y = 1:28,
z = im,
col=gray((0:255)/255),
main = paste("Number:", d[row_index, 1]))
}
我写它是因为我在试图找到一种正确绘制它的方法时遇到了困难,并且由于我没有找到它,所以我在这里分享这个函数供其他人使用。
这是一个小程序,可以可视化 Keras 包中的前 36 个 MNIST 数字。
library(keras)
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
# visualize the digits
par(mfcol=c(6,6))
par(mar=c(0, 0, 3, 0), xaxs='i', yaxs='i')
for (idx in 1:36) {
im <- x_train[idx,,,1]
im <- t(apply(im, 2, rev))
image(1:28, 1:28, im, col=gray((0:255)/255),
xaxt='n', main=paste(y_train[idx]))
}
剧情是这样的: