{"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-03-11T06:18:27.849311Z","iopub.execute_input":"2022-03-11T06:18:27.850106Z","iopub.status.idle":"2022-03-11T06:18:27.871538Z","shell.execute_reply.started":"2022-03-11T06:18:27.850039Z","shell.execute_reply":"2022-03-11T06:18:27.870386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read Data\nData_train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nData_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_train = Data_train.iloc[:,1:].values\ny_train = Data_train[\"label\"].values\nX_test = Data_test.values\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:27.873781Z","iopub.execute_input":"2022-03-11T06:18:27.874174Z","iopub.status.idle":"2022-03-11T06:18:33.077092Z","shell.execute_reply.started":"2022-03-11T06:18:27.874107Z","shell.execute_reply":"2022-03-11T06:18:33.076193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from  sklearn.preprocessing  import OneHotEncoder\nOE = OneHotEncoder()\ny_train = OE.fit_transform(pd.DataFrame(y_train)).toarray()\ny_train.shape\n\n# Normalize image 0 to 1 \nX_train = X_train/255.0\nX_test = X_test/255.0","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.078831Z","iopub.execute_input":"2022-03-11T06:18:33.079165Z","iopub.status.idle":"2022-03-11T06:18:33.333923Z","shell.execute_reply.started":"2022-03-11T06:18:33.079125Z","shell.execute_reply":"2022-03-11T06:18:33.332921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train.reshape(42000,28,28,1)\nX_test=X_test.reshape(28000,28,28,1)\nprint(X_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.336729Z","iopub.execute_input":"2022-03-11T06:18:33.337064Z","iopub.status.idle":"2022-03-11T06:18:33.344944Z","shell.execute_reply.started":"2022-03-11T06:18:33.337021Z","shell.execute_reply":"2022-03-11T06:18:33.343732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ploting All Images\nfrom matplotlib import pyplot as plt\nfor i in range(1, 11):\n    plt.subplot(2, 5, i)\n    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n    plt.title(np.argmax(y_train[i]))\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.346729Z","iopub.execute_input":"2022-03-11T06:18:33.347352Z","iopub.status.idle":"2022-03-11T06:18:33.806286Z","shell.execute_reply.started":"2022-03-11T06:18:33.347300Z","shell.execute_reply":"2022-03-11T06:18:33.805387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras import Sequential, Model\nfrom keras.layers import Dense, Conv2D, Flatten, BatchNormalization, Activation, MaxPooling2D, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.807830Z","iopub.execute_input":"2022-03-11T06:18:33.808336Z","iopub.status.idle":"2022-03-11T06:18:33.814273Z","shell.execute_reply.started":"2022-03-11T06:18:33.808294Z","shell.execute_reply":"2022-03-11T06:18:33.812824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Model to identify images labels\nModel = Sequential([\n    Conv2D(32, kernel_size = 3, activation = 'relu', input_shape=(28,28,1)),\n    BatchNormalization(),\n    Conv2D(32, kernel_size = 3, activation = 'relu'),\n    BatchNormalization(),\n    MaxPooling2D(),\n    Conv2D(32, kernel_size = 5, strides = 2, padding = 'same', activation = 'relu'),\n    BatchNormalization(),\n    \n    # add dropout to prevent overfitting\n    Dropout(0.3),\n    \n    \n    Conv2D(64, kernel_size = 3, activation = 'relu'),\n    BatchNormalization(),\n    Conv2D(64, kernel_size = 3, activation = 'relu'),\n    BatchNormalization(),\n    Conv2D(64, kernel_size = 5, strides = 2, padding = 'same', activation = 'relu'),\n    BatchNormalization(),\n    \n    # add dropout to prevent overfitting\n    Dropout(0.3),\n    \n    Flatten(),\n    Dense(128, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.25),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.816449Z","iopub.execute_input":"2022-03-11T06:18:33.816901Z","iopub.status.idle":"2022-03-11T06:18:33.997254Z","shell.execute_reply.started":"2022-03-11T06:18:33.816857Z","shell.execute_reply":"2022-03-11T06:18:33.996429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:33.998637Z","iopub.execute_input":"2022-03-11T06:18:33.998942Z","iopub.status.idle":"2022-03-11T06:18:34.021995Z","shell.execute_reply.started":"2022-03-11T06:18:33.998897Z","shell.execute_reply":"2022-03-11T06:18:34.021153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Complie the model\nModel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:34.024261Z","iopub.execute_input":"2022-03-11T06:18:34.024848Z","iopub.status.idle":"2022-03-11T06:18:34.037244Z","shell.execute_reply.started":"2022-03-11T06:18:34.024807Z","shell.execute_reply":"2022-03-11T06:18:34.036057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.fit(X_train, y_train, batch_size=60, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T06:18:34.041207Z","iopub.execute_input":"2022-03-11T06:18:34.041539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = Model.evaluate(X_train, y_train)\nprint(f\"Loss: {metrics[0]}\")\nprint(f\"Accuracy: {metrics[1]}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_test = Model.predict(X_test)\ny_pred_test = np.argmax(y_pred_test,axis = 1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission[\"Label\"] = y_pred_test\nsubmission.to_csv('submission.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}