{"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-08-12T10:04:31.361007Z","iopub.execute_input":"2022-08-12T10:04:31.361538Z","iopub.status.idle":"2022-08-12T10:04:31.370804Z","shell.execute_reply.started":"2022-08-12T10:04:31.361500Z","shell.execute_reply":"2022-08-12T10:04:31.369585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(pd.read_csv(\"../input/digit-recognizer/test.csv\"))\ntrain = np.array(pd.read_csv(\"../input/digit-recognizer/train.csv\"))\ntrain2=pd.DataFrame(pd.read_csv(\"../input/digit-recognizer/train.csv\"))\ntest2=pd.DataFrame(pd.read_csv(\"../input/digit-recognizer/test.csv\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:04:32.911572Z","iopub.execute_input":"2022-08-12T10:04:32.912326Z","iopub.status.idle":"2022-08-12T10:04:42.847812Z","shell.execute_reply.started":"2022-08-12T10:04:32.912270Z","shell.execute_reply":"2022-08-12T10:04:42.846751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1-Getting the data ready**","metadata":{}},{"cell_type":"code","source":"image_id=train2.index\ny=train2.label\nX=train2.drop('label',axis=1)\nx_test=test","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:04:46.402595Z","iopub.execute_input":"2022-08-12T10:04:46.403085Z","iopub.status.idle":"2022-08-12T10:04:46.603816Z","shell.execute_reply.started":"2022-08-12T10:04:46.403051Z","shell.execute_reply":"2022-08-12T10:04:46.602098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaler\nX = X/255\nx_test=x_test/255","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:04:48.856702Z","iopub.execute_input":"2022-08-12T10:04:48.857249Z","iopub.status.idle":"2022-08-12T10:04:49.021403Z","shell.execute_reply.started":"2022-08-12T10:04:48.857206Z","shell.execute_reply":"2022-08-12T10:04:49.020178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import mnist","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:04:51.527038Z","iopub.execute_input":"2022-08-12T10:04:51.527475Z","iopub.status.idle":"2022-08-12T10:05:01.306805Z","shell.execute_reply.started":"2022-08-12T10:04:51.527442Z","shell.execute_reply":"2022-08-12T10:05:01.305477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3-Reshape the data to a (28,28) matrix**","metadata":{}},{"cell_type":"code","source":"x_train=np.array(X);X.shape\nx_train=x_train.reshape(x_train.shape[0],28,28,1)\nx_test=x_test.reshape(x_test.shape[0],28,28,1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:03.241342Z","iopub.execute_input":"2022-08-12T10:05:03.242325Z","iopub.status.idle":"2022-08-12T10:05:03.503992Z","shell.execute_reply.started":"2022-08-12T10:05:03.242269Z","shell.execute_reply":"2022-08-12T10:05:03.502119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=tf.keras.utils.to_categorical(y);y.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:29.089731Z","iopub.execute_input":"2022-08-12T10:05:29.090255Z","iopub.status.idle":"2022-08-12T10:05:29.102355Z","shell.execute_reply.started":"2022-08-12T10:05:29.090194Z","shell.execute_reply":"2022-08-12T10:05:29.100912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**4-Let's see the images and check if everything is fine**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:42.551230Z","iopub.execute_input":"2022-08-12T10:05:42.551780Z","iopub.status.idle":"2022-08-12T10:05:42.557881Z","shell.execute_reply.started":"2022-08-12T10:05:42.551721Z","shell.execute_reply":"2022-08-12T10:05:42.556562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_test[562,:,:,0])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:44.815272Z","iopub.execute_input":"2022-08-12T10:05:44.815717Z","iopub.status.idle":"2022-08-12T10:05:45.028259Z","shell.execute_reply.started":"2022-08-12T10:05:44.815676Z","shell.execute_reply":"2022-08-12T10:05:45.026450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**5-Import Keras to use keras sequential method**","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:51.614956Z","iopub.execute_input":"2022-08-12T10:05:51.615372Z","iopub.status.idle":"2022-08-12T10:05:51.621542Z","shell.execute_reply.started":"2022-08-12T10:05:51.615336Z","shell.execute_reply":"2022-08-12T10:05:51.620234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\nConv2D(32, kernel_size=(3,3), activation='relu',input_shape=(28,28,1)),\nConv2D(64, (3,3), activation='relu'),\nMaxPool2D(pool_size=(2,2)),\nDropout(0.25),\nFlatten(),\nDense(125,activation='relu'),\nDropout(0.25),\nDense(10,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:53.079222Z","iopub.execute_input":"2022-08-12T10:05:53.080380Z","iopub.status.idle":"2022-08-12T10:05:53.280833Z","shell.execute_reply.started":"2022-08-12T10:05:53.080337Z","shell.execute_reply":"2022-08-12T10:05:53.279022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam', loss='categorical_crossentropy',\n                  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:05:58.846105Z","iopub.execute_input":"2022-08-12T10:05:58.846630Z","iopub.status.idle":"2022-08-12T10:05:58.858825Z","shell.execute_reply.started":"2022-08-12T10:05:58.846585Z","shell.execute_reply":"2022-08-12T10:05:58.857768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x_train, y,\n    batch_size=128,epochs=10,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:06:00.855916Z","iopub.execute_input":"2022-08-12T10:06:00.856643Z","iopub.status.idle":"2022-08-12T10:12:18.942067Z","shell.execute_reply.started":"2022-08-12T10:06:00.856604Z","shell.execute_reply":"2022-08-12T10:12:18.940712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:25.847580Z","iopub.execute_input":"2022-08-12T10:17:25.848033Z","iopub.status.idle":"2022-08-12T10:17:34.601871Z","shell.execute_reply.started":"2022-08-12T10:17:25.847998Z","shell.execute_reply":"2022-08-12T10:17:34.600742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = np.argmax(prediction,axis = 1);y_test\n#pd.DataFrame(prediction)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:47.086140Z","iopub.execute_input":"2022-08-12T10:21:47.086545Z","iopub.status.idle":"2022-08-12T10:21:47.095964Z","shell.execute_reply.started":"2022-08-12T10:21:47.086512Z","shell.execute_reply":"2022-08-12T10:21:47.094096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Scored  0.98860 with the Keras Sequential**","metadata":{}},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),pd.Series(y_test, name ='Label')],axis = 1);submission\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:54:26.122520Z","iopub.execute_input":"2022-08-12T10:54:26.122926Z","iopub.status.idle":"2022-08-12T10:54:26.162760Z","shell.execute_reply.started":"2022-08-12T10:54:26.122893Z","shell.execute_reply":"2022-08-12T10:54:26.161753Z"},"trusted":true},"execution_count":null,"outputs":[]}]}