{"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\"))\ndigit = pd.read_csv(\"../input/train.csv\")\n####Import all required packages from sckit learn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics.pairwise import euclidean_distances\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom sklearn.metrics import accuracy_score\n###Split the data into train and test\ntrain1,validate = train_test_split(digit,test_size=0.3,random_state=100) \n##Drop label from input variables\ntrain_x = train1.drop('label', axis=1)\ntrain_y = train1['label']\n\ntest_x = validate.drop('label', axis=1)\ntest_y = validate['label']\n\n\n# Any results you write to the current directory are saved as output.\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2ae0575357be89dde24477eca35e9d901593b558"},"cell_type":"code","source":"## Decision Tree\n##Fit the decision tree model\nmodel = DecisionTreeClassifier(random_state=100,max_depth=15)\nmodel.fit(train_x, train_y)\n\n##Predict the decision tree model\n\ntest_pred = model.predict(test_x)\n##Accuracy of decision tree\ndt_acc =accuracy_score(test_y,test_pred)\ndt_acc\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cbba2b773e4ea01d4ae77ecfa54b2743f71ad35"},"cell_type":"code","source":"## Random Forest\n##Fit the random forest model\nmodel_randomforest = RandomForestClassifier(random_state=100,n_estimators=200)\nmodel_randomforest.fit(train_x, train_y)\n##Predict the random forest model\ntest_pred_rf = model_randomforest.predict(test_x)\n##Accuracy of random forest \nrf_acc = accuracy_score(test_y,test_pred_rf)\nrf_acc\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"## AdaBoost\n\nmodel_ab  = AdaBoostClassifier(random_state=100)\nmodel_ab.fit(train_x,train_y)\ntest_pred = model_ab.predict(test_x)\ndf_pred2 = pd.DataFrame({'actual' : test_y,\n                       'predicted' : test_pred})\ndf_pred2['pred_status'] = df_pred2['actual'] == df_pred2['predicted']\nacc_ab = df_pred2['pred_status'].sum() / df_pred2.shape[0] * 100\nacc_ab","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f565cff717ae24f2d7b15cf1f16ecab57867cc5"},"cell_type":"code","source":"test = pd.read_csv(\"../input/test.csv\")\ntest_digit_recognizer = model_randomforest.predict(test)\n\ndf_predict_digit = pd.DataFrame(test_digit_recognizer,columns=['Label'])\ndf_predict_digit['ImageId'] = test.index + 1\ndf_predict_digit.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"404cd2313a6043f640979516da0b386cfa5b577b"},"cell_type":"code","source":"### writing it to csv file\n\ndf_predict_digit[['ImageId','Label']].to_csv('ML-Assignment1.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}