{"cells":[{"metadata":{"_cell_guid":"83160d41-8c47-438d-b99d-89445aa8099c","_uuid":"ac55ffa929695b0ba2341dfd31b3bf0acbb255cc","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# Any results you write to the current directory are saved as output.","execution_count":15,"outputs":[]},{"metadata":{"_cell_guid":"52ef0f3a-60b7-4279-b34e-c7816cce32ea","_uuid":"7692f2a6e20c98bfae525a4f689d69e6bae14920","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib\n%matplotlib inline\n\nmnist = pd.read_csv('../input/train.csv')\nprint (mnist.info())","execution_count":16,"outputs":[]},{"metadata":{"_cell_guid":"0ac36e01-8308-4d2a-820a-de9c7d2e1fd2","_uuid":"43b5635b243c551c135a8feefc84866745e286e6","trusted":true},"cell_type":"code","source":"from sklearn.cross_validation import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(mnist.iloc[:, 1:], mnist.iloc[:, 0], \n                                                    test_size = 0.4, random_state = 0)","execution_count":17,"outputs":[]},{"metadata":{"_cell_guid":"a3405396-e3b8-4185-9502-ae98c43057ff","_uuid":"2ae8ef262c4c3fbdb6734d76ed2759d5bc5bdde8","trusted":true},"cell_type":"code","source":"plt.imshow(X_train.iloc[4000, :].values.reshape(28, 28), cmap=matplotlib.cm.binary)\nprint (y_train.iloc[4000])","execution_count":18,"outputs":[]},{"metadata":{"_cell_guid":"db0d4876-8241-486b-9a19-4528b757a766","_uuid":"e27d9379a006235e11ee51d5e33ff43677f16a5e","collapsed":true,"trusted":true},"cell_type":"code","source":"mnist_test = pd.read_csv('../input/test.csv')","execution_count":19,"outputs":[]},{"metadata":{"_cell_guid":"77cdc595-c035-4abc-a286-d23b749f0061","_uuid":"2d90c89a6cd2387cf1fdad2f0ae4b808b29b21d0","trusted":true},"cell_type":"code","source":"print (mnist_test.info())","execution_count":20,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abaa60937a49a5606a1b166943bdef0ab5e78f38"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation\nfrom keras.utils import np_utils","execution_count":21,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"240d3a9925bbf6a15ad842eae28f6a98ccc2b127"},"cell_type":"code","source":"print (X_train.shape, X_test.shape)","execution_count":22,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"953e4aafba1d3d4321c59cd1d8816c60ebe312a4"},"cell_type":"code","source":"X_train2 = X_train.astype('float32')\nX_test2 = X_test.astype('float32')\nX_train2 = X_train2 / 255\nX_test2 = X_test2 / 255","execution_count":23,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77a6ea2491296ba3781f9be5805812505fe6caeb"},"cell_type":"code","source":"print (y_train.shape, y_test.shape)\ny_train2 = np_utils.to_categorical(y_train, 10)\ny_test2 = np_utils.to_categorical(y_test, 10)\nprint (y_train2.shape, y_test2.shape)","execution_count":24,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d1fa1d399406e86837e4f0c488f04102991d2465"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(784, input_shape=(784,), kernel_initializer='normal'))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, kernel_initializer='normal'))\nmodel.add(Activation('softmax'))\n\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')\n\nhistory = model.fit(X_train2, y_train2,\n                   batch_size=200, epochs = 10,\n                   verbose = 2,\n                   validation_data = (X_test2, y_test2))","execution_count":25,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ade14048e5630bbd48f0c241afd8cef886b80df6"},"cell_type":"code","source":"print (mnist_test.shape)\nmnist_test2 = mnist_test / 255","execution_count":28,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3d6ebe3fb3106c9842e22de4f6185afc8bf26dfa"},"cell_type":"code","source":"yy_test = model.predict_classes(mnist_test2)","execution_count":31,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5975019a301c46458d14a2f0908c9b2f9327d06"},"cell_type":"code","source":"print (yy_test)\nprint (yy_test.shape)","execution_count":32,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b7617d9620c5e6a191743b3440ebf959579cb78"},"cell_type":"code","source":"mnist_submission = pd.DataFrame({'ImageId': range(1,28001), 'Label' : yy_test})\nprint (mnist_submission.info())\nprint (mnist_submission.head())\nmnist_submission.to_csv('accountant_mnist3.csv', index=False)","execution_count":33,"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}