{"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-09-06T21:29:27.036453Z","iopub.execute_input":"2021-09-06T21:29:27.036846Z","iopub.status.idle":"2021-09-06T21:29:27.045575Z","shell.execute_reply.started":"2021-09-06T21:29:27.03681Z","shell.execute_reply":"2021-09-06T21:29:27.044727Z"},"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\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-09-06T21:29:27.047046Z","iopub.execute_input":"2021-09-06T21:29:27.047402Z","iopub.status.idle":"2021-09-06T21:29:27.058922Z","shell.execute_reply.started":"2021-09-06T21:29:27.047372Z","shell.execute_reply":"2021-09-06T21:29:27.058134Z"},"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-06T21:29:27.060893Z","iopub.execute_input":"2021-09-06T21:29:27.061331Z","iopub.status.idle":"2021-09-06T21:29:31.999933Z","shell.execute_reply.started":"2021-09-06T21:29:27.061298Z","shell.execute_reply":"2021-09-06T21:29:31.998816Z"},"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-06T21:29:32.001724Z","iopub.execute_input":"2021-09-06T21:29:32.002032Z","iopub.status.idle":"2021-09-06T21:29:32.008491Z","shell.execute_reply.started":"2021-09-06T21:29:32.002003Z","shell.execute_reply":"2021-09-06T21:29:32.00752Z"},"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-06T21:29:32.009766Z","iopub.execute_input":"2021-09-06T21:29:32.010055Z","iopub.status.idle":"2021-09-06T21:29:32.024333Z","shell.execute_reply.started":"2021-09-06T21:29:32.010019Z","shell.execute_reply":"2021-09-06T21:29:32.02342Z"},"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-06T21:29:32.025817Z","iopub.execute_input":"2021-09-06T21:29:32.026526Z","iopub.status.idle":"2021-09-06T21:29:32.266325Z","shell.execute_reply.started":"2021-09-06T21:29:32.026481Z","shell.execute_reply":"2021-09-06T21:29:32.265302Z"},"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-06T21:29:32.268431Z","iopub.execute_input":"2021-09-06T21:29:32.26872Z","iopub.status.idle":"2021-09-06T21:29:32.393717Z","shell.execute_reply.started":"2021-09-06T21:29:32.268674Z","shell.execute_reply":"2021-09-06T21:29:32.392814Z"},"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-06T21:29:32.395414Z","iopub.execute_input":"2021-09-06T21:29:32.395721Z","iopub.status.idle":"2021-09-06T21:29:32.482713Z","shell.execute_reply.started":"2021-09-06T21:29:32.395673Z","shell.execute_reply":"2021-09-06T21:29:32.481715Z"},"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-06T21:29:32.484039Z","iopub.execute_input":"2021-09-06T21:29:32.484363Z","iopub.status.idle":"2021-09-06T21:29:32.488934Z","shell.execute_reply.started":"2021-09-06T21:29:32.484331Z","shell.execute_reply":"2021-09-06T21:29:32.487934Z"},"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-06T21:29:32.490066Z","iopub.execute_input":"2021-09-06T21:29:32.490528Z","iopub.status.idle":"2021-09-06T21:29:32.549973Z","shell.execute_reply.started":"2021-09-06T21:29:32.490486Z","shell.execute_reply":"2021-09-06T21:29:32.548974Z"},"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-06T21:29:32.551178Z","iopub.execute_input":"2021-09-06T21:29:32.551471Z","iopub.status.idle":"2021-09-06T21:32:10.668982Z","shell.execute_reply.started":"2021-09-06T21:29:32.551444Z","shell.execute_reply":"2021-09-06T21:32:10.668125Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:10.670105Z","iopub.execute_input":"2021-09-06T21:32:10.670375Z","iopub.status.idle":"2021-09-06T21:32:13.611804Z","shell.execute_reply.started":"2021-09-06T21:32:10.67035Z","shell.execute_reply":"2021-09-06T21:32:13.610812Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:13.613063Z","iopub.execute_input":"2021-09-06T21:32:13.613335Z","iopub.status.idle":"2021-09-06T21:32:16.101222Z","shell.execute_reply.started":"2021-09-06T21:32:13.613308Z","shell.execute_reply":"2021-09-06T21:32:16.100284Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:16.102455Z","iopub.execute_input":"2021-09-06T21:32:16.102758Z","iopub.status.idle":"2021-09-06T21:32:16.108844Z","shell.execute_reply.started":"2021-09-06T21:32:16.102729Z","shell.execute_reply":"2021-09-06T21:32:16.108172Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:16.10989Z","iopub.execute_input":"2021-09-06T21:32:16.110289Z","iopub.status.idle":"2021-09-06T21:32:17.455621Z","shell.execute_reply.started":"2021-09-06T21:32:16.110249Z","shell.execute_reply":"2021-09-06T21:32:17.454631Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:17.456988Z","iopub.execute_input":"2021-09-06T21:32:17.457257Z","iopub.status.idle":"2021-09-06T21:32:17.462862Z","shell.execute_reply.started":"2021-09-06T21:32:17.457231Z","shell.execute_reply":"2021-09-06T21:32:17.46197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_pred, verbose=0)\npred_images = x_pred","metadata":{"execution":{"iopub.status.busy":"2021-09-06T21:32:17.464122Z","iopub.execute_input":"2021-09-06T21:32:17.464504Z","iopub.status.idle":"2021-09-06T21:32:23.187869Z","shell.execute_reply.started":"2021-09-06T21:32:17.464463Z","shell.execute_reply":"2021-09-06T21:32:23.186997Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:23.189085Z","iopub.execute_input":"2021-09-06T21:32:23.189351Z","iopub.status.idle":"2021-09-06T21:32:24.858908Z","shell.execute_reply.started":"2021-09-06T21:32:23.189324Z","shell.execute_reply":"2021-09-06T21:32:24.857782Z"},"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":{"execution":{"iopub.status.busy":"2021-09-06T21:32:24.862819Z","iopub.execute_input":"2021-09-06T21:32:24.863095Z","iopub.status.idle":"2021-09-06T21:32:24.930759Z","shell.execute_reply.started":"2021-09-06T21:32:24.863067Z","shell.execute_reply":"2021-09-06T21:32:24.929869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}