{"cells":[{"metadata":{"_uuid":"ac55ffa929695b0ba2341dfd31b3bf0acbb255cc","_cell_guid":"83160d41-8c47-438d-b99d-89445aa8099c","trusted":false,"collapsed":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":null,"outputs":[]},{"metadata":{"_uuid":"7692f2a6e20c98bfae525a4f689d69e6bae14920","_cell_guid":"52ef0f3a-60b7-4279-b34e-c7816cce32ea","trusted":false,"collapsed":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":null,"outputs":[]},{"metadata":{"_uuid":"43b5635b243c551c135a8feefc84866745e286e6","_cell_guid":"0ac36e01-8308-4d2a-820a-de9c7d2e1fd2","trusted":false,"collapsed":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.6, random_state = 0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2ae8ef262c4c3fbdb6734d76ed2759d5bc5bdde8","_cell_guid":"a3405396-e3b8-4185-9502-ae98c43057ff","collapsed":true,"trusted":false},"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":null,"outputs":[]},{"metadata":{"_uuid":"5ee042f53a7a9b9708fca278e4e6eddc2c0f3020","_cell_guid":"19b1873a-a30e-4d1f-af67-fd67ff6da9a4","trusted":false,"collapsed":true},"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier\npract_five = X_train.iloc[4000, :]\nsgd_clf = SGDClassifier(random_state=0)\nsgd_clf.fit(X_train, y_train)\nsgd_clf.predict([pract_five])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"694146f46dec1fab0ee5eea5f6c91a92992b31b6","_cell_guid":"0c31b2dd-f146-4bee-9f56-7aecf9d39b4e","collapsed":true,"trusted":false},"cell_type":"code","source":"pract_score = sgd_clf.decision_function([pract_five])\nprint (pract_score)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"717408634deb8783a9c41fd44bb0ba2010dd3c90","_cell_guid":"d7725ae2-bd47-4fc2-9274-6b040efea895","collapsed":true,"trusted":false},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ny_predict = sgd_clf.predict(X_test)\nprint ((y_test != y_predict).sum())\nprint (accuracy_score(y_test, y_predict))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e27d9379a006235e11ee51d5e33ff43677f16a5e","_cell_guid":"db0d4876-8241-486b-9a19-4528b757a766","collapsed":true,"trusted":false},"cell_type":"code","source":"mnist_test = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2d90c89a6cd2387cf1fdad2f0ae4b808b29b21d0","_cell_guid":"77cdc595-c035-4abc-a286-d23b749f0061","collapsed":true,"trusted":false},"cell_type":"code","source":"print (mnist_test.info())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e6832be863402939fb9ab2307e7075b3d737376e","_cell_guid":"a32e5b52-fafb-42db-9bcf-6aad200625a0","collapsed":true,"trusted":false},"cell_type":"code","source":"yy_test = sgd_clf.predict(mnist_test)\nmnist_submission = pd.DataFrame({'ImageId': range(1,28001), 'Label' : yy_test})","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"57be03a1e44fc248588b7d8e7766e0e2290a6cd7","_cell_guid":"0a3992be-87c1-4427-a783-9ba241225688","collapsed":true,"trusted":false},"cell_type":"code","source":"print (mnist_submission.info())\nprint (mnist_submission.head())","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"eb68f058bc82efb9f9c245370c3c9d678da8f98e","_cell_guid":"c9f5db84-b573-4e88-bc8e-697c7d244431","collapsed":true,"trusted":false},"cell_type":"code","source":"mnist_submission.to_csv('accountant_mnist1.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"98df1b1ab1ba9bba1f125344c0baae8895021231","_cell_guid":"09047baa-39a4-4e58-a3a0-1ee1ce0a7323","collapsed":true},"cell_type":"markdown","source":"Result for SGD,  0.85028, 1828th, not very good...","outputs":[],"execution_count":null},{"metadata":{"_uuid":"740ff7c6ed484b724cc0bb2977adf04ee00b155f","_cell_guid":"4675562a-cab2-4dcf-8d30-75a45abf9ae5","trusted":false,"collapsed":true},"cell_type":"code","source":"def display_scores(scores):\n    print ('Scores: ', scores)\n    print ('Mean: ', scores.mean())\n    print ('STD: ', scores.std())\n\nfrom sklearn.model_selection import cross_val_score, cross_val_predict\nsgd_scores = cross_val_score(sgd_clf, X_train, y_train,\n                        scoring='accuracy', cv=3)\ndisplay_scores(sgd_scores)\n\nfrom sklearn.metrics import confusion_matrix\ny_pred = cross_val_predict(sgd_clf, X_train, y_train, cv=3)\nconfusion_matrix(y_train, y_pred)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fb1f9c1fd5116367f5d8220699a58d10939c25f8","_cell_guid":"93bbc9d3-6c3d-489f-8d7f-6ae96882805b","collapsed":true,"trusted":false},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"432567a3f7868d8ef32abec92404b35f86eb4ee1","_cell_guid":"b2176682-6e00-4109-b085-798c4123f6b9","trusted":false,"collapsed":true},"cell_type":"code","source":"neigh_clf = KNeighborsClassifier()\nneigh_clf.fit(X_train, y_train)\nneigh_scores = cross_val_score(neigh_clf, X_train, y_train, scoring='accuracy', cv=3)\n\ndisplay_scores(neigh_scores)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5d06894120d3b483ec498362c39655968142e266","_cell_guid":"41148b18-26d4-41fd-916a-a53d4702ffaa"},"cell_type":"markdown","source":"Ok, it sounds like the KNeighbors is a good model. Let´s see if I can improve it doing some grid search.","outputs":[],"execution_count":null},{"metadata":{"_uuid":"08f56cb392c5baed243777dbae0a3dd95f814562","_cell_guid":"3df0547e-9d97-4fe9-bdb3-7804eb77c010","collapsed":true,"trusted":false},"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"42574ae7fb24fed6469d54887fea8194c8164a63","_cell_guid":"3471eb26-f8d5-4698-85fb-91a80e7f272c","trusted":false,"collapsed":true},"cell_type":"code","source":"parameters_grid = [{'n_neighbors': [1, 3, 5]}]\ngrid_search = GridSearchCV(neigh_clf, parameters_grid, cv=3, scoring='accuracy')\ngrid_search.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9dcd8956d7abd738adefc5bae41ddf3013770e98","_cell_guid":"0258a2db-23a4-4b38-80a6-830509874274","trusted":false,"collapsed":true},"cell_type":"code","source":"print (grid_search.best_estimator_)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b5a05a635dd0ca260e56da382c6897cb4234fb2c","_cell_guid":"404a4194-8711-40ea-9aca-87e3775aaa36","trusted":false,"collapsed":true},"cell_type":"code","source":"print (grid_search.cv_results_)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d53f5cc467c3554801de645826639c54ec59fd03","_cell_guid":"373b0b28-eae8-4835-b5ae-6c612379a8cb","trusted":false,"collapsed":true},"cell_type":"code","source":"neigh_clf = KNeighborsClassifier(n_neighbors=1)\nneigh_clf.fit(X_train, y_train)\nyy_test = neigh_clf.predict(mnist_test)\nmnist_submission = pd.DataFrame({'ImageId': range(1,28001), 'Label' : yy_test})\nprint (mnist_submission.info())\nprint (mnist_submission.head())\nmnist_submission.to_csv('accountant_mnist2.csv', index=False)","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}