{"cells":[{"metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":false},"cell_type":"code","source":"# This R environment comes with many helpful analytics packages installed\n# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats\n# For example, here's a helpful package to load\n\nlibrary(tidyverse) # metapackage of all tidyverse packages\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nlist.files(path = \"../input\")\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(tidyverse)\nlibrary(keras)\n\ntrain_mnist <- read.csv(\"../input/mnist-in-csv/mnist_train.csv\")\n\ndim(train_mnist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"head(train_mnist)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Range warna tiap pixel = 0 -255","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Visualisasi Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"vizTrain <- function(input){\n  \n  dimmax <- sqrt(ncol(input[,-1]))\n  \n  dimn <- ceiling(sqrt(nrow(input)))\n  par(mfrow=c(dimn, dimn), mar=c(.1, .1, .1, .1))\n  \n  for (i in 1:nrow(input)){\n      m1 <- as.matrix(input[i,2:785])\n      dim(m1) <- c(28,28)\n      \n      m1 <- apply(apply(m1, 1, rev), 1, t)\n      \n      image(1:28, 1:28, \n            m1, col=grey.colors(255), \n            # remove axis text\n            xaxt = 'n', yaxt = 'n')\n      text(2, 20, col=\"white\", cex=1.2, input[i, 1])\n  } \n}\n\nvizTrain(head(train_mnist, 16))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Membagi data menjadi data train dan data test, dengan 80% data adalah data train","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"set.seed(123)\nindex <- sample(nrow(train_mnist), nrow(train_mnist)*0.8)\n\ndata_train <- train_mnist[ index, ]\ndata_test <- train_mnist[ -index, ]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_train$label %>% table() %>% prop.table()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Langkah yang dilakukan\n\n1. Membuang kolom target (label)/mengambil variabel prediktor saja\n2. Mengubah data menjadi matrix\n3. Mengubah matrix menjadi array supaya sesuai dengan input keras\n4. Mengambil variabel target saja\n5. Target variabel diubah dengan encoding menggunakan to_categorical","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_x <- data_train %>% \n  select(-label) %>% \n  as.matrix()\n\ntest_x <- data_test %>% \n  select(-label) %>% \n  as.matrix()\n\n# Mengubah data agar dapat diterima keras\n\n## Prediktor\ntrain_x <- array_reshape(x = train_x, \n                         dim = dim(train_x))\n\ntest_x <- array_reshape(x = test_x, \n                         dim = dim(test_x))\n\n# Cek kelas train_x\nclass(train_x)\n\n## Target, yang perlu diubah hanya train_y\nnum_class <- n_distinct(data_train$label)\ntrain_y <- data_train$label\ntrain_y <- to_categorical(y = train_y, \n                          num_classes = num_class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class(train_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tensorflow::tf_version()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.3"}},"nbformat":4,"nbformat_minor":4}