{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-04-24T20:03:54.497384Z","iopub.execute_input":"2024-04-24T20:03:54.497873Z","iopub.status.idle":"2024-04-24T20:03:55.876002Z","shell.execute_reply.started":"2024-04-24T20:03:54.497830Z","shell.execute_reply":"2024-04-24T20:03:55.873897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import OneHotEncoder\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:03:55.883392Z","iopub.execute_input":"2024-04-24T20:03:55.884279Z","iopub.status.idle":"2024-04-24T20:04:14.844015Z","shell.execute_reply.started":"2024-04-24T20:03:55.884232Z","shell.execute_reply":"2024-04-24T20:04:14.842567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:04:36.988962Z","iopub.execute_input":"2024-04-24T20:04:36.989836Z","iopub.status.idle":"2024-04-24T20:04:44.421091Z","shell.execute_reply.started":"2024-04-24T20:04:36.989786Z","shell.execute_reply":"2024-04-24T20:04:44.419279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:04:44.423934Z","iopub.execute_input":"2024-04-24T20:04:44.424404Z","iopub.status.idle":"2024-04-24T20:04:44.460848Z","shell.execute_reply.started":"2024-04-24T20:04:44.424366Z","shell.execute_reply":"2024-04-24T20:04:44.458919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:04:44.463661Z","iopub.execute_input":"2024-04-24T20:04:44.464295Z","iopub.status.idle":"2024-04-24T20:04:44.487929Z","shell.execute_reply.started":"2024-04-24T20:04:44.464243Z","shell.execute_reply":"2024-04-24T20:04:44.485873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting Data","metadata":{}},{"cell_type":"code","source":"X_train = train_data.drop(columns=['label']).values.astype(np.float32)\ny_train = train_data['label'].values.astype(np.int32)\nX_test = test_data.values.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:05:04.114576Z","iopub.execute_input":"2024-04-24T20:05:04.115071Z","iopub.status.idle":"2024-04-24T20:05:04.402829Z","shell.execute_reply.started":"2024-04-24T20:05:04.115037Z","shell.execute_reply":"2024-04-24T20:05:04.401471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1) \nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:05:47.618408Z","iopub.execute_input":"2024-04-24T20:05:47.618892Z","iopub.status.idle":"2024-04-24T20:05:47.625355Z","shell.execute_reply.started":"2024-04-24T20:05:47.618860Z","shell.execute_reply":"2024-04-24T20:05:47.623750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = OneHotEncoder(categories='auto')\ny_train_encoded = encoder.fit_transform(y_train.reshape(-1, 1)).toarray()","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:05:55.945797Z","iopub.execute_input":"2024-04-24T20:05:55.946269Z","iopub.status.idle":"2024-04-24T20:05:55.971147Z","shell.execute_reply.started":"2024-04-24T20:05:55.946236Z","shell.execute_reply":"2024-04-24T20:05:55.969849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train_encoded, y_val = train_test_split(X_train, y_train_encoded, test_size=0.1, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:06:03.307517Z","iopub.execute_input":"2024-04-24T20:06:03.307957Z","iopub.status.idle":"2024-04-24T20:06:03.918614Z","shell.execute_reply.started":"2024-04-24T20:06:03.307926Z","shell.execute_reply":"2024-04-24T20:06:03.917533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.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(128, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:06:19.320071Z","iopub.execute_input":"2024-04-24T20:06:19.320546Z","iopub.status.idle":"2024-04-24T20:06:19.430383Z","shell.execute_reply.started":"2024-04-24T20:06:19.320513Z","shell.execute_reply":"2024-04-24T20:06:19.429141Z"},"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":"2024-04-24T20:06:26.281341Z","iopub.execute_input":"2024-04-24T20:06:26.281906Z","iopub.status.idle":"2024-04-24T20:06:26.302696Z","shell.execute_reply.started":"2024-04-24T20:06:26.281867Z","shell.execute_reply":"2024-04-24T20:06:26.300859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train_encoded, epochs=10, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:06:33.853195Z","iopub.execute_input":"2024-04-24T20:06:33.854391Z","iopub.status.idle":"2024-04-24T20:12:10.755378Z","shell.execute_reply.started":"2024-04-24T20:06:33.854345Z","shell.execute_reply":"2024-04-24T20:12:10.753536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Saving Model Weights","metadata":{}},{"cell_type":"code","source":"model.save_weights('/kaggle/working/model_weights.weights.h5')","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:13:02.618867Z","iopub.execute_input":"2024-04-24T20:13:02.619614Z","iopub.status.idle":"2024-04-24T20:13:02.677628Z","shell.execute_reply.started":"2024-04-24T20:13:02.619580Z","shell.execute_reply":"2024-04-24T20:13:02.676435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predicting and Saving .csv","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredicted_labels = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:21:05.617247Z","iopub.execute_input":"2024-04-24T20:21:05.617778Z","iopub.status.idle":"2024-04-24T20:21:11.436703Z","shell.execute_reply.started":"2024-04-24T20:21:05.617742Z","shell.execute_reply":"2024-04-24T20:21:11.435314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'ImageId': range(1, len(predicted_labels) + 1), 'Label': predicted_labels})","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:21:11.438729Z","iopub.execute_input":"2024-04-24T20:21:11.439093Z","iopub.status.idle":"2024-04-24T20:21:11.445896Z","shell.execute_reply.started":"2024-04-24T20:21:11.439063Z","shell.execute_reply":"2024-04-24T20:21:11.444088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T20:21:11.447538Z","iopub.execute_input":"2024-04-24T20:21:11.448347Z","iopub.status.idle":"2024-04-24T20:21:11.502999Z","shell.execute_reply.started":"2024-04-24T20:21:11.448298Z","shell.execute_reply":"2024-04-24T20:21:11.501822Z"},"trusted":true},"execution_count":null,"outputs":[]}]}