{"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-12-26T09:06:13.71304Z","iopub.execute_input":"2021-12-26T09:06:13.713739Z","iopub.status.idle":"2021-12-26T09:06:13.73609Z","shell.execute_reply.started":"2021-12-26T09:06:13.713577Z","shell.execute_reply":"2021-12-26T09:06:13.734636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\ntf.config.experimental_run_functions_eagerly(True)\n\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:13.740088Z","iopub.execute_input":"2021-12-26T09:06:13.741218Z","iopub.status.idle":"2021-12-26T09:06:16.081436Z","shell.execute_reply.started":"2021-12-26T09:06:13.74117Z","shell.execute_reply":"2021-12-26T09:06:16.080477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv')\ntrain_labels = train_data['label']\ndel train_data['label']","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:16.082697Z","iopub.execute_input":"2021-12-26T09:06:16.083016Z","iopub.status.idle":"2021-12-26T09:06:21.407977Z","shell.execute_reply.started":"2021-12-26T09:06:16.08298Z","shell.execute_reply":"2021-12-26T09:06:21.407168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Now we need to split the training data so as to train and test the model and finally submit the predictions on the test_data","metadata":{}},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:21.410152Z","iopub.execute_input":"2021-12-26T09:06:21.410454Z","iopub.status.idle":"2021-12-26T09:06:21.419651Z","shell.execute_reply.started":"2021-12-26T09:06:21.410422Z","shell.execute_reply":"2021-12-26T09:06:21.419021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now we start with the CNN\n\nfrom tensorflow.keras import datasets, layers, models\ntrain_data = np.array(train_data).reshape(42000,28,28)\ntrain_labels = np.array(train_labels)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:21.420871Z","iopub.execute_input":"2021-12-26T09:06:21.421234Z","iopub.status.idle":"2021-12-26T09:06:21.99708Z","shell.execute_reply.started":"2021-12-26T09:06:21.421206Z","shell.execute_reply":"2021-12-26T09:06:21.996253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train, y_train) = (train_data[:33600], train_labels[:33600])\n(x_test, y_test) = (train_data[33600:42000], train_labels[33600:42000])\n\n# y are the labels\n# x are the images","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:21.998565Z","iopub.execute_input":"2021-12-26T09:06:21.998938Z","iopub.status.idle":"2021-12-26T09:06:22.006657Z","shell.execute_reply.started":"2021-12-26T09:06:21.998893Z","shell.execute_reply":"2021-12-26T09:06:22.004071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = x_test/255.0\nx_train = x_train/255.0","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:22.008098Z","iopub.execute_input":"2021-12-26T09:06:22.008924Z","iopub.status.idle":"2021-12-26T09:06:22.129318Z","shell.execute_reply.started":"2021-12-26T09:06:22.008892Z","shell.execute_reply":"2021-12-26T09:06:22.128503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:22.130413Z","iopub.execute_input":"2021-12-26T09:06:22.130835Z","iopub.status.idle":"2021-12-26T09:06:22.138372Z","shell.execute_reply.started":"2021-12-26T09:06:22.130805Z","shell.execute_reply":"2021-12-26T09:06:22.137007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train)\ny_test = keras.utils.to_categorical(y_test) # One-hot encoding of the labels","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:22.142018Z","iopub.execute_input":"2021-12-26T09:06:22.142854Z","iopub.status.idle":"2021-12-26T09:06:22.148662Z","shell.execute_reply.started":"2021-12-26T09:06:22.142795Z","shell.execute_reply":"2021-12-26T09:06:22.14801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:22.150587Z","iopub.execute_input":"2021-12-26T09:06:22.151221Z","iopub.status.idle":"2021-12-26T09:06:22.161771Z","shell.execute_reply.started":"2021-12-26T09:06:22.151179Z","shell.execute_reply":"2021-12-26T09:06:22.160918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To see an image\n\nplt.figure()\nplt.imshow(x_train[5])\nplt.xlabel(np.argmax(y_train[5]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:22.162767Z","iopub.execute_input":"2021-12-26T09:06:22.163324Z","iopub.status.idle":"2021-12-26T09:06:22.374495Z","shell.execute_reply.started":"2021-12-26T09:06:22.163291Z","shell.execute_reply":"2021-12-26T09:06:22.373666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model\n# model = keras.models.Sequential()\n# model.add(keras.layers.Conv2D(filters=10,kernel_size=2,activation='relu', input_shape=(28,28,1)))\n# model.add(keras.layers.Dense(200, activation='sigmoid'))  # hidden layer (2)\n# model.add(keras.layers.Dense(10, activation='softmax')) # output layer (3)\n\nfrom tensorflow.keras import datasets, layers, models\n\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(10, (2,2), activation='relu', input_shape=(28,28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n# model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n# model.add(layers.MaxPooling2D((2, 2)))\n# model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n\nmodel.add(layers.Flatten())\n# model.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))\n","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:08:55.236891Z","iopub.execute_input":"2021-12-26T09:08:55.2372Z","iopub.status.idle":"2021-12-26T09:08:55.269942Z","shell.execute_reply.started":"2021-12-26T09:08:55.237166Z","shell.execute_reply":"2021-12-26T09:08:55.269082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Compile Code\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:09:00.958251Z","iopub.execute_input":"2021-12-26T09:09:00.958513Z","iopub.status.idle":"2021-12-26T09:09:00.968295Z","shell.execute_reply.started":"2021-12-26T09:09:00.958486Z","shell.execute_reply":"2021-12-26T09:09:00.967322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\n\nx_train1 = x_train.reshape(-1,28,28,1)\ny_train1 = y_train\n# x_train_new = tf.data.Dataset.from_tensor_slices((x_train1, y_train))\n\nmodel.fit(x_train1, y_train1, epochs=1)  # we pass the data, labels and epochs","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:09:02.175765Z","iopub.execute_input":"2021-12-26T09:09:02.176076Z","iopub.status.idle":"2021-12-26T09:09:43.358218Z","shell.execute_reply.started":"2021-12-26T09:09:02.176037Z","shell.execute_reply":"2021-12-26T09:09:43.357307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train1.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.570857Z","iopub.execute_input":"2021-12-26T09:06:44.571605Z","iopub.status.idle":"2021-12-26T09:06:44.575935Z","shell.execute_reply.started":"2021-12-26T09:06:44.571573Z","shell.execute_reply":"2021-12-26T09:06:44.575401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.576848Z","iopub.execute_input":"2021-12-26T09:06:44.577315Z","iopub.status.idle":"2021-12-26T09:06:44.986493Z","shell.execute_reply.started":"2021-12-26T09:06:44.577285Z","shell.execute_reply":"2021-12-26T09:06:44.985253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.987382Z","iopub.status.idle":"2021-12-26T09:06:44.987684Z","shell.execute_reply.started":"2021-12-26T09:06:44.987526Z","shell.execute_reply":"2021-12-26T09:06:44.987541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = [0,0,0,0,0,0,0,0,0,0]\ntot = [0,0,0,0,0,0,0,0,0,0]\nidx = 0\nfor i in range(8400):\n    # print(str(np.argmax(predictions[i])) + \" \" + str(np.argmax(test_labels[i])))\n    prediction = np.argmax(predictions[i])\n    answer = np.argmax(y_test[i])\n    tot[answer]+=1\n    if(prediction == answer):\n        correct[prediction]+=1\n","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.989158Z","iopub.status.idle":"2021-12-26T09:06:44.989475Z","shell.execute_reply.started":"2021-12-26T09:06:44.989308Z","shell.execute_reply":"2021-12-26T09:06:44.989325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"individual_acc = correct.copy()\n\nfor i in range(10):\n    individual_acc[i] /= tot[i]\n\nacc = np.mean(individual_acc)\nacc","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.990487Z","iopub.status.idle":"2021-12-26T09:06:44.990822Z","shell.execute_reply.started":"2021-12-26T09:06:44.990669Z","shell.execute_reply":"2021-12-26T09:06:44.990685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"individual_acc","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.991692Z","iopub.status.idle":"2021-12-26T09:06:44.992Z","shell.execute_reply.started":"2021-12-26T09:06:44.991825Z","shell.execute_reply":"2021-12-26T09:06:44.99184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = np.array(test_data).reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.993231Z","iopub.status.idle":"2021-12-26T09:06:44.993523Z","shell.execute_reply.started":"2021-12-26T09:06:44.993368Z","shell.execute_reply":"2021-12-26T09:06:44.993383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data / 255.0\nactual_predictions = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.994512Z","iopub.status.idle":"2021-12-26T09:06:44.994825Z","shell.execute_reply.started":"2021-12-26T09:06:44.994642Z","shell.execute_reply":"2021-12-26T09:06:44.994658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_predictions[2002]","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.995814Z","iopub.status.idle":"2021-12-26T09:06:44.996134Z","shell.execute_reply.started":"2021-12-26T09:06:44.995978Z","shell.execute_reply":"2021-12-26T09:06:44.995994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = []\nidx = 1\nfor a in actual_predictions:\n    b = np.argmax(a)\n    row = [idx]\n    idx += 1\n    row.append(b)\n    result.append(row)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.996775Z","iopub.status.idle":"2021-12-26T09:06:44.997091Z","shell.execute_reply.started":"2021-12-26T09:06:44.996903Z","shell.execute_reply":"2021-12-26T09:06:44.996918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result[3]","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.99795Z","iopub.status.idle":"2021-12-26T09:06:44.998257Z","shell.execute_reply.started":"2021-12-26T09:06:44.9981Z","shell.execute_reply":"2021-12-26T09:06:44.998115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\n\n# name of csv file \nfilename = \"final_sub_2.csv\"\n    \n# writing to csv file \nwith open(filename, 'w') as csvfile: \n    # creating a csv writer object \n    csvwriter = csv.writer(csvfile) \n        \n    csvwriter.writerow(['ImageId', 'Label'])\n    # writing the data rows \n    csvwriter.writerows(result)","metadata":{"execution":{"iopub.status.busy":"2021-12-26T09:06:44.999596Z","iopub.status.idle":"2021-12-26T09:06:44.999887Z","shell.execute_reply.started":"2021-12-26T09:06:44.999733Z","shell.execute_reply":"2021-12-26T09:06:44.999747Z"},"trusted":true},"execution_count":null,"outputs":[]}]}