{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"a7207d10-d6a4-ee0a-1a41-6cfc4e6eb06c"},"source":"Regularised neural network with cross validation."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c9e3ed5b-16d2-84a3-92b9-3254eb729350"},"outputs":[],"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\nfrom sklearn.neural_network import MLPClassifier\n\n#from scipy import ndimage, misc\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import GridSearchCV\n\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('whitegrid')\n\n%matplotlib inline\n\n\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"91d8d0b4-2214-e8b9-9cf3-16cd910b46d8"},"outputs":[],"source":"train_df = pd.read_csv('../input/train.csv', header=0)\ntest_df = pd.read_csv('../input/test.csv', header=0)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ef114790-df40-4dc8-2cd8-9e65fb3f9f22"},"outputs":[],"source":"train_df.head(10)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"842b8d4c-b534-edab-3e56-24f999bdfd1b"},"outputs":[],"source":"train_np = train_df.values\nX_train = train_np[:, 1:len(train_np[1, :])]\nY_train = train_np[:, 0]\n\nX_test = test_df.values"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c21c3127-afb2-d480-f021-12dc1bd4b1f8"},"outputs":[],"source":"X_train_bw = np.zeros(X_train.shape)\nX_test_bw = np.zeros(X_test.shape)\n\nfor row in range(len(X_train)):\n    for column in range(len(X_train[0,:])):\n        if X_train[row, column] > 0:\n            X_train_bw[row, column] = 1\n\n\n\nfor row in range(len(X_test)):\n    for column in range(len(X_test[0,:])):\n        if X_test[row, column] > 0:\n            X_test_bw[row, column] = 1\n\n            "},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"654d4a23-3054-9652-42cb-acc080d1e78f"},"outputs":[],"source":"clf = MLPClassifier(hidden_layer_sizes=(25, 25), activation='logistic')\n\nalpha_grid = [0.01, 0.03, 0.1]\n\nparam_grid = {\n    'alpha' : alpha_grid\n}\n\ngrid_search = GridSearchCV(clf, param_grid, cv=3)\n\ngrid_search.fit(X_train_bw, Y_train)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"0c22dd9f-b3dd-c412-44d3-01b9b50ae104"},"outputs":[],"source":"grid_search.best_params_"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"d27bba7a-4708-80b1-8b0f-c62dba293f47"},"outputs":[],"source":"grid_search.best_score_"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3576b394-2c99-aa22-c8f8-d656539e15a1"},"outputs":[],"source":"Y_pred = grid_search.predict(X_test_bw)\n\nImageId = [id + 1 for id in range(len(Y_pred))]\n\nsubmission = pd.DataFrame({\n    'ImageId' : ImageId,\n    'Label' : Y_pred\n})\n\nsubmission.to_csv('digit_recognizer_NN_bw_cv.csv', index=False)\nprint(check_output([\"ls\", \"../working\"]).decode(\"utf8\"))"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}