{"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-02-07T23:10:17.969360Z","iopub.execute_input":"2023-02-07T23:10:17.970781Z","iopub.status.idle":"2023-02-07T23:10:18.009555Z","shell.execute_reply.started":"2023-02-07T23:10:17.970638Z","shell.execute_reply":"2023-02-07T23:10:18.008508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# My imports\n\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:19.935904Z","iopub.execute_input":"2023-02-07T23:10:19.936660Z","iopub.status.idle":"2023-02-07T23:10:26.160984Z","shell.execute_reply.started":"2023-02-07T23:10:19.936604Z","shell.execute_reply":"2023-02-07T23:10:26.159571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading in the training dataset & testing dataset\n\ntrain = 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-02-07T23:10:28.807716Z","iopub.execute_input":"2023-02-07T23:10:28.808364Z","iopub.status.idle":"2023-02-07T23:10:34.830572Z","shell.execute_reply.started":"2023-02-07T23:10:28.808327Z","shell.execute_reply":"2023-02-07T23:10:34.829248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Displaying the training dataset for a first look.\n\ndisplay(train)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:36.140197Z","iopub.execute_input":"2023-02-07T23:10:36.140626Z","iopub.status.idle":"2023-02-07T23:10:36.176407Z","shell.execute_reply.started":"2023-02-07T23:10:36.140590Z","shell.execute_reply":"2023-02-07T23:10:36.175196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The training dataset has 42,000 images with 784 pixels per image.\nLabels for the images are in the first column 'label'**","metadata":{}},{"cell_type":"code","source":"display(test)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:38.586586Z","iopub.execute_input":"2023-02-07T23:10:38.586991Z","iopub.status.idle":"2023-02-07T23:10:38.613873Z","shell.execute_reply.started":"2023-02-07T23:10:38.586951Z","shell.execute_reply":"2023-02-07T23:10:38.612646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate the labels from the training dataset pixels\n# Convert the labels into a numpy array\n\nlabels = train[\"label\"].to_numpy()\ndisplay(labels)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:41.411274Z","iopub.execute_input":"2023-02-07T23:10:41.412768Z","iopub.status.idle":"2023-02-07T23:10:41.425231Z","shell.execute_reply.started":"2023-02-07T23:10:41.412714Z","shell.execute_reply":"2023-02-07T23:10:41.423921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop the labels from the training dataset to create a dataset of ONLY the pixels.\n# Display dataset to make sure the column dropped\n\ntrain.drop(columns='label', axis=1, inplace=True)\ndisplay(train)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:43.936908Z","iopub.execute_input":"2023-02-07T23:10:43.937328Z","iopub.status.idle":"2023-02-07T23:10:44.079002Z","shell.execute_reply.started":"2023-02-07T23:10:43.937293Z","shell.execute_reply":"2023-02-07T23:10:44.077865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the pixels for each image into a numpy array\ntrain_pixels = train.to_numpy()\n\n# Print the pixels to make sure they're in an array\nprint(train_pixels)\n\n# Print the shape of a pixel to know absolutely what size it is\nprint(train_pixels[9999].shape)\n\n# Print the length of both the pixels and the labels to make sure they're the same.\nprint(len(train_pixels), len(labels))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:47.455746Z","iopub.execute_input":"2023-02-07T23:10:47.456153Z","iopub.status.idle":"2023-02-07T23:10:47.463579Z","shell.execute_reply.started":"2023-02-07T23:10:47.456120Z","shell.execute_reply":"2023-02-07T23:10:47.462273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tried to reshape all arrays in train_pixesl from 784 to 28. \n# Didn't work though. Not sure why\n\n#for i in range(len(train_pixels)):\n#    train_pixels[i].reshape(28,28)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:02:36.992454Z","iopub.execute_input":"2023-02-07T23:02:36.992820Z","iopub.status.idle":"2023-02-07T23:02:36.997405Z","shell.execute_reply.started":"2023-02-07T23:02:36.992789Z","shell.execute_reply":"2023-02-07T23:02:36.996358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printed shape of train_pixels to see if the loop above worked. It didn't\n\n# print(train_pixels[90].shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:02:38.731017Z","iopub.execute_input":"2023-02-07T23:02:38.731695Z","iopub.status.idle":"2023-02-07T23:02:38.735596Z","shell.execute_reply.started":"2023-02-07T23:02:38.731660Z","shell.execute_reply":"2023-02-07T23:02:38.734554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing a single image with label in greyscale\n\nnp_arr = train_pixels[90].reshape(28,28)\nplt.imshow(np_arr, interpolation='nearest', cmap=plt.cm.binary)\nplt.title(labels[90])\nplt.colorbar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:10:50.838353Z","iopub.execute_input":"2023-02-07T23:10:50.838980Z","iopub.status.idle":"2023-02-07T23:10:51.133979Z","shell.execute_reply.started":"2023-02-07T23:10:50.838938Z","shell.execute_reply":"2023-02-07T23:10:51.133136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing 16 random images and their labels. \n# Reshaped image in the plt.imshow row.\nplt.figure(figsize=(5,6))\n\nfor i in range(16):\n    k = random.randint(0, 42000)\n    plt.subplot(4, 4, i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(train_pixels[k].reshape(28,28), cmap=plt.cm.binary)\n    plt.xlabel(labels[k])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ensuring that the pixels range from 0 to 255\n\nranged = np.unique(train_pixels)\nprint(ranged)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:11:35.178710Z","iopub.execute_input":"2023-02-07T23:11:35.179203Z","iopub.status.idle":"2023-02-07T23:11:36.395233Z","shell.execute_reply.started":"2023-02-07T23:11:35.179161Z","shell.execute_reply":"2023-02-07T23:11:36.393990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalizing the train pixels and the test pixels\n\ntrain_pixels = train_pixels / 255\ntest = test / 255","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:11:38.802184Z","iopub.execute_input":"2023-02-07T23:11:38.802705Z","iopub.status.idle":"2023-02-07T23:11:38.970469Z","shell.execute_reply.started":"2023-02-07T23:11:38.802667Z","shell.execute_reply":"2023-02-07T23:11:38.969328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# initiating neural network model\n\nmodel = tf.keras.models.Sequential([\n            tf.keras.layers.Flatten(),     # Converting matrix forms (28x28) to vector forms (784)\n            tf.keras.layers.Dense(128, activation=tf.nn.relu),   # A layer with 128 nodes and ReLU as the choice of the activation function\n            tf.keras.layers.Dense(10, activation=tf.nn.softmax)  # Output layer with 10 nodes (because there are 10 classifications)\n])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:11:41.045881Z","iopub.execute_input":"2023-02-07T23:11:41.046300Z","iopub.status.idle":"2023-02-07T23:11:42.275185Z","shell.execute_reply.started":"2023-02-07T23:11:41.046268Z","shell.execute_reply":"2023-02-07T23:11:42.274141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initiate the model with the training dataset\n\nmodel.compile(\n    optimizer = tf.optimizers.Adam(),\n    loss = 'sparse_categorical_crossentropy',\n    metrics = ['accuracy']\n    )\n\nmodel.fit(train_pixels, labels, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:11:44.879913Z","iopub.execute_input":"2023-02-07T23:11:44.880694Z","iopub.status.idle":"2023-02-07T23:12:16.372987Z","shell.execute_reply.started":"2023-02-07T23:11:44.880643Z","shell.execute_reply":"2023-02-07T23:12:16.371816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Put the deep learning model on the test set\n\n#x_test = model.evaluate(test)\n#predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:31:08.784070Z","iopub.execute_input":"2023-02-07T23:31:08.784518Z","iopub.status.idle":"2023-02-07T23:31:11.020994Z","shell.execute_reply.started":"2023-02-07T23:31:08.784459Z","shell.execute_reply":"2023-02-07T23:31:11.020110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = []\nfor row in range(len(test_data)):\n    spl = test_data.iloc[row, ]['pixel0' : 'pixel783']\n    np_mid = spl.to_numpy() / 255\n    np_arr = np_mid.reshape(28,28)\n    X_test.append(np_arr)\n    \nX_test = tf.convert_to_tensor(X_test)\npredictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:33:55.701964Z","iopub.execute_input":"2023-02-07T23:33:55.702373Z","iopub.status.idle":"2023-02-07T23:34:10.643649Z","shell.execute_reply.started":"2023-02-07T23:33:55.702340Z","shell.execute_reply":"2023-02-07T23:34:10.642404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []\nimg_id = []\nix = 1\nfor i in range(len(test)):\n    ans = np.argmax(predictions[i])\n    pred.append(ans)\n    img_id.append(ix)\n    ix+=1","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:34:15.399821Z","iopub.execute_input":"2023-02-07T23:34:15.400259Z","iopub.status.idle":"2023-02-07T23:34:15.480132Z","shell.execute_reply.started":"2023-02-07T23:34:15.400221Z","shell.execute_reply":"2023-02-07T23:34:15.478686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:34:18.068033Z","iopub.execute_input":"2023-02-07T23:34:18.068435Z","iopub.status.idle":"2023-02-07T23:34:18.075664Z","shell.execute_reply.started":"2023-02-07T23:34:18.068402Z","shell.execute_reply":"2023-02-07T23:34:18.074297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Export Sumbission\noutput = pd.DataFrame({'ImageId': img_id, 'Label': pred})\noutput.to_csv('sumbission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:35:44.296701Z","iopub.execute_input":"2023-02-07T23:35:44.297129Z","iopub.status.idle":"2023-02-07T23:35:44.357285Z","shell.execute_reply.started":"2023-02-07T23:35:44.297089Z","shell.execute_reply":"2023-02-07T23:35:44.355783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T23:35:53.436926Z","iopub.execute_input":"2023-02-07T23:35:53.437341Z","iopub.status.idle":"2023-02-07T23:35:53.447971Z","shell.execute_reply.started":"2023-02-07T23:35:53.437308Z","shell.execute_reply":"2023-02-07T23:35:53.446783Z"},"trusted":true},"execution_count":null,"outputs":[]}]}