{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30684,"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-04-08T23:25:00.081697Z","iopub.execute_input":"2024-04-08T23:25:00.082693Z","iopub.status.idle":"2024-04-08T23:25:00.093732Z","shell.execute_reply.started":"2024-04-08T23:25:00.082640Z","shell.execute_reply":"2024-04-08T23:25:00.092276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This notebook is accompanied by a Medium article: \n# https://python.plainenglish.io/starting-with-tensorflow-on-kaggle-0785fb83c86d\nimport tensorflow as tf\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout\n\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\nnum_neurons = 512\nnum_classes = 10\nepochs = 10","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:02.995898Z","iopub.execute_input":"2024-04-08T23:25:02.997188Z","iopub.status.idle":"2024-04-08T23:25:03.005184Z","shell.execute_reply.started":"2024-04-08T23:25:02.997139Z","shell.execute_reply":"2024-04-08T23:25:03.003847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:06.804391Z","iopub.execute_input":"2024-04-08T23:25:06.804802Z","iopub.status.idle":"2024-04-08T23:25:12.417732Z","shell.execute_reply.started":"2024-04-08T23:25:06.804771Z","shell.execute_reply":"2024-04-08T23:25:12.416339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:15.800910Z","iopub.execute_input":"2024-04-08T23:25:15.801406Z","iopub.status.idle":"2024-04-08T23:25:15.808568Z","shell.execute_reply.started":"2024-04-08T23:25:15.801368Z","shell.execute_reply":"2024-04-08T23:25:15.807092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=train_data.drop('label',axis=1).values.reshape(len(train_data),28,28,1)\ny_train=train_data.label.values\nx_test = test_data.values.reshape(len(test_data),28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:18.339836Z","iopub.execute_input":"2024-04-08T23:25:18.340344Z","iopub.status.idle":"2024-04-08T23:25:18.566784Z","shell.execute_reply.started":"2024-04-08T23:25:18.340308Z","shell.execute_reply":"2024-04-08T23:25:18.565525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:21.101111Z","iopub.execute_input":"2024-04-08T23:25:21.102115Z","iopub.status.idle":"2024-04-08T23:25:21.109010Z","shell.execute_reply.started":"2024-04-08T23:25:21.102057Z","shell.execute_reply":"2024-04-08T23:25:21.107637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train / 255.0\nx_test = x_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:23.192567Z","iopub.execute_input":"2024-04-08T23:25:23.193331Z","iopub.status.idle":"2024-04-08T23:25:23.467645Z","shell.execute_reply.started":"2024-04-08T23:25:23.193284Z","shell.execute_reply":"2024-04-08T23:25:23.466308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),\n                    MaxPooling2D(2,2),\n                    Conv2D(32, (3,3), activation='relu'),\n                    MaxPooling2D(2,2),\n                    Flatten(),\n                    Dropout(0.5),\n                    Dense(num_neurons, activation=tf.nn.relu), \n                    Dense(num_classes, activation=tf.nn.softmax)])\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(x_train, y_train, epochs=epochs)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:25:26.356346Z","iopub.execute_input":"2024-04-08T23:25:26.357200Z","iopub.status.idle":"2024-04-08T23:30:09.518137Z","shell.execute_reply.started":"2024-04-08T23:25:26.357155Z","shell.execute_reply":"2024-04-08T23:30:09.516895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test)\ny_pred = np.argmax(y_pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:30:13.548406Z","iopub.execute_input":"2024-04-08T23:30:13.549175Z","iopub.status.idle":"2024-04-08T23:30:19.353013Z","shell.execute_reply.started":"2024-04-08T23:30:13.549122Z","shell.execute_reply":"2024-04-08T23:30:19.351387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmit.Label = y_pred\nsubmit.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-08T23:30:22.388279Z","iopub.execute_input":"2024-04-08T23:30:22.388745Z","iopub.status.idle":"2024-04-08T23:30:22.456937Z","shell.execute_reply.started":"2024-04-08T23:30:22.388705Z","shell.execute_reply":"2024-04-08T23:30:22.455571Z"},"trusted":true},"execution_count":null,"outputs":[]}]}