{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef prep_data(raw):\n    y = raw[:, 0]\n    out_y = keras.utils.to_categorical(y, num_classes)\n    \n    x = raw[:,1:]\n    num_images = raw.shape[0]\n    out_x = x.reshape(num_images, img_rows, img_cols, 1)\n    out_x = out_x / 255\n    return out_x, out_y\n\ndigit_file = \"../input/train.csv\"\ndigit_data = np.loadtxt(digit_file, skiprows=1, delimiter=',')\nx, y = prep_data(digit_data)\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout\n\nbatch_size = 16\n#Model Arch (Conv2D-->Conv2D--->Flatten--->Dense--->Dense)\ndigit_model = Sequential()\ndigit_model.add(Conv2D(16, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\ndigit_model.add(Conv2D(16, (3, 3), activation='relu'))\ndigit_model.add(Flatten())\ndigit_model.add(Dense(128, activation='relu'))\ndigit_model.add(Dense(num_classes, activation='softmax'))\n\ndigit_model.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\ndigit_model.fit(x, y,\n          batch_size=batch_size,\n          epochs=3,\n          validation_split = 0.2)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bf9a2d0878300f68ec27f8834aec0f97f51307de"},"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, Dropout\n\nbatch_size = 16\n\ndigit_model = Sequential()\ndigit_model.add(Conv2D(16, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\ndigit_model.add(Conv2D(16, (3, 3), activation='relu', strides=2))\ndigit_model.add(Flatten())\ndigit_model.add(Dense(128, activation='relu'))\ndigit_model.add(Dense(num_classes, activation='softmax'))\n\ndigit_model.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\n\ndigit_model.fit(x, y,\n          batch_size=batch_size,\n          epochs=10,\n          validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e45ca00d84d76620884d447d5032ab9b09daba3"},"cell_type":"code","source":"results=digit_model.predict(x)\n\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54b19b1091abfb0d5352b1b9f5e2d3897a002174"},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False,quoting=csv.QUOTENONNUMERIC)\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}