{"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)\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-07-15T15:33:38.044400Z","iopub.execute_input":"2023-07-15T15:33:38.044859Z","iopub.status.idle":"2023-07-15T15:33:38.054822Z","shell.execute_reply.started":"2023-07-15T15:33:38.044822Z","shell.execute_reply":"2023-07-15T15:33:38.053618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\n\n\ndf = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf\n","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:38.057284Z","iopub.execute_input":"2023-07-15T15:33:38.057616Z","iopub.status.idle":"2023-07-15T15:33:41.310635Z","shell.execute_reply.started":"2023-07-15T15:33:38.057588Z","shell.execute_reply":"2023-07-15T15:33:41.309575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.reshape(df.loc[:, df.columns != 'label'].to_numpy(), (len(df), 28, 28))\ny = df['label'].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:41.312091Z","iopub.execute_input":"2023-07-15T15:33:41.312482Z","iopub.status.idle":"2023-07-15T15:33:41.426705Z","shell.execute_reply.started":"2023-07-15T15:33:41.312450Z","shell.execute_reply":"2023-07-15T15:33:41.425462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nprint(\"Training data shape:\", X_train.shape)\nprint(\"Training labels shape:\", y_train.shape)\nprint(\"Test data shape:\", X_test.shape)\nprint(\"Test labels shape:\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:42.854680Z","iopub.execute_input":"2023-07-15T15:33:42.855042Z","iopub.status.idle":"2023-07-15T15:33:43.262829Z","shell.execute_reply.started":"2023-07-15T15:33:42.855010Z","shell.execute_reply":"2023-07-15T15:33:43.261576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize pixel values between 0 and 1\nX_train = X_train / 255.0\nX_test = X_test / 255.0\n\n# Reshape data to add a channel dimension\nX_train = np.expand_dims(X_train, axis=-1)\nX_test = np.expand_dims(X_test, axis=-1)\n\n# Convert labels to one-hot encoding\ny_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\ny_test = tf.keras.utils.to_categorical(y_test, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:43.264120Z","iopub.execute_input":"2023-07-15T15:33:43.264431Z","iopub.status.idle":"2023-07-15T15:33:43.381102Z","shell.execute_reply.started":"2023-07-15T15:33:43.264403Z","shell.execute_reply":"2023-07-15T15:33:43.379899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n    tf.keras.layers.MaxPooling2D((2, 2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:43.382752Z","iopub.execute_input":"2023-07-15T15:33:43.383083Z","iopub.status.idle":"2023-07-15T15:33:43.461552Z","shell.execute_reply.started":"2023-07-15T15:33:43.383052Z","shell.execute_reply":"2023-07-15T15:33:43.460413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:43.462897Z","iopub.execute_input":"2023-07-15T15:33:43.463230Z","iopub.status.idle":"2023-07-15T15:33:43.479021Z","shell.execute_reply.started":"2023-07-15T15:33:43.463200Z","shell.execute_reply":"2023-07-15T15:33:43.478008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:33:43.480652Z","iopub.execute_input":"2023-07-15T15:33:43.481231Z","iopub.status.idle":"2023-07-15T15:40:38.555564Z","shell.execute_reply.started":"2023-07-15T15:33:43.481198Z","shell.execute_reply":"2023-07-15T15:40:38.554526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:38.557864Z","iopub.execute_input":"2023-07-15T15:40:38.558295Z","iopub.status.idle":"2023-07-15T15:40:38.910357Z","shell.execute_reply.started":"2023-07-15T15:40:38.558253Z","shell.execute_reply":"2023-07-15T15:40:38.908941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test, y_test)\nprint(f'Test Loss: {test_loss}, Test Accuracy: {test_accuracy}')","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:38.911917Z","iopub.execute_input":"2023-07-15T15:40:38.912288Z","iopub.status.idle":"2023-07-15T15:40:40.592872Z","shell.execute_reply.started":"2023-07-15T15:40:38.912257Z","shell.execute_reply":"2023-07-15T15:40:40.592071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:40.594561Z","iopub.execute_input":"2023-07-15T15:40:40.594989Z","iopub.status.idle":"2023-07-15T15:40:42.714014Z","shell.execute_reply.started":"2023-07-15T15:40:40.594947Z","shell.execute_reply":"2023-07-15T15:40:42.712824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = np.reshape(submission.to_numpy(), (len(submission), 28, 28))\nsubmission_data = submission_data / 255.0\nsubmission_data = np.expand_dims(submission_data, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:42.715192Z","iopub.execute_input":"2023-07-15T15:40:42.715525Z","iopub.status.idle":"2023-07-15T15:40:42.792807Z","shell.execute_reply.started":"2023-07-15T15:40:42.715466Z","shell.execute_reply":"2023-07-15T15:40:42.791758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(submission_data)\npredictions","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:42.797442Z","iopub.execute_input":"2023-07-15T15:40:42.797827Z","iopub.status.idle":"2023-07-15T15:40:48.764216Z","shell.execute_reply.started":"2023-07-15T15:40:42.797796Z","shell.execute_reply":"2023-07-15T15:40:48.763033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:48.765606Z","iopub.execute_input":"2023-07-15T15:40:48.766700Z","iopub.status.idle":"2023-07-15T15:40:48.773831Z","shell.execute_reply.started":"2023-07-15T15:40:48.766667Z","shell.execute_reply":"2023-07-15T15:40:48.772576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:48.775186Z","iopub.execute_input":"2023-07-15T15:40:48.775545Z","iopub.status.idle":"2023-07-15T15:40:48.785423Z","shell.execute_reply.started":"2023-07-15T15:40:48.775497Z","shell.execute_reply":"2023-07-15T15:40:48.784340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(data={\"ImageId\": range(1,28001),\"Label\":np.argmax(predictions, axis=1)})\nresult.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T15:40:48.786820Z","iopub.execute_input":"2023-07-15T15:40:48.787183Z","iopub.status.idle":"2023-07-15T15:40:48.862368Z","shell.execute_reply.started":"2023-07-15T15:40:48.787154Z","shell.execute_reply":"2023-07-15T15:40:48.861167Z"},"trusted":true},"execution_count":null,"outputs":[]}]}