{"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\"))\ndata=pd.read_csv('../input/train.csv')\ntest=pd.read_csv('../input/test.csv')\nsample=pd.read_csv('../input/sample_submission.csv')\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Splitting data into train and validate\nfrom sklearn.model_selection import train_test_split\ntrain, validate=train_test_split(data, test_size=0.2, random_state=100)\ntrain_x=train.drop('label',axis=1)\ntrain_y=train['label']\nvalidate_x=validate.drop('label', axis=1)\nvalidate_y=validate['label']\n#print(train_x.shape, train_y.shape, validate_x.shape, validate_y.shape)\nfrom sklearn.metrics import accuracy_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e983b6ab57e07720455b9a755a6f718396c11782"},"cell_type":"code","source":"## Decision Tree model\n## Identified the optimum depth as 19 with most accuracy\nfrom sklearn.tree import DecisionTreeClassifier\n\nmodel_dt=DecisionTreeClassifier(max_depth=19, random_state=100)\nmodel_dt.fit(train_x, train_y)\nvalidate_pred_dt=model_dt.predict(validate_x)\naccuracy_dt=accuracy_score(validate_y, validate_pred_dt)\n#print(accuracy_dt)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd82881741cff4acd22ed1dfcd356dbf89bd9c29"},"cell_type":"code","source":"## Random Forest\n## Identified n_estimators as 500 with most accuracy\nfrom sklearn.ensemble import RandomForestClassifier\nmodel_rf=RandomForestClassifier(random_state=100,n_estimators=500)\nmodel_rf.fit(train_x,train_y)\nvalidate_pred_rf=model_rf.predict(validate_x)\naccuracy_rf=accuracy_score(validate_y, validate_pred_rf)\n#print(accuracy_rf)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b06f37673189e5ff15b39dfdfb015e322befcb4b"},"cell_type":"code","source":"## AdaBoost\nfrom sklearn.ensemble import AdaBoostClassifier\nmodel_ab=AdaBoostClassifier(random_state=100)\nmodel_ab.fit(train_x,train_y)\nvalidate_pred_ab=model_ab.predict(validate_x)\naccuracy_ab=accuracy_score(validate_y, validate_pred_ab)\n#print(accuracy_ab)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24277f2c517e98caa6e8385f7a24e8b8f1f9980d"},"cell_type":"code","source":"## KNN\n## KNN with 3 neighbors gives the most accuracy\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.metrics import accuracy_score\nmodel_knn=KNeighborsClassifier(n_neighbors=3)\nmodel_knn.fit(train_x, train_y)\nvalidate_pred_knn=model_knn.predict(validate_x)\naccuracy_knn=accuracy_score(validate_y, validate_pred_knn)\n#print(accuracy_knn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03a9f457f7581643c54c1075fad5c0cd7b97f25e"},"cell_type":"code","source":"# Using KNN for final prediction as it is providing the most accuracy.\n# Could have used Random Forest as it is giving almost same accuracy with less response time.\ndata_x=data.drop('label',axis=1)\ndata_y=data['label']\nmodel_knn.fit(data_x,data_y)\ntest_pred=model_knn.predict(test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53b3e936d3a03f37e80eba08eda728937027a079"},"cell_type":"code","source":"pred_df=pd.DataFrame({'ImageId':test.index.values+1,'Label':test_pred})\npred_df.to_csv('submission_3.csv', index=False)","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}