{"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":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport cv2\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\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-16T05:58:46.395923Z","iopub.execute_input":"2023-05-16T05:58:46.398382Z","iopub.status.idle":"2023-05-16T05:58:46.52843Z"},"trusted":true},"execution_count":1,"outputs":[{"ename":"ERROR","evalue":"Error in parse(text = x, srcfile = src): <text>:5:8: unexpected symbol\n4: \n5: import numpy\n          ^\n","traceback":["Error in parse(text = x, srcfile = src): <text>:5:8: unexpected symbol\n4: \n5: import numpy\n          ^\nTraceback:\n"],"output_type":"error"}]},{"cell_type":"code","source":"library(tidyverse)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-16T05:59:35.851886Z","iopub.execute_input":"2023-05-16T05:59:35.885558Z","iopub.status.idle":"2023-05-16T05:59:37.127313Z"},"trusted":true},"execution_count":2,"outputs":[{"name":"stderr","text":"── \u001b[1mAttaching core tidyverse packages\u001b[22m ──────────────────────── tidyverse 2.0.0 ──\n\u001b[32m✔\u001b[39m \u001b[34mdplyr    \u001b[39m 1.1.0     \u001b[32m✔\u001b[39m \u001b[34mreadr    \u001b[39m 2.1.4\n\u001b[32m✔\u001b[39m \u001b[34mforcats  \u001b[39m 1.0.0     \u001b[32m✔\u001b[39m \u001b[34mstringr  \u001b[39m 1.5.0\n\u001b[32m✔\u001b[39m \u001b[34mggplot2  \u001b[39m 3.4.1     \u001b[32m✔\u001b[39m \u001b[34mtibble   \u001b[39m 3.1.8\n\u001b[32m✔\u001b[39m \u001b[34mlubridate\u001b[39m 1.9.2     \u001b[32m✔\u001b[39m \u001b[34mtidyr    \u001b[39m 1.3.0\n\u001b[32m✔\u001b[39m \u001b[34mpurrr    \u001b[39m 1.0.1     \n── \u001b[1mConflicts\u001b[22m ────────────────────────────────────────── tidyverse_conflicts() ──\n\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m    masks \u001b[34mstats\u001b[39m::lag()\n\u001b[36mℹ\u001b[39m Use the conflicted package (\u001b[3m\u001b[34m<http://conflicted.r-lib.org/>\u001b[39m\u001b[23m) to force all conflicts to become errors\n","output_type":"stream"}]},{"cell_type":"code","source":"data <- read.csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\")\ndim(data)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T06:13:33.894522Z","iopub.execute_input":"2023-05-16T06:13:33.896543Z","iopub.status.idle":"2023-05-16T06:19:46.362619Z"},"trusted":true},"execution_count":1,"outputs":[{"output_type":"display_data","data":{"text/html":"<style>\n.list-inline {list-style: none; margin:0; padding: 0}\n.list-inline>li {display: inline-block}\n.list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n</style>\n<ol class=list-inline><li>26296946</li><li>20</li></ol>\n","text/markdown":"1. 26296946\n2. 20\n\n\n","text/latex":"\\begin{enumerate*}\n\\item 26296946\n\\item 20\n\\end{enumerate*}\n","text/plain":"[1] 26296946       20"},"metadata":{}}]},{"cell_type":"code","source":"X <- \ny <- data.frame(y = factor(labels)) %>%\n  recipe(y~., data=.) %>%\n  step_dummy(y, one_hot = TRUE) %>%\n  prep(NULL) %>%\n  juice()\ny <- y %>% as.matrix()\ncolnames(y)<-as.character(0:9)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(reticulate)\nkeras <- import(\"keras\")\n## create sequential model\nmodel_nn1 <- keras$Sequential()\n## add layers\n### 1. flatten layer (input layer)\nmodel_nn1$add(keras$layers$Flatten(input_shape = c(64L,64L)))\n### 2. hidden layer\nmodel_nn1$add(keras$layers$Dense(units = 400, \n                                 activation = \"relu\"))\n### 2. output layer\nmodel_nn1$add(keras$layers$Dense(10, activation = \"softmax\"))\n\n## compling\nmodel_nn1$compile(optimizer = \"adam\",\n                       loss = \"categorical_crossentropy\",\n                       metrics = 'accuracy')\nmodel_nn1$summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## set early stopping\nearly_stopping <- keras$callbacks$EarlyStopping(monitor = \"val_loss\",\n                                                patience = 10L)\n## fit the predictive model\nann_result_notune <- model_nn1$fit(x = train_x,\n                                   y = train_y,\n                                   validation_split = 0.4,\n                                   epochs = 500L,\n                                   callbacks = list(early_stopping))","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}