{"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":"2021-05-27T18:59:10.836924Z","iopub.execute_input":"2021-05-27T18:59:10.837561Z","iopub.status.idle":"2021-05-27T18:59:10.851147Z","shell.execute_reply.started":"2021-05-27T18:59:10.837468Z","shell.execute_reply":"2021-05-27T18:59:10.849931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:10.853003Z","iopub.execute_input":"2021-05-27T18:59:10.853556Z","iopub.status.idle":"2021-05-27T18:59:11.766148Z","shell.execute_reply.started":"2021-05-27T18:59:10.853476Z","shell.execute_reply":"2021-05-27T18:59:11.765119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:11.768335Z","iopub.execute_input":"2021-05-27T18:59:11.768767Z","iopub.status.idle":"2021-05-27T18:59:15.587543Z","shell.execute_reply.started":"2021-05-27T18:59:11.768721Z","shell.execute_reply":"2021-05-27T18:59:15.586391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/digit-recognizer/test.csv')\ntest=test.rename(columns = {0:'new_name'})\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:15.589208Z","iopub.execute_input":"2021-05-27T18:59:15.589517Z","iopub.status.idle":"2021-05-27T18:59:17.822422Z","shell.execute_reply.started":"2021-05-27T18:59:15.589486Z","shell.execute_reply":"2021-05-27T18:59:17.821274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:17.823876Z","iopub.execute_input":"2021-05-27T18:59:17.824318Z","iopub.status.idle":"2021-05-27T18:59:17.879439Z","shell.execute_reply.started":"2021-05-27T18:59:17.824272Z","shell.execute_reply":"2021-05-27T18:59:17.878349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train['label']\nx_train = train.drop('label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:17.882015Z","iopub.execute_input":"2021-05-27T18:59:17.882709Z","iopub.status.idle":"2021-05-27T18:59:17.981020Z","shell.execute_reply.started":"2021-05-27T18:59:17.882656Z","shell.execute_reply":"2021-05-27T18:59:17.980104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:17.983611Z","iopub.execute_input":"2021-05-27T18:59:17.984232Z","iopub.status.idle":"2021-05-27T18:59:18.009892Z","shell.execute_reply.started":"2021-05-27T18:59:17.984178Z","shell.execute_reply":"2021-05-27T18:59:18.008813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\n\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:18.011452Z","iopub.execute_input":"2021-05-27T18:59:18.011896Z","iopub.status.idle":"2021-05-27T18:59:18.030911Z","shell.execute_reply.started":"2021-05-27T18:59:18.011863Z","shell.execute_reply":"2021-05-27T18:59:18.029690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preprocessing input data","metadata":{}},{"cell_type":"code","source":"x_train = x_train.astype('float32')\ntest = test.astype('float32')\nx_train = x_train/225.0\ntest = test/225.0\nx_train = x_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:18.032423Z","iopub.execute_input":"2021-05-27T18:59:18.032922Z","iopub.status.idle":"2021-05-27T18:59:18.289063Z","shell.execute_reply.started":"2021-05-27T18:59:18.032881Z","shell.execute_reply":"2021-05-27T18:59:18.287948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:18.290563Z","iopub.execute_input":"2021-05-27T18:59:18.291002Z","iopub.status.idle":"2021-05-27T18:59:18.296995Z","shell.execute_reply.started":"2021-05-27T18:59:18.290954Z","shell.execute_reply":"2021-05-27T18:59:18.295850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(4,4, figsize=(15, 15))\naxes = axes.ravel()\n\nfor i in np.arange(0, 16):\n    axes[i].imshow(x_train[i])\n    axes[i].set_title(\"Digit = %s\" % y_train[i])\n    axes[i].axis(\"off\")\n    plt.subplots_adjust(wspace=1)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:18.298107Z","iopub.execute_input":"2021-05-27T18:59:18.298373Z","iopub.status.idle":"2021-05-27T18:59:19.264151Z","shell.execute_reply.started":"2021-05-27T18:59:18.298347Z","shell.execute_reply":"2021-05-27T18:59:19.263195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Preprocessing Labels","metadata":{}},{"cell_type":"code","source":"print(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:19.265281Z","iopub.execute_input":"2021-05-27T18:59:19.265766Z","iopub.status.idle":"2021-05-27T18:59:19.270619Z","shell.execute_reply.started":"2021-05-27T18:59:19.265723Z","shell.execute_reply":"2021-05-27T18:59:19.269449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:10]","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:19.271973Z","iopub.execute_input":"2021-05-27T18:59:19.272378Z","iopub.status.idle":"2021-05-27T18:59:19.285218Z","shell.execute_reply.started":"2021-05-27T18:59:19.272298Z","shell.execute_reply":"2021-05-27T18:59:19.283841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\ny_train = np_utils.to_categorical(y_train,10)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:19.287010Z","iopub.execute_input":"2021-05-27T18:59:19.287560Z","iopub.status.idle":"2021-05-27T18:59:25.118370Z","shell.execute_reply.started":"2021-05-27T18:59:19.287515Z","shell.execute_reply":"2021-05-27T18:59:25.117312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.120102Z","iopub.execute_input":"2021-05-27T18:59:25.120553Z","iopub.status.idle":"2021-05-27T18:59:25.126841Z","shell.execute_reply.started":"2021-05-27T18:59:25.120485Z","shell.execute_reply":"2021-05-27T18:59:25.125613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Spliting Data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx1, x_val, y1, y_val = train_test_split(x_train,y_train, test_size = 0.3, random_state = 12)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.128399Z","iopub.execute_input":"2021-05-27T18:59:25.128843Z","iopub.status.idle":"2021-05-27T18:59:25.785377Z","shell.execute_reply.started":"2021-05-27T18:59:25.128808Z","shell.execute_reply":"2021-05-27T18:59:25.784260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x1.shape)\nprint(x_val.shape)\nprint(y1.shape)\nprint(y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.786775Z","iopub.execute_input":"2021-05-27T18:59:25.787134Z","iopub.status.idle":"2021-05-27T18:59:25.794707Z","shell.execute_reply.started":"2021-05-27T18:59:25.787097Z","shell.execute_reply":"2021-05-27T18:59:25.793324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Defining Model Architecture","metadata":{}},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.optimizers import SGD\nfrom keras.layers import Flatten\nfrom keras.layers import Dense \nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import AveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.layers import BatchNormalization\nfrom keras.layers import Dropout \nfrom keras.layers import Activation","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.796011Z","iopub.execute_input":"2021-05-27T18:59:25.796342Z","iopub.status.idle":"2021-05-27T18:59:25.809274Z","shell.execute_reply.started":"2021-05-27T18:59:25.796311Z","shell.execute_reply":"2021-05-27T18:59:25.808185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = Sequential()\nmodel1.add(Conv2D(16, (3,3), activation = 'relu', kernel_initializer = 'he_uniform', input_shape = (28,28,1)))\nmodel1.add(MaxPooling2D((2,2)))\nmodel1.add(Conv2D(32, (3,3), activation = 'relu', kernel_initializer = 'he_uniform'))\nmodel1.add(MaxPooling2D((2,2)))\nmodel1.add(Flatten())\nmodel1.add(Dense(100, activation = 'relu', kernel_initializer = 'he_uniform'))\nmodel1.add(Dense(64, activation = 'relu', kernel_initializer = 'he_uniform'))\nmodel1.add(Dense(10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.812239Z","iopub.execute_input":"2021-05-27T18:59:25.812582Z","iopub.status.idle":"2021-05-27T18:59:25.950415Z","shell.execute_reply.started":"2021-05-27T18:59:25.812534Z","shell.execute_reply":"2021-05-27T18:59:25.949297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compile Model","metadata":{}},{"cell_type":"code","source":"model1.compile(optimizer = Adam(), loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.952179Z","iopub.execute_input":"2021-05-27T18:59:25.952478Z","iopub.status.idle":"2021-05-27T18:59:25.970759Z","shell.execute_reply.started":"2021-05-27T18:59:25.952449Z","shell.execute_reply":"2021-05-27T18:59:25.969673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fit model on training data","metadata":{}},{"cell_type":"code","source":"history1 = []                \nhistory1 = model1.fit(x1, y1, batch_size=32, epochs = 10, validation_data=(x_val, y_val),  callbacks=[history1])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T18:59:25.972352Z","iopub.execute_input":"2021-05-27T18:59:25.973033Z","iopub.status.idle":"2021-05-27T19:01:32.357637Z","shell.execute_reply.started":"2021-05-27T18:59:25.972976Z","shell.execute_reply":"2021-05-27T19:01:32.356524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytest1 = model1.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:32.359846Z","iopub.execute_input":"2021-05-27T19:01:32.360181Z","iopub.status.idle":"2021-05-27T19:01:35.444819Z","shell.execute_reply.started":"2021-05-27T19:01:32.360148Z","shell.execute_reply":"2021-05-27T19:01:35.443770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(ytest1, axis=1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:35.446659Z","iopub.execute_input":"2021-05-27T19:01:35.447142Z","iopub.status.idle":"2021-05-27T19:01:35.456516Z","shell.execute_reply.started":"2021-05-27T19:01:35.447070Z","shell.execute_reply":"2021-05-27T19:01:35.455435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:35.458065Z","iopub.execute_input":"2021-05-27T19:01:35.458448Z","iopub.status.idle":"2021-05-27T19:01:35.468953Z","shell.execute_reply.started":"2021-05-27T19:01:35.458410Z","shell.execute_reply":"2021-05-27T19:01:35.467526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_df = pd.DataFrame({'ImageID': range(1, len(test)+1), 'Label': y_pred})","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:35.470236Z","iopub.execute_input":"2021-05-27T19:01:35.470620Z","iopub.status.idle":"2021-05-27T19:01:35.479510Z","shell.execute_reply.started":"2021-05-27T19:01:35.470590Z","shell.execute_reply":"2021-05-27T19:01:35.478300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:35.481462Z","iopub.execute_input":"2021-05-27T19:01:35.481914Z","iopub.status.idle":"2021-05-27T19:01:35.499223Z","shell.execute_reply.started":"2021-05-27T19:01:35.481866Z","shell.execute_reply":"2021-05-27T19:01:35.497720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_df.to_csv('/kaggle/working/Predictions.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T19:01:35.501007Z","iopub.execute_input":"2021-05-27T19:01:35.501802Z","iopub.status.idle":"2021-05-27T19:01:35.562635Z","shell.execute_reply.started":"2021-05-27T19:01:35.501750Z","shell.execute_reply":"2021-05-27T19:01:35.561522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}