{"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":"2022-01-13T09:53:08.98103Z","iopub.execute_input":"2022-01-13T09:53:08.981575Z","iopub.status.idle":"2022-01-13T09:53:09.009987Z","shell.execute_reply.started":"2022-01-13T09:53:08.981462Z","shell.execute_reply":"2022-01-13T09:53:09.008174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tensorflow\nfrom tensorflow import keras\n","metadata":{"execution":{"iopub.status.busy":"2022-01-13T09:54:18.987284Z","iopub.execute_input":"2022-01-13T09:54:18.987544Z","iopub.status.idle":"2022-01-13T09:54:23.543933Z","shell.execute_reply.started":"2022-01-13T09:54:18.987496Z","shell.execute_reply":"2022-01-13T09:54:23.542806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsample_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:52:47.298789Z","iopub.execute_input":"2022-01-13T11:52:47.299045Z","iopub.status.idle":"2022-01-13T11:52:50.838666Z","shell.execute_reply.started":"2022-01-13T11:52:47.299021Z","shell.execute_reply":"2022-01-13T11:52:50.83707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:16:14.155854Z","iopub.execute_input":"2022-01-13T10:16:14.156005Z","iopub.status.idle":"2022-01-13T10:16:14.170655Z","shell.execute_reply.started":"2022-01-13T10:16:14.155986Z","shell.execute_reply":"2022-01-13T10:16:14.169571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:16:14.171551Z","iopub.execute_input":"2022-01-13T10:16:14.171717Z","iopub.status.idle":"2022-01-13T10:16:14.181066Z","shell.execute_reply.started":"2022-01-13T10:16:14.171696Z","shell.execute_reply":"2022-01-13T10:16:14.180129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:16:14.182925Z","iopub.execute_input":"2022-01-13T10:16:14.183817Z","iopub.status.idle":"2022-01-13T10:16:14.194072Z","shell.execute_reply.started":"2022-01-13T10:16:14.183785Z","shell.execute_reply":"2022-01-13T10:16:14.193431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(['label'],axis=1 )","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:16:14.195276Z","iopub.execute_input":"2022-01-13T10:16:14.195674Z","iopub.status.idle":"2022-01-13T10:16:14.246542Z","shell.execute_reply.started":"2022-01-13T10:16:14.195634Z","shell.execute_reply":"2022-01-13T10:16:14.245531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:16:16.805606Z","iopub.execute_input":"2022-01-13T10:16:16.805934Z","iopub.status.idle":"2022-01-13T10:16:16.810207Z","shell.execute_reply.started":"2022-01-13T10:16:16.805889Z","shell.execute_reply":"2022-01-13T10:16:16.809293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.iloc[0].array.reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:30:35.29868Z","iopub.execute_input":"2022-01-13T10:30:35.298925Z","iopub.status.idle":"2022-01-13T10:30:35.309405Z","shell.execute_reply.started":"2022-01-13T10:30:35.298901Z","shell.execute_reply":"2022-01-13T10:30:35.308699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X /255","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:30:28.340902Z","iopub.execute_input":"2022-01-13T10:30:28.341153Z","iopub.status.idle":"2022-01-13T10:30:28.392111Z","shell.execute_reply.started":"2022-01-13T10:30:28.341129Z","shell.execute_reply":"2022-01-13T10:30:28.391579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_sample(X , index):\n    plt.matshow(X.iloc[index].array.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:42:29.018646Z","iopub.execute_input":"2022-01-13T10:42:29.018861Z","iopub.status.idle":"2022-01-13T10:42:29.023362Z","shell.execute_reply.started":"2022-01-13T10:42:29.018837Z","shell.execute_reply":"2022-01-13T10:42:29.022767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[10]","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:18:45.891056Z","iopub.execute_input":"2022-01-13T10:18:45.891278Z","iopub.status.idle":"2022-01-13T10:18:45.896251Z","shell.execute_reply.started":"2022-01-13T10:18:45.891254Z","shell.execute_reply":"2022-01-13T10:18:45.895879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train ,X_test, y_train, y_test = train_test_split(X,y,test_size=0.33,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T10:30:48.407808Z","iopub.execute_input":"2022-01-13T10:30:48.408129Z","iopub.status.idle":"2022-01-13T10:30:48.649491Z","shell.execute_reply.started":"2022-01-13T10:30:48.408089Z","shell.execute_reply":"2022-01-13T10:30:48.649007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model","metadata":{}},{"cell_type":"code","source":"X_train = X_train.values.reshape(28140,28,28)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:16:31.912071Z","iopub.execute_input":"2022-01-13T11:16:31.912377Z","iopub.status.idle":"2022-01-13T11:16:31.917244Z","shell.execute_reply.started":"2022-01-13T11:16:31.912344Z","shell.execute_reply":"2022-01-13T11:16:31.916395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.values.reshape(13860,28,28)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:17:55.051898Z","iopub.execute_input":"2022-01-13T11:17:55.052167Z","iopub.status.idle":"2022-01-13T11:17:55.058389Z","shell.execute_reply.started":"2022-01-13T11:17:55.05214Z","shell.execute_reply":"2022-01-13T11:17:55.056956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:18:05.53143Z","iopub.execute_input":"2022-01-13T11:18:05.531683Z","iopub.status.idle":"2022-01-13T11:18:05.538099Z","shell.execute_reply.started":"2022-01-13T11:18:05.531659Z","shell.execute_reply":"2022-01-13T11:18:05.537547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import  layers, models","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:20:37.671839Z","iopub.execute_input":"2022-01-13T11:20:37.672083Z","iopub.status.idle":"2022-01-13T11:20:37.677515Z","shell.execute_reply.started":"2022-01-13T11:20:37.672058Z","shell.execute_reply":"2022-01-13T11:20:37.677011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = models.Sequential([\n        # cnn\n        layers.Conv1D(filters=32 , kernel_size=3, activation='relu', input_shape=(28,28)),\n        layers.MaxPooling1D(2),\n\n        layers.Conv1D(filters=64 , kernel_size=3, activation='relu'),\n        layers.MaxPooling1D(2),\n        # ann\n        layers.Flatten(),\n        layers.Dense(64, activation='relu'),\n        layers.Dense(10, activation='softmax')    \n    ])","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:32:24.453175Z","iopub.execute_input":"2022-01-13T11:32:24.453909Z","iopub.status.idle":"2022-01-13T11:32:24.524506Z","shell.execute_reply.started":"2022-01-13T11:32:24.453861Z","shell.execute_reply":"2022-01-13T11:32:24.523361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:32:26.080813Z","iopub.execute_input":"2022-01-13T11:32:26.081249Z","iopub.status.idle":"2022-01-13T11:32:26.091807Z","shell.execute_reply.started":"2022-01-13T11:32:26.081206Z","shell.execute_reply":"2022-01-13T11:32:26.09051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train,y_train,epochs=100)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:33:34.990916Z","iopub.execute_input":"2022-01-13T11:33:34.991391Z","iopub.status.idle":"2022-01-13T11:40:19.864457Z","shell.execute_reply.started":"2022-01-13T11:33:34.991358Z","shell.execute_reply":"2022-01-13T11:40:19.863669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:52:18.744715Z","iopub.execute_input":"2022-01-13T11:52:18.745002Z","iopub.status.idle":"2022-01-13T11:52:20.788395Z","shell.execute_reply.started":"2022-01-13T11:52:18.744975Z","shell.execute_reply":"2022-01-13T11:52:20.78794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:53:02.581004Z","iopub.execute_input":"2022-01-13T11:53:02.581475Z","iopub.status.idle":"2022-01-13T11:53:02.620856Z","shell.execute_reply.started":"2022-01-13T11:53:02.581446Z","shell.execute_reply":"2022-01-13T11:53:02.619477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.values.reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:54:09.551049Z","iopub.execute_input":"2022-01-13T11:54:09.551355Z","iopub.status.idle":"2022-01-13T11:54:09.55759Z","shell.execute_reply.started":"2022-01-13T11:54:09.551318Z","shell.execute_reply":"2022-01-13T11:54:09.556593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction","metadata":{}},{"cell_type":"code","source":"y_pred = cnn.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:54:24.968655Z","iopub.execute_input":"2022-01-13T11:54:24.968947Z","iopub.status.idle":"2022-01-13T11:54:28.700532Z","shell.execute_reply.started":"2022-01-13T11:54:24.968918Z","shell.execute_reply":"2022-01-13T11:54:28.69898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = [0,1,2,3,4,5,6,7,8,9]","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:54:36.681699Z","iopub.execute_input":"2022-01-13T11:54:36.682044Z","iopub.status.idle":"2022-01-13T11:54:36.688911Z","shell.execute_reply.started":"2022-01-13T11:54:36.68201Z","shell.execute_reply":"2022-01-13T11:54:36.68748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_classes = [ classes[np.argmax(element)] for element in y_pred]","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:55:12.628304Z","iopub.execute_input":"2022-01-13T11:55:12.628837Z","iopub.status.idle":"2022-01-13T11:55:12.730092Z","shell.execute_reply.started":"2022-01-13T11:55:12.628804Z","shell.execute_reply":"2022-01-13T11:55:12.728781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['Label'] = y_classes","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:55:15.586907Z","iopub.execute_input":"2022-01-13T11:55:15.587222Z","iopub.status.idle":"2022-01-13T11:55:15.600707Z","shell.execute_reply.started":"2022-01-13T11:55:15.58719Z","shell.execute_reply":"2022-01-13T11:55:15.599158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submision3',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-13T11:55:21.959097Z","iopub.execute_input":"2022-01-13T11:55:21.959413Z","iopub.status.idle":"2022-01-13T11:55:22.021234Z","shell.execute_reply.started":"2022-01-13T11:55:21.95938Z","shell.execute_reply":"2022-01-13T11:55:22.020115Z"},"trusted":true},"execution_count":null,"outputs":[]}]}