{"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)\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.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_orig=pd.read_csv(\"../input/train.csv\")\ntest_orig=pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4fce2d61e0ef57ffdec90bb46611a4de333e54d"},"cell_type":"code","source":"y_train=train_orig['label'].values\nx_train=train_orig.drop(labels='label',axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"deb048b97bf86f31f6a38f08b8288411242845d9"},"cell_type":"code","source":"x_train=x_train.values\nx_test=test_orig.values\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e83ceca947bec4b885b860fc14dfad29f9ac6469"},"cell_type":"code","source":"x_train=x_train.reshape(x_train.shape[0],28,28,1)\nx_test=x_test.reshape(x_test.shape[0],28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5237bfac81db620cf4c3b135a45abe4e030f61f7"},"cell_type":"code","source":"print(\"dimension of training set\",x_train.shape)\nprint(\"dimension of test set\",x_test.shape)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f99662c5d8dc2257d559dc02f0a661a129b16cf"},"cell_type":"code","source":"#normalisation of the data\nx_train=x_train.astype(\"float32\")\nx_test=x_test.astype(\"float32\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6db6453642ee37514e527727dc3a90d5eac70a8e"},"cell_type":"code","source":"x_train=x_train/255\nx_test=x_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a47c9c68b3607425765d5fe96979358592048071"},"cell_type":"code","source":"import tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,Dropout,Flatten,MaxPool2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0f0d32a96d64bce0aa7c2fc1a6673499e867414"},"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=30,kernel_size=(3,3),input_shape=(28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation=tf.nn.relu))\nmodel.add(Dropout(.2))\nmodel.add(Dense(10,activation=tf.nn.softmax))\nmodel.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=['accuracy'])\nmodel.fit(x=x_train,y=y_train,epochs=30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"81be823bf7901169cc8c1644714b1dd0ea756bbf"},"cell_type":"code","source":"y_pred=model.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a3753327d4d89e40babbbe6cd99cdc1c12570f2f"},"cell_type":"code","source":"l=[]\nfor i in range(y_pred.shape[0]):\n    l.append(y_pred[i].argmax())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b34cb7f10a336a8d105a6566b5ed1c8c87443fee"},"cell_type":"code","source":"with open('predictions.csv', 'w') as predictions_file:\n    predictions_file.write('ImageId,Label' + '\\n')\n    for i in range(len(x_test)):\n        predictions_file.write(f'{i + 1},{l[i]}' + '\\n')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e30397df726f329132c8c526457ff131290ce91a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06d4cb7055646c6c3f58dd072fb7095e24756511"},"cell_type":"code","source":"\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e8796ce0848b42b55b84e20f6e57c0bc50dbad1"},"cell_type":"code","source":"","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}