{"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-04-28T19:31:50.541985Z","iopub.execute_input":"2023-04-28T19:31:50.542472Z","iopub.status.idle":"2023-04-28T19:31:50.575703Z","shell.execute_reply.started":"2023-04-28T19:31:50.542436Z","shell.execute_reply":"2023-04-28T19:31:50.574276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:31:52.025969Z","iopub.execute_input":"2023-04-28T19:31:52.026287Z","iopub.status.idle":"2023-04-28T19:31:58.012396Z","shell.execute_reply.started":"2023-04-28T19:31:52.026257Z","shell.execute_reply":"2023-04-28T19:31:58.011056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ny_train = train.label\nX_train = train.loc[:, train.columns != 'label']","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:31:58.015689Z","iopub.execute_input":"2023-04-28T19:31:58.016034Z","iopub.status.idle":"2023-04-28T19:31:58.149018Z","shell.execute_reply.started":"2023-04-28T19:31:58.016000Z","shell.execute_reply":"2023-04-28T19:31:58.147729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout\nfrom keras.optimizers import Adam\nfrom keras.callbacks import TensorBoard\nnum_classes = 10\nepochs = 20","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:32:21.872028Z","iopub.execute_input":"2023-04-28T19:32:21.872616Z","iopub.status.idle":"2023-04-28T19:32:30.240864Z","shell.execute_reply.started":"2023-04-28T19:32:21.872570Z","shell.execute_reply":"2023-04-28T19:32:30.239767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_data = np.array(train_df, dtype = 'float32')\ntest_data = np.array(test_df, dtype='float32')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:32:30.242306Z","iopub.execute_input":"2023-04-28T19:32:30.244874Z","iopub.status.idle":"2023-04-28T19:32:34.645736Z","shell.execute_reply.started":"2023-04-28T19:32:30.244838Z","shell.execute_reply":"2023-04-28T19:32:34.644689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:32:34.647307Z","iopub.execute_input":"2023-04-28T19:32:34.647632Z","iopub.status.idle":"2023-04-28T19:32:34.654402Z","shell.execute_reply.started":"2023-04-28T19:32:34.647602Z","shell.execute_reply":"2023-04-28T19:32:34.653369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_data[:,1:]/255\ny_train = train_data[:,0]\nx_test= test_data[:]/255\n\nx_train,x_validate,y_train,y_validate = train_test_split(x_train,y_train,test_size = 0.2,random_state = 12345)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:35:37.816358Z","iopub.execute_input":"2023-04-28T19:35:37.816798Z","iopub.status.idle":"2023-04-28T19:35:38.273805Z","shell.execute_reply.started":"2023-04-28T19:35:37.816762Z","shell.execute_reply":"2023-04-28T19:35:38.272772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_rows = 28\nimage_cols = 28\nbatch_size = 4096\nimage_shape = (image_rows,image_cols,1) ","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:32:35.118182Z","iopub.execute_input":"2023-04-28T19:32:35.118572Z","iopub.status.idle":"2023-04-28T19:32:35.127151Z","shell.execute_reply.started":"2023-04-28T19:32:35.118536Z","shell.execute_reply":"2023-04-28T19:32:35.126296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:35:40.608033Z","iopub.execute_input":"2023-04-28T19:35:40.611122Z","iopub.status.idle":"2023-04-28T19:35:40.618025Z","shell.execute_reply.started":"2023-04-28T19:35:40.611076Z","shell.execute_reply":"2023-04-28T19:35:40.616629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0],*image_shape)\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:33:13.270990Z","iopub.execute_input":"2023-04-28T19:33:13.271494Z","iopub.status.idle":"2023-04-28T19:33:13.284112Z","shell.execute_reply.started":"2023-04-28T19:33:13.271427Z","shell.execute_reply":"2023-04-28T19:33:13.282867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0],*image_shape)\nx_test = x_test.reshape(x_test.shape[0],*image_shape)\nx_validate = x_validate.reshape(x_validate.shape[0],*image_shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:35:43.789546Z","iopub.execute_input":"2023-04-28T19:35:43.789966Z","iopub.status.idle":"2023-04-28T19:35:43.796249Z","shell.execute_reply.started":"2023-04-28T19:35:43.789929Z","shell.execute_reply":"2023-04-28T19:35:43.794855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncnn_model = Sequential([\n    Conv2D(filters=32,kernel_size=3,activation='relu',input_shape = image_shape),\n    MaxPooling2D(pool_size=2) ,# down sampling the output instead of 28*28 it is 14*14\n    Dropout(0.2),\n    Flatten(), # flatten out the layers\n    Dense(32,activation='relu'),\n    Dense(10,activation = 'softmax')\n    \n])\n\ncnn_model.compile(loss ='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001),metrics =['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:35:46.906721Z","iopub.execute_input":"2023-04-28T19:35:46.907846Z","iopub.status.idle":"2023-04-28T19:35:47.155758Z","shell.execute_reply.started":"2023-04-28T19:35:46.907796Z","shell.execute_reply":"2023-04-28T19:35:47.152730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = cnn_model.fit(\n    x_train,\n    y_train,\n    batch_size=4096,\n    epochs=75,\n    verbose=1,\n    validation_data=(x_validate,y_validate),\n)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:35:51.754637Z","iopub.execute_input":"2023-04-28T19:35:51.755853Z","iopub.status.idle":"2023-04-28T19:49:14.534184Z","shell.execute_reply.started":"2023-04-28T19:35:51.755789Z","shell.execute_reply":"2023-04-28T19:49:14.532958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nclass_names = [str(i) for i in range(10)]\nplt.figure(figsize=(10, 10))\nfor i in range(36):\n    plt.subplot(6, 6, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i].reshape((28,28)))\n    label_index = int(y_train[i])\n    plt.title(class_names[label_index])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:06:11.952919Z","iopub.execute_input":"2023-04-28T20:06:11.953531Z","iopub.status.idle":"2023-04-28T20:06:13.688753Z","shell.execute_reply.started":"2023-04-28T20:06:11.953487Z","shell.execute_reply":"2023-04-28T20:06:13.687622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history.keys()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:54:30.494763Z","iopub.execute_input":"2023-04-28T19:54:30.495217Z","iopub.status.idle":"2023-04-28T19:54:30.501565Z","shell.execute_reply.started":"2023-04-28T19:54:30.495163Z","shell.execute_reply":"2023-04-28T19:54:30.500523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nplt.subplot(2, 2, 1)\nplt.plot(history.history['loss'], label='Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend()\nplt.title('Training - Loss Function')\n\nplt.subplot(2, 2, 2)\nplt.plot(history.history['accuracy'], label='Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.title('Train - Accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T19:54:39.407430Z","iopub.execute_input":"2023-04-28T19:54:39.407858Z","iopub.status.idle":"2023-04-28T19:54:39.841366Z","shell.execute_reply.started":"2023-04-28T19:54:39.407819Z","shell.execute_reply":"2023-04-28T19:54:39.840331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_array = cnn_model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:00:01.994618Z","iopub.execute_input":"2023-04-28T20:00:01.995514Z","iopub.status.idle":"2023-04-28T20:00:05.451697Z","shell.execute_reply.started":"2023-04-28T20:00:01.995460Z","shell.execute_reply":"2023-04-28T20:00:05.450768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_label_array = [np.argmax(cnn_array[i]) for i in range(len(cnn_array))]","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:04:22.540088Z","iopub.execute_input":"2023-04-28T20:04:22.540544Z","iopub.status.idle":"2023-04-28T20:04:22.598765Z","shell.execute_reply.started":"2023-04-28T20:04:22.540501Z","shell.execute_reply":"2023-04-28T20:04:22.597724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn_output_df = pd.DataFrame()\ncnn_output_df['Label'] = cnn_label_array\ncnn_output_df.index +=1\ncnn_output_df.index.name = \"ImageId\"\ncnn_output_df.to_csv('/kaggle/working/submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-28T20:06:22.404757Z","iopub.execute_input":"2023-04-28T20:06:22.405455Z","iopub.status.idle":"2023-04-28T20:06:22.466836Z","shell.execute_reply.started":"2023-04-28T20:06:22.405412Z","shell.execute_reply":"2023-04-28T20:06:22.465658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}