{"cells":[{"metadata":{"_uuid":"e36d38a4f30fd9a1f92664f5af0a78b58f19df25","_cell_guid":"0e7c9cd9-2aa7-4a1b-9454-60c4b8b8f7c6","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\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":"e1fcc4988370cc040ab19d6530aa4c721430cacf","_cell_guid":"05c4989c-2779-4cf4-832c-2e8984666f29","trusted":true},"cell_type":"code","source":"#Load other modules\nimport matplotlib.pyplot as plt  #Graphics\nimport seaborn as sns\nfrom sklearn.ensemble import RandomForestClassifier  #Random Forest algorithm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.model_selection import RandomizedSearchCV, GridSearchCV \nfrom sklearn.model_selection import cross_val_score\n#To show graphs within the notebook\n%matplotlib inline \n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ebda4490759bacb17c49e287b642d38b78dba21","collapsed":true,"_cell_guid":"65ab9b16-c6fa-4df2-b0a7-94982aea44f3","trusted":true},"cell_type":"code","source":"#load the datasets\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8b389d193143bdc22091824caef9ff0a4696b7a1","_cell_guid":"4cbc258d-7ae5-4b55-9b3a-6f7c6beb53ad","trusted":true},"cell_type":"code","source":"#Printing the size of the dataframe\nprint (\"Training dataset has %i observations and %i variables\" %(train.shape[0], train.shape[1]))\nprint (\"Testing dataset has %i observations and %i variables\" %(test.shape[0], test.shape[1]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2ed9cf897f026775aa7570062d041e89b02ab46b","_cell_guid":"f72341bc-0775-4658-84ad-d0b0d30fcfed","trusted":true},"cell_type":"code","source":"#check few rows\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1623346b812b05b3ea96a93f50a72745f67399ef","collapsed":true,"_cell_guid":"becde63b-f99a-4267-8297-141ae1376a5a","trusted":true},"cell_type":"code","source":"#Seperate the target and independant variables\ndf_x=train.iloc[:,1:]\ndf_y=train.iloc[:,0]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"11ffab42a1fcf15630b986a6feab3adcf55ca9c1","collapsed":true,"_cell_guid":"585f5ac0-7bea-4cde-a84a-6ff1183c5d22","trusted":true},"cell_type":"code","source":"def print_image(row, df):\n    temp=df.iloc[row,:].values\n    temp = temp.reshape(28,28).astype('uint8')\n    plt.imshow(temp)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"890dc2d91329e8b47388baeffaa0205dccfcd3c4","_cell_guid":"ab59e441-2096-452c-bae9-a2a39d6419e2","trusted":true},"cell_type":"code","source":"print_image(5, df_x)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"55cd55b7752a47c3719c25134b85756269e299bf","_cell_guid":"4c1a85d7-acb5-42d4-8c61-bac3e995cf8e","trusted":true},"cell_type":"code","source":"#Check the frequency of each number\ndf_y.value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8484f4d88b7c5ccaa772ce76ab1606f9157d708b","_cell_guid":"cb36bb37-82d4-4cd3-ac02-b863cbc62612","trusted":true},"cell_type":"code","source":"sns.countplot(df_y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"234bf61d2611372c497a6ab20447a7694ed1f612","_cell_guid":"43493f2e-bc4d-43de-bca1-112546baf4ed","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,10))\nfor i in range(40):\n    plt.subplot(4, 10, i+1)\n    print_image(i, test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"09ae524c95d0633db7ba745b1afc484273c51949","_cell_guid":"b39f5091-7da1-49e6-b225-e58883a53d67","trusted":true},"cell_type":"code","source":"#Split the dataset\nX_train, X_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.2, random_state=0)\nprint(X_train.shape)\nprint(X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f3ff4001949f010bf1c8ae201b10e10687ff5d7b","_cell_guid":"468d361a-8c3c-44c5-82a1-0f0348bf3f0e","trusted":true},"cell_type":"code","source":"#training random Forest\nrf=RandomForestClassifier(n_estimators=100)\nrf.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"43fbbde532db261b201c3298afbcff84f05a0d91","_cell_guid":"75612f96-8bfa-48d1-b741-61ca37760a6c","trusted":true},"cell_type":"code","source":"pred=rf.predict(X_test)\nprint (\"Classification Report\")\nprint(classification_report(y_test, pred))\nprint (\"Confusion Report\")\nprint(confusion_matrix(y_test, pred))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ae987c70ec76d34a88bd92c641faff7e16031321"},"cell_type":"markdown","source":"The accuracy is 96%","outputs":[],"execution_count":null},{"metadata":{"_uuid":"69660dba64d1d860910a2c4d2ab6cd462908bad4","_cell_guid":"9e9f4c50-d524-47b8-8d7a-2e75dc6934df","trusted":true},"cell_type":"code","source":"#Cross validation\nrf=RandomForestClassifier(n_estimators=100)\nrf.fit(df_x, df_y)\nscore = cross_val_score(rf, df_x, df_y)\nprint (np.mean(score))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"15700532eaa69fb9f363afd60bfa97211e3fdf25"},"cell_type":"markdown","source":"The accuracy has marginally increased to 96.21%\n\nEven after increasing the value for n_estimators, the accuracy stays in the range of 96% - 97%. ","outputs":[],"execution_count":null},{"metadata":{"trusted":true,"_uuid":"055a0cab3a3b0cdaf73a3a2faf46fddecb2567a2"},"cell_type":"code","source":"#Predicting on test data\npred=rf.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ad6a931e9754942b30e6f547b492d6a108e3865"},"cell_type":"code","source":"pred = pd.Series(pred,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),pred],axis = 1)\nsubmission.to_csv(\"mnist_rf.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"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"}},"nbformat":4,"nbformat_minor":1}