{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-01-04T17:15:00.483476Z","iopub.execute_input":"2024-01-04T17:15:00.483929Z","iopub.status.idle":"2024-01-04T17:15:00.918505Z","shell.execute_reply.started":"2024-01-04T17:15:00.483890Z","shell.execute_reply":"2024-01-04T17:15:00.917211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import Sequential\nfrom matplotlib import pyplot as plt ","metadata":{"execution":{"iopub.status.busy":"2024-01-04T17:48:40.707467Z","iopub.execute_input":"2024-01-04T17:48:40.707931Z","iopub.status.idle":"2024-01-04T17:48:40.863726Z","shell.execute_reply.started":"2024-01-04T17:48:40.707894Z","shell.execute_reply":"2024-01-04T17:48:40.862402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T17:53:18.143651Z","iopub.execute_input":"2024-01-04T17:53:18.144162Z","iopub.status.idle":"2024-01-04T17:53:21.304615Z","shell.execute_reply.started":"2024-01-04T17:53:18.144124Z","shell.execute_reply":"2024-01-04T17:53:21.303408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-04T17:53:38.340971Z","iopub.execute_input":"2024-01-04T17:53:38.341406Z","iopub.status.idle":"2024-01-04T17:53:38.361588Z","shell.execute_reply.started":"2024-01-04T17:53:38.341373Z","shell.execute_reply":"2024-01-04T17:53:38.360711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_df['label']\ntrain_df = train_df.drop(\"label\", axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T17:55:11.589061Z","iopub.execute_input":"2024-01-04T17:55:11.589544Z","iopub.status.idle":"2024-01-04T17:55:11.723346Z","shell.execute_reply.started":"2024-01-04T17:55:11.589506Z","shell.execute_reply":"2024-01-04T17:55:11.722012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**IMAGE AUGMENTATION**","metadata":{}},{"cell_type":"code","source":"#NORMALIZE\nX_train = train_df.values.astype('float32') / 255\nX_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\").values.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2024-01-04T17:57:02.788207Z","iopub.execute_input":"2024-01-04T17:57:02.788662Z","iopub.status.idle":"2024-01-04T17:57:04.547302Z","shell.execute_reply.started":"2024-01-04T17:57:02.788625Z","shell.execute_reply":"2024-01-04T17:57:04.546165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#RESHAPE\nX_train = X_train.reshape(-1, 28, 28)\nX_test = X_test.reshape(-1, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T18:03:13.943800Z","iopub.execute_input":"2024-01-04T18:03:13.945011Z","iopub.status.idle":"2024-01-04T18:03:13.950115Z","shell.execute_reply.started":"2024-01-04T18:03:13.944966Z","shell.execute_reply":"2024-01-04T18:03:13.948859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(2, 5, figsize=(10, 5))\nfor i in range(10):\n    mask = y_train==i\n    image=X_train[mask][0].reshape((28,28))\n    axs[i//5,i%5].set_title(f\"image {i}\")\n    axs[i//5,i%5].imshow(image, cmap = 'Greys')\n    axs[i//5,i%5].set_xticks([])\n    axs[i//5,i%5].set_yticks([])","metadata":{"execution":{"iopub.status.busy":"2024-01-04T18:03:36.901449Z","iopub.execute_input":"2024-01-04T18:03:36.902657Z","iopub.status.idle":"2024-01-04T18:03:37.743004Z","shell.execute_reply.started":"2024-01-04T18:03:36.902605Z","shell.execute_reply":"2024-01-04T18:03:37.741908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128,activation='relu', name=\"Dense_Layer\"))\nmodel.add(layers.Dense(10, activation='softmax', name=\"outputs\"))\n\nmodel.summary()\n\nmodel.compile(optimizer=keras.optimizers.RMSprop(),\n              loss=keras.losses.SparseCategoricalCrossentropy(),\n              metrics=['accuracy'])\n\ntf.keras.utils.plot_model(model,show_layer_names=True,expand_nested=True,show_shapes=True,rankdir=\"LR\")","metadata":{"execution":{"iopub.status.busy":"2024-01-04T18:18:05.724686Z","iopub.execute_input":"2024-01-04T18:18:05.725198Z","iopub.status.idle":"2024-01-04T18:18:06.210340Z","shell.execute_reply.started":"2024-01-04T18:18:05.725159Z","shell.execute_reply":"2024-01-04T18:18:06.209236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data = model.fit(X_train, y_train, batch_size=64, epochs=10, verbose=1, validation_split=0.2)\n\nresults = model.evaluate(X_train,y_train)\n\nprediction = model.predict(X_test)\nprediction1 = np.argmax(prediction, axis=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T18:28:24.063141Z","iopub.execute_input":"2024-01-04T18:28:24.063664Z","iopub.status.idle":"2024-01-04T18:32:00.531711Z","shell.execute_reply.started":"2024-01-04T18:28:24.063626Z","shell.execute_reply":"2024-01-04T18:32:00.530215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_res = pd.Series(prediction1, name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1, 28001), name=\"ImageId\"), final_res], axis=1)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T18:37:17.613649Z","iopub.execute_input":"2024-01-04T18:37:17.615113Z","iopub.status.idle":"2024-01-04T18:37:17.681511Z","shell.execute_reply.started":"2024-01-04T18:37:17.615071Z","shell.execute_reply":"2024-01-04T18:37:17.680309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}