{"cells":[{"metadata":{"_uuid":"e331b719f773375547618762d384a50f23190084","_cell_guid":"f775f58a-a768-4453-92db-185446053483","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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n#### load dependencies\nimport keras\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.datasets import mnist\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.optimizers import SGD\nimport pandas as pd \nimport matplotlib.pyplot as plt,matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline\n\n# Load the data\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\n# Any results you write to the current directory are saved as output.\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1c55fa4c8c10b68e4a43bcfb14cafae6461af87b","_cell_guid":"d99d48df-38ce-405e-9b43-091e11c15d08","trusted":true,"collapsed":true},"cell_type":"code","source":"# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) \ny_train = train[\"label\"]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"92f4d51c0fd3e16611d2a6309f4a2dca7c5bf048","collapsed":true,"_cell_guid":"1c87210a-1828-4ea0-b428-7a5dbe1e9574","trusted":true},"cell_type":"code","source":"# Normalize the data\nX_train = X_train / 255.0\ntest = test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2a73e12d61671b6c9cfa4b7f193e78ebfe011c5","_cell_guid":"6bde54a3-3160-42a9-ba70-1b3627f8757c","trusted":true},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1cd30cfd5c6116ae75590cb54f9bfd2d92f71195","_cell_guid":"2e7f8b8e-4e8d-4b4e-9cd7-9d18d31029c0","trusted":true,"collapsed":true},"cell_type":"code","source":"X_train = X_train.values.reshape(42000,784).astype('float32')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4dc86c7d62564a72bf2ec4794b2111cc6425cf22","_cell_guid":"ddf6f3b4-9568-4c38-87af-9da2d010745e","trusted":true,"collapsed":true},"cell_type":"code","source":"n_classes = 10\ny_train = keras.utils.to_categorical(y_train,n_classes)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"52bf2b171e5ff417118c7359d12d8ed88e6ac421","_cell_guid":"e7fd5a07-af96-4130-a1cb-516b10306936","trusted":true},"cell_type":"code","source":"#train test split\nX_train, X_test,y_train, y_test = train_test_split(X_train, y_train, train_size=0.8, random_state=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4bd7296b1eebe1ca8e3464f81bd4b2fb8a31494"},"cell_type":"code","source":"y_test[0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"877460980227c77ed57b3e342463639d52f96844","_cell_guid":"33a27c6e-3bf5-4858-b8e0-431a866ad29e","trusted":true,"collapsed":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense((64),activation='relu',input_shape=(784,))) # 64 hidden layer, input = 784\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dense((10),activation='softmax')) #10 output","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"73771048c76064a6ec9ac5f57163067d1f043f77","_cell_guid":"41c0fafc-27e8-4ff2-adc0-55b37bd6ff20","trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"82bc0e1bcdd5b3e8be5f81400b3aee8d8e777def","collapsed":true,"_cell_guid":"34af9796-094e-4927-bb2a-d5a4a41669df","trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer=SGD(lr=0.1),metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ac1330adea292b8f0740f8ffb0b82036e35af6f","_cell_guid":"ad1c3758-5133-43e9-8838-69236e93f25d","trusted":true},"cell_type":"code","source":"model.fit(X_train,y_train,batch_size=128,epochs=100,verbose=1,validation_data=(X_test,y_test))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fe4ac7964db21b17ebebb91d4c8b90b4a795d902","_cell_guid":"00fbb422-ba91-43a2-92dc-a3813ff8ceea","trusted":true},"cell_type":"code","source":"model.evaluate(X_test,y_test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b89bb35db785f2560446c1d7df1ecda6375bdcd","collapsed":true,"_cell_guid":"647cbcdb-99c5-4cdd-a4cb-3311d0bd67a7","trusted":false},"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.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}