{"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":"import numpy as np\nimport pandas as pd\nimport os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\nfrom IPython.display import YouTubeVideo\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport warnings\nwarnings.filterwarnings('ignore')\n","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:54.682149Z","iopub.execute_input":"2022-03-23T21:32:54.68245Z","iopub.status.idle":"2022-03-23T21:32:54.688432Z","shell.execute_reply.started":"2022-03-23T21:32:54.682419Z","shell.execute_reply":"2022-03-23T21:32:54.687516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprint(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:54.708634Z","iopub.execute_input":"2022-03-23T21:32:54.709455Z","iopub.status.idle":"2022-03-23T21:32:56.997949Z","shell.execute_reply.started":"2022-03-23T21:32:54.709414Z","shell.execute_reply":"2022-03-23T21:32:56.996983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_df.drop(columns = ['label']).values.astype('float32')\ny = train_df['label'].values\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, stratify = y, random_state = 0)\n\nprint(X_train.shape)\n\n''' Reshape the tensfor, to  '''\nX_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:56.999914Z","iopub.execute_input":"2022-03-23T21:32:57.000423Z","iopub.status.idle":"2022-03-23T21:32:57.556575Z","shell.execute_reply.started":"2022-03-23T21:32:57.000376Z","shell.execute_reply":"2022-03-23T21:32:57.555709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n            keras.Input(shape = (28, 28, 1)),\n            layers.Conv2D(32, 3, padding = 'valid', activation = 'relu'),\n            layers.MaxPooling2D(),\n            layers.Conv2D(64, 3, padding = 'valid', activation = 'relu'),\n            layers.MaxPooling2D(),\n            layers.Conv2D(128, 3, padding = 'valid', activation = 'relu'),\n            layers.Flatten(),\n            layers.Dense(128, activation = 'relu'),\n            layers.Dense(10),\n])\n\nmodel.compile(\n        loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True),\n        optimizer = keras.optimizers.Adam(lr = 1e-4),\n        metrics = ['accuracy'],\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:57.557738Z","iopub.execute_input":"2022-03-23T21:32:57.557971Z","iopub.status.idle":"2022-03-23T21:32:57.62769Z","shell.execute_reply.started":"2022-03-23T21:32:57.557946Z","shell.execute_reply":"2022-03-23T21:32:57.626809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:57.629355Z","iopub.execute_input":"2022-03-23T21:32:57.629681Z","iopub.status.idle":"2022-03-23T21:32:57.639066Z","shell.execute_reply.started":"2022-03-23T21:32:57.629648Z","shell.execute_reply":"2022-03-23T21:32:57.638279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, batch_size = 64, epochs = 50, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:32:57.640554Z","iopub.execute_input":"2022-03-23T21:32:57.64147Z","iopub.status.idle":"2022-03-23T21:44:22.286519Z","shell.execute_reply.started":"2022-03-23T21:32:57.64142Z","shell.execute_reply":"2022-03-23T21:44:22.285623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_train, y_train, batch_size = 64, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:44:22.287896Z","iopub.execute_input":"2022-03-23T21:44:22.288108Z","iopub.status.idle":"2022-03-23T21:44:27.308923Z","shell.execute_reply.started":"2022-03-23T21:44:22.288083Z","shell.execute_reply":"2022-03-23T21:44:27.30798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test, y_test, batch_size = 64, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:44:27.311015Z","iopub.execute_input":"2022-03-23T21:44:27.311235Z","iopub.status.idle":"2022-03-23T21:44:27.948392Z","shell.execute_reply.started":"2022-03-23T21:44:27.311209Z","shell.execute_reply":"2022-03-23T21:44:27.947696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nfinal_labels = model.predict(((test_df.values).reshape(-1, 28, 28, 1) / 255.0))\nfinal_labels = np.argmax(final_labels, axis = 1)\nfinal_labels","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:44:27.949722Z","iopub.execute_input":"2022-03-23T21:44:27.949964Z","iopub.status.idle":"2022-03-23T21:44:35.270541Z","shell.execute_reply.started":"2022-03-23T21:44:27.949937Z","shell.execute_reply":"2022-03-23T21:44:35.268989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame()\nimage_id = [i for i in range(1, 28001)]\nsubmission_df['ImageId'] = image_id\nsubmission_df['Label'] = final_labels\nsubmission_df.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-23T21:47:18.650021Z","iopub.execute_input":"2022-03-23T21:47:18.650341Z","iopub.status.idle":"2022-03-23T21:47:18.702986Z","shell.execute_reply.started":"2022-03-23T21:47:18.650305Z","shell.execute_reply":"2022-03-23T21:47:18.702263Z"},"trusted":true},"execution_count":null,"outputs":[]}]}