{"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)\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\n\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-22T05:34:41.972244Z","iopub.execute_input":"2023-02-22T05:34:41.972634Z","iopub.status.idle":"2023-02-22T05:34:49.105192Z","shell.execute_reply.started":"2023-02-22T05:34:41.972602Z","shell.execute_reply":"2023-02-22T05:34:49.104267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"# Load the train and test data\ntrain = pd.read_csv(dirname + '/train.csv')\ntest = pd.read_csv(dirname + '/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:49.976011Z","iopub.execute_input":"2023-02-22T05:34:49.976660Z","iopub.status.idle":"2023-02-22T05:34:55.022231Z","shell.execute_reply.started":"2023-02-22T05:34:49.976623Z","shell.execute_reply":"2023-02-22T05:34:55.021175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show the head of the train data\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.024171Z","iopub.execute_input":"2023-02-22T05:34:55.024622Z","iopub.status.idle":"2023-02-22T05:34:55.050987Z","shell.execute_reply.started":"2023-02-22T05:34:55.024585Z","shell.execute_reply":"2023-02-22T05:34:55.049789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show the head of the test data\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.052485Z","iopub.execute_input":"2023-02-22T05:34:55.052905Z","iopub.status.idle":"2023-02-22T05:34:55.069901Z","shell.execute_reply.started":"2023-02-22T05:34:55.052870Z","shell.execute_reply":"2023-02-22T05:34:55.068747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare the training data","metadata":{}},{"cell_type":"code","source":"# Define the parameters\nINPUT_SHAPE = 28\n\nCHANEL = 1","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.072697Z","iopub.execute_input":"2023-02-22T05:34:55.073058Z","iopub.status.idle":"2023-02-22T05:34:55.077661Z","shell.execute_reply.started":"2023-02-22T05:34:55.073017Z","shell.execute_reply":"2023-02-22T05:34:55.076472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train['label']\ntrain_x = train.drop('label', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.079252Z","iopub.execute_input":"2023-02-22T05:34:55.079957Z","iopub.status.idle":"2023-02-22T05:34:55.171155Z","shell.execute_reply.started":"2023-02-22T05:34:55.079922Z","shell.execute_reply":"2023-02-22T05:34:55.169870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize data\ntrain_x = train_x / 255.0\ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.174079Z","iopub.execute_input":"2023-02-22T05:34:55.174853Z","iopub.status.idle":"2023-02-22T05:34:55.347783Z","shell.execute_reply.started":"2023-02-22T05:34:55.174813Z","shell.execute_reply":"2023-02-22T05:34:55.346697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape the train data to 28 * 28 * 1\ntrain_x = train_x.values.reshape(-1, INPUT_SHAPE, INPUT_SHAPE, CHANEL)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.349083Z","iopub.execute_input":"2023-02-22T05:34:55.349449Z","iopub.status.idle":"2023-02-22T05:34:55.355830Z","shell.execute_reply.started":"2023-02-22T05:34:55.349413Z","shell.execute_reply":"2023-02-22T05:34:55.354836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the sample images\nplt.figure(figsize = (10, 10))\nfor i in range(9):\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(train_x[i])\n    plt.xlabel('Actual value is: ' + str(train_y[i]))\nplt.tight_layout(pad = 0.1)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:55.357535Z","iopub.execute_input":"2023-02-22T05:34:55.358288Z","iopub.status.idle":"2023-02-22T05:34:56.601650Z","shell.execute_reply.started":"2023-02-22T05:34:55.358246Z","shell.execute_reply":"2023-02-22T05:34:56.600743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tensorflow CNN model","metadata":{}},{"cell_type":"code","source":"# Get the number of classes\nnum_classes = len(train_y.unique())\n\nmodel = Sequential([\n  layers.Conv2D(32, (3, 3), activation='relu', input_shape=(INPUT_SHAPE, INPUT_SHAPE, 1)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:56.602867Z","iopub.execute_input":"2023-02-22T05:34:56.603259Z","iopub.status.idle":"2023-02-22T05:34:59.844155Z","shell.execute_reply.started":"2023-02-22T05:34:56.603222Z","shell.execute_reply":"2023-02-22T05:34:59.843191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:59.847648Z","iopub.execute_input":"2023-02-22T05:34:59.848001Z","iopub.status.idle":"2023-02-22T05:34:59.861054Z","shell.execute_reply.started":"2023-02-22T05:34:59.847974Z","shell.execute_reply":"2023-02-22T05:34:59.860152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:59.862022Z","iopub.execute_input":"2023-02-22T05:34:59.862379Z","iopub.status.idle":"2023-02-22T05:34:59.892359Z","shell.execute_reply.started":"2023-02-22T05:34:59.862345Z","shell.execute_reply":"2023-02-22T05:34:59.891634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  x = train_x,\n  y = train_y,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:34:59.893274Z","iopub.execute_input":"2023-02-22T05:34:59.893603Z","iopub.status.idle":"2023-02-22T05:36:22.990055Z","shell.execute_reply.started":"2023-02-22T05:34:59.893571Z","shell.execute_reply":"2023-02-22T05:36:22.988971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\n# plt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:36:22.991836Z","iopub.execute_input":"2023-02-22T05:36:22.992169Z","iopub.status.idle":"2023-02-22T05:36:23.236435Z","shell.execute_reply.started":"2023-02-22T05:36:22.992138Z","shell.execute_reply":"2023-02-22T05:36:23.235614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20, 20))\nfor i in range(9):\n    image = train_x[i].reshape(-1, 28, 28, 1)\n    predicted = model.predict(image)\n    predicted_y = np.argmax(predicted)\n    plt.subplot(3, 3, i + 1)\n    plt.imshow(image[0])\n    print(f'Image{i}Predicted value: {predicted_y}')    ","metadata":{"execution":{"iopub.status.busy":"2023-02-22T05:54:12.715952Z","iopub.execute_input":"2023-02-22T05:54:12.716313Z","iopub.status.idle":"2023-02-22T05:54:14.578810Z","shell.execute_reply.started":"2023-02-22T05:54:12.716284Z","shell.execute_reply":"2023-02-22T05:54:14.577852Z"},"trusted":true},"execution_count":null,"outputs":[]}]}