{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Digit Recognizer\n## Learn computer vision fundamentals with the famous MNIST data\n\nThe data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.\n\nEach image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.\n\nThe training data set, (train.csv), has 785 columns. The first column, called \"label\", is the digit that was drawn by the user. The rest of the columns contain the pixel-values of the associated image.\n\nEach pixel column in the training set has a name like pixelx, where x is an integer between 0 and 783, inclusive. To locate this pixel on the image, suppose that we have decomposed x as x = i * 28 + j, where i and j are integers between 0 and 27, inclusive. Then pixelx is located on row i and column j of a 28 x 28 matrix, (indexing by zero).\n\nFor example, pixel31 indicates the pixel that is in the fourth column from the left, and the second row from the top, as in the ascii-diagram below.\n\nVisually, if we omit the \"pixel\" prefix, the pixels make up the image like this:\n\n`000 001 002 003 ... 026 027\n 028 029 030 031 ... 054 055\n 056 057 058 059 ... 082 083\n  |   |   |   |  ...  |   |\n 728 729 730 731 ... 754 755\n 756 757 758 759 ... 782 783`\n\nThe test data set, (test.csv), is the same as the training set, except that it does not contain the \"label\" column.\n\nYour submission file should be in the following format: For each of the 28000 images in the test set, output a single line containing the ImageId and the digit you predict. For example, if you predict that the first image is of a 3, the second image is of a 7, and the third image is of a 8, then your submission file would look like:\n\n`ImageId,Label\n 1,3\n 2,7\n 3,8 \n(27997 more lines)`\n\nThe evaluation metric for this contest is the categorization accuracy, or the proportion of test images that are correctly classified. For example, a categorization accuracy of 0.97 indicates that you have correctly classified all but 3% of the images."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\n# Loading the images\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Create training data matrix\n\ntrain_file = \"../input/digit-recognizer/train.csv\"\ntrain_data = np.loadtxt(train_file, skiprows=1, delimiter=',')\n\nprint(train_data)\nprint(train_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\n\n# Preparing and normalizing the data for training\n\nnum_classes = 10\nnum_imgs = train_data.shape[0]\n\nimg_rows = 28\nimg_columns = 28\n\ny_raw = train_data[:,0]\ny_train = keras.utils.to_categorical(y_raw, num_classes)\n\nx_raw = train_data[:,1:]\nx_train = x_raw.reshape(num_imgs, img_rows, img_columns,1)\nx_train = x_train/255\n\nprint(x_raw)\nprint(y_train)\nprint(num_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D\n\n# We create the model\n# It is advised to add Dense layers after flattening.\n\nmy_model = Sequential()\nmy_model.add(Conv2D(\n    filters=20,\n    kernel_size=3,\n    input_shape=(img_rows, img_columns, 1),\n    activation='relu',\n))\n\nmy_model.add(Conv2D(\n    filters=20,\n    kernel_size=3,\n    input_shape=(img_rows, img_columns, 1),\n    activation='relu',\n))\n\nmy_model.add(Flatten())\nmy_model.add(Dense(units=100,activation='relu'))\nmy_model.add(Dense(units=10,activation='softmax'))\n\nmy_model.compile(\n    optimizer = 'adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy'],\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# We fit the model and check its expected accuracy\n\nmy_model.fit(\n    x=x_train,\n    y=y_train,\n    batch_size=100,\n    epochs=4, \n    verbose=1,\n    validation_split=0.2,\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preparing the test data\n\nnum_test_imgs = 28000\n\ntest_file = \"../input/digit-recognizer/test.csv\"\ntest_data = np.loadtxt(test_file, skiprows=1, delimiter=',')\nprint(test_data.shape)\n\nX_raw = test_data[:,0:]\nprint(X_raw.shape)\n\nX_test = X_raw.reshape(num_test_imgs, img_rows, img_columns,1)\nX_test = X_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Getting predictions from test data\n\npred = my_model.predict(X_test)\nprint('Predictions',pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Prepare predictions for submission\n\ni = 1\npred_dict = {'ImageId':[],'Label':[]}\nfor image_row in pred:\n    index_max = max(range(len(image_row)), key=image_row.__getitem__)\n    pred_dict['ImageId'].append(i)\n    pred_dict['Label'].append(index_max)\n    i=i+1\n    \nsubmission = pd.DataFrame(data=pred_dict)\n\nsubmission.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}