{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f64486f94eae5bbb069652f75ba44b49404a09cf"},"cell_type":"markdown","source":"Let's read in the data and look at its shape:"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4e374fb9688ef08f150cb0b046ce32b5f5e322d4"},"cell_type":"markdown","source":"Useful packages"},{"metadata":{"trusted":true,"_uuid":"fdd0d8c98a31c5ab7a464952d6c44b185e7ef61a"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03e1dbdb449f8515617dd3d30f838f5b1d18c4a6"},"cell_type":"code","source":"y = data['label']\nX = data.drop(['label'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"75b8ea710aaa205b6641863f9df0bdf9dbd1cd7b"},"cell_type":"markdown","source":"Making a test set to make sure the model works well and finally we test the model with a few different number of estimators to find the best choice."},{"metadata":{"trusted":true,"_uuid":"33067133aa8ca286e295d051a4440722d6fb6f2c"},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1)\n#cross validating for number of estimators\nNs = [50, 100, 150, 200, 250, 300]; Accuracies = []\nfor n in Ns:\n    clf = RandomForestClassifier(n_estimators = n)\n    clf.fit(X_train, y_train)\n    score = clf.score(X_test, y_test)\n    Accuracies.append(score)\n    print('\\r Accuracy:{}'.format(score), end = \"\")\nMax_score = max(Accuracies)\nBest_N = Ns[Accuracies.index(Max_score)]\nprint( '\\n Best Score:', Max_score, Best_N )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"403f1ab40b60900a812a7a614c87bd99ce5074a6"},"cell_type":"markdown","source":"Now we use the parameter that gave us the best accuracy to retrain our model."},{"metadata":{"trusted":true,"_uuid":"5fd12e109975197e8feae84840da607b0f8d7428"},"cell_type":"code","source":"clf = RandomForestClassifier(n_estimators = Best_N)\nclf.fit(X, y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3b76fe61269a87ac59c5f5ba5638f3f9173b5dd6"},"cell_type":"markdown","source":"Reading in the test set and applying the model on it:"},{"metadata":{"trusted":true,"_uuid":"c89966505b79279c5d32abd0663867ce3d73cd95"},"cell_type":"code","source":"test_data = pd.read_csv('../input/test.csv')\ntest_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3716745077165147683f8df4509c4cbfca9c5ffe"},"cell_type":"code","source":"prediction = clf.predict(test_data)\nprint (prediction)\nImageID = range(1, len(prediction)+1)\noutput = {'ImageId': ImageID, 'Label': prediction}\nOutput = pd.DataFrame.from_dict(output)\nOutput.set_index('ImageId', inplace = True)\nprint (Output.head())\nOutput.to_csv('./Submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8ef1104ee19d20b61ab5bbec29c4c34a850bd0f"},"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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}