{"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.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout\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":{"trusted":true,"_uuid":"3301bd31bc76b5678b8005a099a4f7c8a48ffecd"},"cell_type":"code","source":"def prep_train_data(raw):\n    out_y = keras.utils.to_categorical(raw.label,num_classes=10)\n    number_of_images = raw.shape[0]\n    x_as_array = raw.values[:,1:]\n    x_tensor = x_as_array.reshape(number_of_images,28,28,1)\n    out_x = (x_tensor-128)/255\n    return out_x, out_y\n\ndef prep_test_data(raw):\n    number_of_images = raw.shape[0]\n    x_as_array = raw.values\n    x_tensor = x_as_array.reshape(number_of_images,28,28,1)\n    out_x = (x_tensor-128)/255\n    return out_x\n    ","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"raw_train_data = pd.read_csv(\"../input/train.csv\")\nraw_test_data = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30e18dcee8d7d742f39ba358fe4ce59af46887a7"},"cell_type":"code","source":"raw_test_data.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":false,"trusted":true,"_uuid":"116b57b59aad5e32c1474e7ed252d05a1dc14ef2"},"cell_type":"code","source":"x_train, y_train = prep_train_data(raw_train_data)\nx_test= prep_test_data(raw_test_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b1d82f5d9cbc14b7ebad5744298209620d73db8"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu',input_shape=(28, 28, 1)))\nmodel.add(Conv2D(20,kernel_size=(4,4),activation='relu'))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax')) #10 is the number of classes\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b658daba8012c5795c23529d9ee342b46d67e6d8"},"cell_type":"code","source":"model.compile(loss=keras.losses.categorical_crossentropy,\n              optimizer='adam',\n              metrics=['accuracy'])\nmodel.fit(x_train, y_train,\n          batch_size=128,\n          epochs=2,\n          validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0eb53904030c7c66ee118292e73960792781dffe"},"cell_type":"code","source":"output = pd.DataFrame({'Label':model.predict_classes(x_test)})\noutput.index += 1\noutput.to_csv('out.csv',index_label='ImageId')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1a05631963f2d452e9ac5cc2d0b45455d4231b7"},"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}