{"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":"2023-10-14T19:57:28.101097Z","iopub.execute_input":"2023-10-14T19:57:28.101420Z","iopub.status.idle":"2023-10-14T19:57:28.111283Z","shell.execute_reply.started":"2023-10-14T19:57:28.101392Z","shell.execute_reply":"2023-10-14T19:57:28.110075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n%matplotlib inline\n\nlabeled_images = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nimages = labeled_images.iloc[:,1:]\nlabels = labeled_images.iloc[:,:1]\ntrain_images, test_images,train_labels, test_labels = train_test_split(images,\n                                            labels, train_size=0.8, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:57:43.005420Z","iopub.execute_input":"2023-10-14T19:57:43.006464Z","iopub.status.idle":"2023-10-14T19:57:46.794054Z","shell.execute_reply.started":"2023-10-14T19:57:43.006417Z","shell.execute_reply":"2023-10-14T19:57:46.793103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing an Image","metadata":{}},{"cell_type":"code","source":"i=1\nimg=train_images.iloc[i].values\nimg=img.reshape((28,28))\nplt.imshow(img,cmap='gray')\nplt.title(train_labels.iloc[i,0])","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:58:09.330735Z","iopub.execute_input":"2023-10-14T19:58:09.331281Z","iopub.status.idle":"2023-10-14T19:58:09.734964Z","shell.execute_reply.started":"2023-10-14T19:58:09.331243Z","shell.execute_reply":"2023-10-14T19:58:09.734015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(train_images.iloc[i])\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:58:14.772171Z","iopub.execute_input":"2023-10-14T19:58:14.772483Z","iopub.status.idle":"2023-10-14T19:58:15.020058Z","shell.execute_reply.started":"2023-10-14T19:58:14.772457Z","shell.execute_reply":"2023-10-14T19:58:15.019115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\n\nmodel = keras.Sequential([\n    layers.Dense(1024, activation='relu', input_shape=[784]),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(1024, activation='relu'),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(1024, activation='relu'),\n    layers.Dropout(0.3),\n    layers.BatchNormalization(),\n    layers.Dense(10, activation='softmax'),\n])\n\nmodel.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy'] \n)\n\nmodel.fit(\n    train_images, train_labels.values.ravel(),\n    validation_data=(test_images, test_labels),\n    batch_size=256,\n    epochs=100,\n    verbose=0,\n)\n\nloss, accuracy = model.evaluate(test_images, test_labels)\nprint('Test loss:', loss)\nprint('Test accuracy:', accuracy)\n\n#model.fit(train_images, train_labels.values.ravel())\n","metadata":{"execution":{"iopub.status.busy":"2023-10-14T19:58:20.407275Z","iopub.execute_input":"2023-10-14T19:58:20.407778Z","iopub.status.idle":"2023-10-14T20:00:46.156082Z","shell.execute_reply.started":"2023-10-14T19:58:20.407749Z","shell.execute_reply":"2023-10-14T20:00:46.154961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T20:03:22.464035Z","iopub.execute_input":"2023-10-14T20:03:22.464374Z","iopub.status.idle":"2023-10-14T20:03:23.404302Z","shell.execute_reply.started":"2023-10-14T20:03:22.464348Z","shell.execute_reply":"2023-10-14T20:03:23.403388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-14T20:04:40.127343Z","iopub.execute_input":"2023-10-14T20:04:40.127712Z","iopub.status.idle":"2023-10-14T20:04:40.135470Z","shell.execute_reply.started":"2023-10-14T20:04:40.127684Z","shell.execute_reply":"2023-10-14T20:04:40.134365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n#test_data[test_data>0]=1\nresults=model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T20:20:19.381214Z","iopub.execute_input":"2023-10-14T20:20:19.381544Z","iopub.status.idle":"2023-10-14T20:20:24.109084Z","shell.execute_reply.started":"2023-10-14T20:20:19.381519Z","shell.execute_reply":"2023-10-14T20:20:24.108126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-14T15:52:14.068094Z","iopub.execute_input":"2023-10-14T15:52:14.068454Z","iopub.status.idle":"2023-10-14T15:52:14.075773Z","shell.execute_reply.started":"2023-10-14T15:52:14.068410Z","shell.execute_reply":"2023-10-14T15:52:14.074380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results","metadata":{"execution":{"iopub.status.busy":"2023-10-14T20:20:40.422575Z","iopub.execute_input":"2023-10-14T20:20:40.422931Z","iopub.status.idle":"2023-10-14T20:20:40.430620Z","shell.execute_reply.started":"2023-10-14T20:20:40.422906Z","shell.execute_reply":"2023-10-14T20:20:40.429430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_dir = '/kaggle/working/'\nos.makedirs(output_dir, exist_ok=True)\n\ndf = pd.DataFrame(results, columns=[str(i) for i in range(10)])\ndf.index.name = 'ImageId'\ndf.index += 1\ndf = df.idxmax(axis=1)\ndf.name = 'Label'\ndf.to_csv(os.path.join(output_dir, 'results.csv'), header=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-14T20:21:25.591289Z","iopub.execute_input":"2023-10-14T20:21:25.591644Z","iopub.status.idle":"2023-10-14T20:21:25.655526Z","shell.execute_reply.started":"2023-10-14T20:21:25.591609Z","shell.execute_reply":"2023-10-14T20:21:25.654654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-14T15:58:13.345684Z","iopub.execute_input":"2023-10-14T15:58:13.346323Z","iopub.status.idle":"2023-10-14T15:58:13.355038Z","shell.execute_reply.started":"2023-10-14T15:58:13.346294Z","shell.execute_reply":"2023-10-14T15:58:13.353198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}