{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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)\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\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":"2022-07-20T08:22:04.971298Z","iopub.execute_input":"2022-07-20T08:22:04.971812Z","iopub.status.idle":"2022-07-20T08:22:04.989360Z","shell.execute_reply.started":"2022-07-20T08:22:04.971768Z","shell.execute_reply":"2022-07-20T08:22:04.988085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = '../input/digit-recognizer'\ndf_train = pd.read_csv(os.path.join(data_path, 'train.csv'))\ndf_test = pd.read_csv(os.path.join(data_path, 'test.csv'))\nprint(df_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:04.994546Z","iopub.execute_input":"2022-07-20T08:22:04.995717Z","iopub.status.idle":"2022-07-20T08:22:10.646288Z","shell.execute_reply.started":"2022-07-20T08:22:04.995679Z","shell.execute_reply":"2022-07-20T08:22:10.644398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"NaN values in train: \", df_train.isnull().values.any())\nprint(\"NaN values in test: \", df_test.isnull().values.any())\nprint(\"Largest entry: \", max(df_train.drop(columns=\"label\").max()))\n\nX = df_train.drop(columns=\"label\").astype(float)\nX_test = df_test.astype(float)\n\ny = df_train[\"label\"]\n\nX /= 255\nX_test /= 255","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:10.648283Z","iopub.execute_input":"2022-07-20T08:22:10.648636Z","iopub.status.idle":"2022-07-20T08:22:11.156914Z","shell.execute_reply.started":"2022-07-20T08:22:10.648598Z","shell.execute_reply":"2022-07-20T08:22:11.155962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 10\ny = keras.utils.to_categorical(y, num_classes)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.158334Z","iopub.execute_input":"2022-07-20T08:22:11.158704Z","iopub.status.idle":"2022-07-20T08:22:11.167886Z","shell.execute_reply.started":"2022-07-20T08:22:11.158669Z","shell.execute_reply":"2022-07-20T08:22:11.166755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 28\nX = X.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)\nX_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.171273Z","iopub.execute_input":"2022-07-20T08:22:11.172307Z","iopub.status.idle":"2022-07-20T08:22:11.178240Z","shell.execute_reply.started":"2022-07-20T08:22:11.172268Z","shell.execute_reply":"2022-07-20T08:22:11.177011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed(seed)\ntf.random.set_seed(seed)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.179885Z","iopub.execute_input":"2022-07-20T08:22:11.180312Z","iopub.status.idle":"2022-07-20T08:22:11.192584Z","shell.execute_reply.started":"2022-07-20T08:22:11.180277Z","shell.execute_reply":"2022-07-20T08:22:11.191595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_dev, y_train, y_dev = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.195300Z","iopub.execute_input":"2022-07-20T08:22:11.195664Z","iopub.status.idle":"2022-07-20T08:22:11.577177Z","shell.execute_reply.started":"2022-07-20T08:22:11.195628Z","shell.execute_reply":"2022-07-20T08:22:11.576202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ntrain_gen = ImageDataGenerator(\n        rotation_range=10,  \n        zoom_range = 0.1,\n        width_shift_range=0.1, \n        height_shift_range=0.1, \n)\ntrain_gen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.578430Z","iopub.execute_input":"2022-07-20T08:22:11.578795Z","iopub.status.idle":"2022-07-20T08:22:11.655008Z","shell.execute_reply.started":"2022-07-20T08:22:11.578760Z","shell.execute_reply":"2022-07-20T08:22:11.654077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (28, 28, 1)\n\nmodel = keras.Sequential(\n    [\n        keras.Input(shape=input_shape),\n        layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Flatten(),\n        layers.Dropout(0.5),\n        layers.Dense(num_classes, activation=\"softmax\"),\n    ]\n)\n\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.656617Z","iopub.execute_input":"2022-07-20T08:22:11.656982Z","iopub.status.idle":"2022-07-20T08:22:11.709193Z","shell.execute_reply.started":"2022-07-20T08:22:11.656946Z","shell.execute_reply":"2022-07-20T08:22:11.708088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training = model.fit(X_train, y_train, epochs=50, batch_size=128, validation_data=(X_dev, y_dev), verbose=1)\ntraining = model.fit_generator(train_gen.flow(X_train,y_train, batch_size=128), validation_data = (X_dev,y_dev), epochs = 100)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:22:11.710552Z","iopub.execute_input":"2022-07-20T08:22:11.711007Z","iopub.status.idle":"2022-07-20T08:36:29.162680Z","shell.execute_reply.started":"2022-07-20T08:22:11.710969Z","shell.execute_reply":"2022-07-20T08:36:29.161742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(X_test),axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:36:29.166315Z","iopub.execute_input":"2022-07-20T08:36:29.166588Z","iopub.status.idle":"2022-07-20T08:36:30.370638Z","shell.execute_reply.started":"2022-07-20T08:36:29.166562Z","shell.execute_reply":"2022-07-20T08:36:30.369668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission_df = pd.DataFrame({'ImageId':sample_submission['ImageId'],'Label':predictions})\nprint(submission_df)\nsubmission_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:36:30.372109Z","iopub.execute_input":"2022-07-20T08:36:30.372450Z","iopub.status.idle":"2022-07-20T08:36:30.429246Z","shell.execute_reply.started":"2022-07-20T08:36:30.372405Z","shell.execute_reply":"2022-07-20T08:36:30.428243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}