{"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-03-09T14:48:27.045579Z","iopub.execute_input":"2023-03-09T14:48:27.045991Z","iopub.status.idle":"2023-03-09T14:48:27.080642Z","shell.execute_reply.started":"2023-03-09T14:48:27.045951Z","shell.execute_reply":"2023-03-09T14:48:27.079814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-09T14:53:42.190864Z","iopub.execute_input":"2023-03-09T14:53:42.191359Z","iopub.status.idle":"2023-03-09T14:53:44.771879Z","shell.execute_reply.started":"2023-03-09T14:53:42.191316Z","shell.execute_reply":"2023-03-09T14:53:44.770491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-09T14:54:00.400011Z","iopub.execute_input":"2023-03-09T14:54:00.401404Z","iopub.status.idle":"2023-03-09T14:54:00.422583Z","shell.execute_reply.started":"2023-03-09T14:54:00.401364Z","shell.execute_reply":"2023-03-09T14:54:00.421439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_data.drop(labels = [\"label\"],axis = 1) \ny_train = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:20:44.324479Z","iopub.execute_input":"2023-03-09T15:20:44.324835Z","iopub.status.idle":"2023-03-09T15:20:44.368716Z","shell.execute_reply.started":"2023-03-09T15:20:44.324804Z","shell.execute_reply":"2023-03-09T15:20:44.367717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest_data = test_data / 255.0\nX_train = X_train.values.reshape(-1,28,28,1) # (number of samples, 28, 28, 1) 1 -> number of channels\ntest_data = test_data.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:20:49.249095Z","iopub.execute_input":"2023-03-09T15:20:49.249934Z","iopub.status.idle":"2023-03-09T15:20:49.316055Z","shell.execute_reply.started":"2023-03-09T15:20:49.249900Z","shell.execute_reply":"2023-03-09T15:20:49.315214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:20:53.019210Z","iopub.execute_input":"2023-03-09T15:20:53.019600Z","iopub.status.idle":"2023-03-09T15:20:53.026438Z","shell.execute_reply.started":"2023-03-09T15:20:53.019571Z","shell.execute_reply":"2023-03-09T15:20:53.025271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:20:54.980230Z","iopub.execute_input":"2023-03-09T15:20:54.980644Z","iopub.status.idle":"2023-03-09T15:20:54.985895Z","shell.execute_reply.started":"2023-03-09T15:20:54.980613Z","shell.execute_reply":"2023-03-09T15:20:54.985226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:22:48.590840Z","iopub.execute_input":"2023-03-09T15:22:48.591196Z","iopub.status.idle":"2023-03-09T15:22:57.710017Z","shell.execute_reply.started":"2023-03-09T15:22:48.591164Z","shell.execute_reply":"2023-03-09T15:22:57.708481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:29:25.898889Z","iopub.execute_input":"2023-03-09T15:29:25.899600Z","iopub.status.idle":"2023-03-09T15:29:25.905091Z","shell.execute_reply.started":"2023-03-09T15:29:25.899564Z","shell.execute_reply":"2023-03-09T15:29:25.904399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.3)\nprint(y_train.shape)\nprint(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:29:46.093729Z","iopub.execute_input":"2023-03-09T15:29:46.094133Z","iopub.status.idle":"2023-03-09T15:29:46.430483Z","shell.execute_reply.started":"2023-03-09T15:29:46.094099Z","shell.execute_reply":"2023-03-09T15:29:46.429416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu')) # 32 feature maps, with the size of 5×5 \nmodel.add(MaxPool2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\na = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=5, batch_size=200, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:29:53.611858Z","iopub.execute_input":"2023-03-09T15:29:53.612237Z","iopub.status.idle":"2023-03-09T15:30:35.705612Z","shell.execute_reply.started":"2023-03-09T15:29:53.612198Z","shell.execute_reply":"2023-03-09T15:30:35.704169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_data)\ny_pred = np.argmax(y_pred,axis = 1)\ny_pred = pd.Series(y_pred,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),y_pred],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:36:36.027109Z","iopub.execute_input":"2023-03-09T15:36:36.028382Z","iopub.status.idle":"2023-03-09T15:36:40.259919Z","shell.execute_reply.started":"2023-03-09T15:36:36.028343Z","shell.execute_reply":"2023-03-09T15:36:40.258353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-09T15:36:48.513650Z","iopub.execute_input":"2023-03-09T15:36:48.514001Z","iopub.status.idle":"2023-03-09T15:36:48.523508Z","shell.execute_reply.started":"2023-03-09T15:36:48.513966Z","shell.execute_reply":"2023-03-09T15:36:48.522503Z"},"trusted":true},"execution_count":null,"outputs":[]}]}