{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-03-22T15:48:02.722325Z","iopub.execute_input":"2024-03-22T15:48:02.722663Z","iopub.status.idle":"2024-03-22T15:48:04.530038Z","shell.execute_reply.started":"2024-03-22T15:48:02.722634Z","shell.execute_reply":"2024-03-22T15:48:04.529040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # Suppress TensorFlow warnings\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten, Conv2D, MaxPooling2D","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:50:07.580388Z","iopub.execute_input":"2024-03-22T15:50:07.581602Z","iopub.status.idle":"2024-03-22T15:50:07.586914Z","shell.execute_reply.started":"2024-03-22T15:50:07.581567Z","shell.execute_reply":"2024-03-22T15:50:07.585865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Separate features and labels\nX = train_df.drop(columns=['label']).values.astype('float32')\ny = train_df['label'].values.astype('int32')\nX_test = test_df.values.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:50:34.410064Z","iopub.execute_input":"2024-03-22T15:50:34.410739Z","iopub.status.idle":"2024-03-22T15:50:40.716940Z","shell.execute_reply.started":"2024-03-22T15:50:34.410688Z","shell.execute_reply":"2024-03-22T15:50:40.716083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX /= 255.0\nX_test /= 255.0\n\nX = X.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)\n\ny = to_categorical(y, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:51:34.339385Z","iopub.execute_input":"2024-03-22T15:51:34.339768Z","iopub.status.idle":"2024-03-22T15:51:34.365916Z","shell.execute_reply.started":"2024-03-22T15:51:34.339739Z","shell.execute_reply":"2024-03-22T15:51:34.365150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:51:43.961562Z","iopub.execute_input":"2024-03-22T15:51:43.962266Z","iopub.status.idle":"2024-03-22T15:51:44.300984Z","shell.execute_reply.started":"2024-03-22T15:51:43.962234Z","shell.execute_reply":"2024-03-22T15:51:44.300157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input\n\nmodel = Sequential([\n    Input(shape=(28, 28, 1)),\n    Conv2D(32, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:52:54.526009Z","iopub.execute_input":"2024-03-22T15:52:54.526688Z","iopub.status.idle":"2024-03-22T15:52:54.569083Z","shell.execute_reply.started":"2024-03-22T15:52:54.526653Z","shell.execute_reply":"2024-03-22T15:52:54.568049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:53:08.709814Z","iopub.execute_input":"2024-03-22T15:53:08.710833Z","iopub.status.idle":"2024-03-22T15:53:08.724540Z","shell.execute_reply.started":"2024-03-22T15:53:08.710796Z","shell.execute_reply":"2024-03-22T15:53:08.723545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size=128, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:53:42.206457Z","iopub.execute_input":"2024-03-22T15:53:42.206844Z","iopub.status.idle":"2024-03-22T15:54:02.612603Z","shell.execute_reply.started":"2024-03-22T15:53:42.206813Z","shell.execute_reply":"2024-03-22T15:54:02.611656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:54:19.296494Z","iopub.execute_input":"2024-03-22T15:54:19.296888Z","iopub.status.idle":"2024-03-22T15:54:21.817163Z","shell.execute_reply.started":"2024-03-22T15:54:19.296856Z","shell.execute_reply":"2024-03-22T15:54:21.816182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val)\nprint(f'Validation Loss: {loss:.4f} and Validation Accuracy: {accuracy:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:56:04.759101Z","iopub.execute_input":"2024-03-22T15:56:04.759878Z","iopub.status.idle":"2024-03-22T15:56:05.139549Z","shell.execute_reply.started":"2024-03-22T15:56:04.759844Z","shell.execute_reply":"2024-03-22T15:56:05.138617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'ImageId': range(1, len(predictions) + 1), 'Label': np.argmax(predictions, axis=1)})\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-22T15:56:22.752312Z","iopub.execute_input":"2024-03-22T15:56:22.752669Z","iopub.status.idle":"2024-03-22T15:56:22.798309Z","shell.execute_reply.started":"2024-03-22T15:56:22.752644Z","shell.execute_reply":"2024-03-22T15:56:22.797564Z"},"trusted":true},"execution_count":null,"outputs":[]}]}