{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","collapsed":true,"trusted":true},"cell_type":"markdown","source":"In this notebook I built simple neural network using sklearn package."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"trusted":true},"cell_type":"code","source":"# import necessary packages\nimport pandas as pd\nfrom numpy import log\nimport matplotlib.pyplot as plt\nfrom sklearn.neural_network import MLPClassifier\n%matplotlib inline","execution_count":15,"outputs":[]},{"metadata":{"_uuid":"66bba8a725692fd66c3f7e1d7c28dc94f9a8cf51"},"cell_type":"markdown","source":"Import training and test data."},{"metadata":{"_cell_guid":"5b69770c-2976-482c-946a-c2118d0e54dd","_uuid":"645e1b572d58f7181154fe585c8dddfdd62c2071","collapsed":true,"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/train.csv')\ndf_test = pd.read_csv('../input/test.csv')","execution_count":17,"outputs":[]},{"metadata":{"_uuid":"59433f21bbb0e3a335613ffcd2f91cd81bf85266"},"cell_type":"markdown","source":"Separate labels from training set."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"ac783a5e24ea85b2ad406a4255d97210be7f5939"},"cell_type":"code","source":"df_train_y = df_train['label']\ndf_train_x = df_train.drop('label', axis=1)","execution_count":18,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3de1d5c934273fd2b1ae421437f2a452dd77e500"},"cell_type":"code","source":"df_train_x.iloc[0].shape","execution_count":34,"outputs":[]},{"metadata":{"_uuid":"f05816bf66437e42d7a402ec5cc5c546354e1589"},"cell_type":"markdown","source":"The drawing was flattened to a one-dimensional vector. To display it, change the size of the image to two-dimensional."},{"metadata":{"trusted":true,"_uuid":"b5c0e85edee55c10f36cc7b098b7adb46fa5cd4f"},"cell_type":"code","source":"plt.matshow(df_train_x.iloc[0].values.reshape(28,28))\nprint('Correct number: {}'.format(df_train_y.iloc[0]))","execution_count":19,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0bd4ea442985a855976be407522b62f1f49daf3f"},"cell_type":"code","source":"plt.matshow(df_train_x.iloc[20].values.reshape(28,28))\nprint('Correct number: {}'.format(df_train_y.iloc[20]))","execution_count":20,"outputs":[]},{"metadata":{"_uuid":"9a3b7768432c2412e5b72be7c4020876108eb1d9"},"cell_type":"markdown","source":"How many examples have we?"},{"metadata":{"trusted":true,"_uuid":"6b714e4ea50ffe2cfb1a40aeae1c64c6bd1a2d05"},"cell_type":"code","source":"df_train_x.shape","execution_count":21,"outputs":[]},{"metadata":{"_uuid":"a8cc20ec75e9f990701010a06a53c3713764e0c8"},"cell_type":"markdown","source":"The drawing has data in the range from 0 to 255."},{"metadata":{"trusted":true,"_uuid":"58f68b2cb6a532014f894ed505a177b9118b59b4"},"cell_type":"code","source":"df_train_x.max().max()","execution_count":22,"outputs":[]},{"metadata":{"_uuid":"6666467faa669c9ae6d8afa93466ee8dfe8cdc2b"},"cell_type":"markdown","source":"Normalize data  from 0 to 1."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"49d427f033421baf3685fdf34d4b86436daff54e"},"cell_type":"code","source":"df_train_x = df_train_x / 255\ndf_test = df_test / 255 ","execution_count":23,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"473cdd5cbe93e8cdca946c5512fb5ff9d49f30bc"},"cell_type":"code","source":"df_train_x.max().max()","execution_count":24,"outputs":[]},{"metadata":{"_uuid":"c37bb8e66e80c50058b6157efb32e428ef8b7c3f"},"cell_type":"markdown","source":"Build neural network model with 50 hidden layers."},{"metadata":{"trusted":true,"_uuid":"1ed5d59fcacb2ddc03dae237e7a2b32e785f75ca"},"cell_type":"code","source":"clf = MLPClassifier(hidden_layer_sizes=(50, 10))\nclf.fit(df_train_x, df_train_y)","execution_count":25,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"4348b83383eb32cb0479ea8ed044166396683dee"},"cell_type":"code","source":"train_result = clf.predict_proba(df_train_x)","execution_count":26,"outputs":[]},{"metadata":{"_uuid":"7ae6a25a561f8ce29d4b20cc68c99e262b1f8cce"},"cell_type":"markdown","source":"Create function to calculate loglos."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"bbbd4cc831c5d7f9ad6641240c91cfa062b3a3f3"},"cell_type":"code","source":"def logloss(df_pred, df_correct):\n    log_los = 0\n    for i, index in enumerate(df_correct.values):\n        log_los += -log(df_pred[i, index])\n    log_los /= len(df_correct)\n    return log_los","execution_count":27,"outputs":[]},{"metadata":{"_uuid":"e7364f6d71e54c6352bbaf3a7b114214e5a2afaa"},"cell_type":"markdown","source":"Loglos for training set."},{"metadata":{"trusted":true,"_uuid":"f2aa0e037c93f25f44e0858824e7a27571f442a6"},"cell_type":"code","source":"loglos_train = logloss(train_result, df_train_y)\nprint('Train loglos: {}'.format(loglos_train))","execution_count":28,"outputs":[]},{"metadata":{"_uuid":"6b30e00076ebbbb950ad76a81f3df7b2f3a88324"},"cell_type":"markdown","source":"Predict result for test set."},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"328e3c8600028ea327b1bd5710332302ec074fc5"},"cell_type":"code","source":"result = clf.predict(df_test)","execution_count":29,"outputs":[]},{"metadata":{"_uuid":"4b0bea385a1d2890e95470cf687aa5e1db114f4b"},"cell_type":"markdown","source":"Display test image and predictor for it."},{"metadata":{"trusted":true,"_uuid":"fd5e3d9ab415efc17f9c353f1b3adbc872da9d55"},"cell_type":"code","source":"plt.matshow(df_test.iloc[0].values.reshape(28,28))\nprint('Predict number: {}'.format(result[0]))","execution_count":30,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6d79ace39598a6da44cccc1788fd4cdb8843a68"},"cell_type":"code","source":"plt.matshow(df_test.iloc[3].values.reshape(28,28))\nprint('Predict number: {}'.format(result[3]))","execution_count":31,"outputs":[]},{"metadata":{"_uuid":"01f9afaffbd9e2f04661d15d37a3679545c5362e"},"cell_type":"markdown","source":"How many numbers are in the training set?"},{"metadata":{"trusted":true,"_uuid":"2ef9bf73cf775ac201535115d0bad083f2788212"},"cell_type":"code","source":"df_train_y.plot.hist(grid=True,bins=10)","execution_count":32,"outputs":[]},{"metadata":{"_uuid":"b9c84afba348cec9f814c622533c4cceaf749390"},"cell_type":"markdown","source":"How many numbers are in the predictor set?"},{"metadata":{"trusted":true,"_uuid":"6b0f0983b1a8822f754b42b324b2563338f7b6b7"},"cell_type":"code","source":"pd.DataFrame(result).plot.hist(grid=True,bins=10)","execution_count":33,"outputs":[]},{"metadata":{"_uuid":"4e4942a6b9bf5a27925ff374b35714e187bd363c"},"cell_type":"markdown","source":"Final result is 0.9597. \nThere's not so much code. It's very easy to implement this.\nNext step to improve this model is CNN. "}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}