{"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":{"execution":{"iopub.status.busy":"2021-09-18T18:53:24.936018Z","iopub.execute_input":"2021-09-18T18:53:24.936635Z","iopub.status.idle":"2021-09-18T18:53:24.949236Z","shell.execute_reply.started":"2021-09-18T18:53:24.936500Z","shell.execute_reply":"2021-09-18T18:53:24.948178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nimport math\nimport pandas as pd\nimport numpy as np\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:24.951531Z","iopub.execute_input":"2021-09-18T18:53:24.951778Z","iopub.status.idle":"2021-09-18T18:53:24.968646Z","shell.execute_reply.started":"2021-09-18T18:53:24.951752Z","shell.execute_reply":"2021-09-18T18:53:24.967867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\") \n\ntrain_data, test_data = train_test_split(data, test_size=0.3)      #data for cheking and testing\ntrain_data, valid_data = train_test_split(train_data, test_size=0.2)\n\npred_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\") #data for predictions","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:24.969795Z","iopub.execute_input":"2021-09-18T18:53:24.970606Z","iopub.status.idle":"2021-09-18T18:53:29.816561Z","shell.execute_reply.started":"2021-09-18T18:53:24.970559Z","shell.execute_reply":"2021-09-18T18:53:29.815614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Number of train examples: {train_data.shape[0]}\")\nprint(f\"Number of test exmaples {test_data.shape[0]}\")\nprint(f\"Number of valid exmaples {valid_data.shape[0]}\")\nprint(f\"Number of prediction examples: {pred_data.shape[0]}\")","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:29.817777Z","iopub.execute_input":"2021-09-18T18:53:29.818023Z","iopub.status.idle":"2021-09-18T18:53:29.824227Z","shell.execute_reply.started":"2021-09-18T18:53:29.817995Z","shell.execute_reply":"2021-09-18T18:53:29.823407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_list = list(np.sort(train_data['label'].unique()))\nprint(f\"Class list of numbers: {class_list}\")","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:29.826161Z","iopub.execute_input":"2021-09-18T18:53:29.826414Z","iopub.status.idle":"2021-09-18T18:53:29.839217Z","shell.execute_reply.started":"2021-09-18T18:53:29.826386Z","shell.execute_reply":"2021-09-18T18:53:29.838454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"darkgrid\")\nsns.countplot(x=train_data[\"label\"])","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:29.840467Z","iopub.execute_input":"2021-09-18T18:53:29.840702Z","iopub.status.idle":"2021-09-18T18:53:30.098729Z","shell.execute_reply.started":"2021-09-18T18:53:29.840674Z","shell.execute_reply":"2021-09-18T18:53:30.097698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = (train_data.iloc[:, 1:].values).astype('float32')\ny_train = train_data.iloc[:, 0].values.astype('int32')\n\nx_test = (test_data.iloc[:, 1:].values).astype('float32')\ny_test = test_data.iloc[:, 0].values.astype('int32')\n\nx_valid = (valid_data.iloc[:, 1:].values).astype('float32')\ny_valid = valid_data.iloc[:, 0].values.astype('int32')\n\nx_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32') / 255\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32') / 255\nx_valid = x_valid.reshape(x_valid.shape[0], 28, 28, 1).astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:30.099938Z","iopub.execute_input":"2021-09-18T18:53:30.100192Z","iopub.status.idle":"2021-09-18T18:53:30.223712Z","shell.execute_reply.started":"2021-09-18T18:53:30.100162Z","shell.execute_reply":"2021-09-18T18:53:30.222876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_pred = pred_data.values.astype('float32')\nx_pred = x_pred.reshape(x_pred.shape[0], 28, 28, 1).astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:30.225086Z","iopub.execute_input":"2021-09-18T18:53:30.225346Z","iopub.status.idle":"2021-09-18T18:53:30.316724Z","shell.execute_reply.started":"2021-09-18T18:53:30.225303Z","shell.execute_reply":"2021-09-18T18:53:30.315754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_train_examples = x_train.shape[0]\nnum_test_examples = x_test.shape[0]\nnum_pred_examples = x_pred.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:30.318166Z","iopub.execute_input":"2021-09-18T18:53:30.318428Z","iopub.status.idle":"2021-09-18T18:53:30.323715Z","shell.execute_reply.started":"2021-09-18T18:53:30.318396Z","shell.execute_reply":"2021-09-18T18:53:30.322772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu,\n                           input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D((2, 2), strides=2),\n    tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu),\n    tf.keras.layers.MaxPooling2D((2, 2), strides=2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation=tf.nn.relu),\n    tf.keras.layers.Dense(10, activation=tf.nn.softmax)\n])","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:30.324992Z","iopub.execute_input":"2021-09-18T18:53:30.325213Z","iopub.status.idle":"2021-09-18T18:53:30.385181Z","shell.execute_reply.started":"2021-09-18T18:53:30.325186Z","shell.execute_reply":"2021-09-18T18:53:30.384367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\n\nmodel.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])\nmodel.fit(\n    x=x_train, \n    y=y_train, \n    epochs=10, \n    steps_per_epoch=math.ceil(num_train_examples/BATCH_SIZE),\n    validation_data = (x_valid, y_valid))","metadata":{"execution":{"iopub.status.busy":"2021-09-18T18:53:30.386780Z","iopub.execute_input":"2021-09-18T18:53:30.387088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(x_test, y_test, steps=math.ceil(num_test_examples/BATCH_SIZE))\nprint('Accuracy on test dataset:', test_accuracy)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_test = model.predict(x_test, verbose=0)\ntest_images = x_test\ntest_labels = y_test","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image(i, predictions_array, true_labels, images):\n    predictions_array, true_label, img = predictions_array[i], true_labels[i], images[i]\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([])\n\n    plt.imshow(img[...,0], cmap=plt.cm.binary)\n\n    predicted_label = np.argmax(predictions_array)\n    if predicted_label == true_label:\n        color = 'blue'\n    else:\n        color = 'red'\n\n    plt.xlabel(\"{} {:2.0f}% ({})\".format(predicted_label,\n                                    100*np.max(predictions_array),\n                                    true_label),\n                                    color=color)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_rows = 5\nnum_cols = 5\nnum_images = num_rows*num_cols\nplt.figure(figsize=(2*num_cols, 2*num_rows))\n\nfor i in range(num_images):\n    plt.subplot(num_rows, num_cols, i+1)\n    plot_image(i, predictions_test, test_labels, test_images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image_pred(i, predictions_array, images):\n    predictions_array, img = predictions_array[i], images[i]\n    plt.grid(False)\n    plt.xticks([])\n    plt.yticks([])\n\n    plt.imshow(img[...,0], cmap=plt.cm.binary)\n\n    predicted_label = np.argmax(predictions_array)\n\n    plt.xlabel(\"{} {:2.0f}%\".format(predicted_label,\n                                    100*np.max(predictions_array)),\n                                    color='blue')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_pred, verbose=0)\npred_images = x_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_rows = 5\nnum_cols = 5\nnum_images = num_rows*num_cols\nplt.figure(figsize=(2*num_cols, 2*num_rows))\n\nfor i in range(num_images):\n    plt.subplot(num_rows, num_cols, i+1)\n    plot_image_pred(i, predictions, pred_images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solution = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\") \nsolution[\"Label\"] = pd.Series(np.argmax(predictions, axis=1))\nsolution.to_csv(\"sample_submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}