{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn import tree\nimport pydotplus\nfrom sklearn.tree import DecisionTreeClassifier\nimport matplotlib.pyplot as plt\nimport matplotlib.image as pltimg\nimport numpy as np","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# setting the display option to maximum\npd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\npd.set_option('display.width', None)\npd.set_option('display.max_colwidth', -1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Train and Test data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('Kaggle-digit-train.csv')\n\ntest_data = pd.read_csv('Kaggle-digit-test.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the data structure\nprint(train_data.info())\n\n# checking for missing and null values in the given dataset\nprint(train_data.isnull().sum())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see that there are no null or missing values in the dataset.","metadata":{}},{"cell_type":"code","source":"# Identifying the counts of distinct labels\ntrain_data['label'].value_counts()","metadata":{"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splitting the Features and Target data for training and testing datasets\nfeatures_train = train_data.drop(['label'],axis = 1)\ntarget_train = pd.DataFrame(train_data['label'])\nfeatures_test = test_data.drop(['label'],axis = 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting histogram for target variable in train_data to see the data distribution.\nplt.hist(target_train)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see our target data is near to uniform distribution, thus the data is not normally distributed.","metadata":{}},{"cell_type":"markdown","source":"## Let's build Decision Tree Models","metadata":{}},{"cell_type":"markdown","source":"##### Applying Cross validation to first decision tree model using default hyperparameters with 3 folds","metadata":{}},{"cell_type":"code","source":"# Using train_data train the model with default criterion \"gini\"\ndt = DecisionTreeClassifier()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing library to perform k-fold cross validation\nfrom sklearn.model_selection import cross_val_score\n\n# Passing full data X and y because the K-fold will split the data and automatically choose train/test\naccuracies_dt = cross_val_score(estimator = dt, X = features_train, y = target_train, cv = 3) \nprint('The mean values of the accuracies is: ',accuracies_dt.mean()) \nprint('-----------------------------------')\nprint('The mean deviation of the accuracies is: ',accuracies_dt.std())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The mean value for the accuracies is ~85% with a mean deviation of ~0.06%. That means our model is accurate for ~85% of time.","metadata":{}},{"cell_type":"markdown","source":"##### Applying Cross validation to first decision tree model using criterion entropy with 3 folds","metadata":{}},{"cell_type":"code","source":"# Using train_data train the model with with criterion \"entropy\"\ndt2 = DecisionTreeClassifier(criterion = \"entropy\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Passing full data X and y because the K-fold will split the data and automatically choose train/test\naccuracies_dt2 = cross_val_score(estimator = dt2, X = features_train, y = target_train, cv = 3) \nprint('The mean values of the accuracies is: ',accuracies_dt2.mean()) \nprint('-----------------------------------')\nprint('The mean deviation of the accuracies is: ',accuracies_dt2.std())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The mean value for the accuracies is ~86% with a mean deviation of ~0.2%. That means our model is accurate for ~86% of time.","metadata":{}},{"cell_type":"markdown","source":"##### Applying Cross validation to first decision tree model using criterion entropy , max_depth = 10, max_leaf_nodes = 20, random_state = 42 with 3 folds","metadata":{}},{"cell_type":"code","source":"# Using train_data to train the model with criterion \"entropy\", max_depth = 10, max_leaf_nodes = 20, random_state = 42\ndt3 = DecisionTreeClassifier(criterion = \"entropy\", max_depth = 10, max_leaf_nodes = 20, random_state = 42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Passing full data X and y because the K-fold will split the data and automatically choose train/test\naccuracies_dt3 = cross_val_score(estimator = dt2, X = features_train, y = target_train, cv = 3) \nprint('The mean values of the accuracies is: ',accuracies_dt3.mean()) \nprint('-----------------------------------')\nprint('The mean deviation of the accuracies is: ',accuracies_dt3.std())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The mean value for the accuracies is ~86% with a mean deviation of ~0.2%. That means our model is accurate for ~86% of time.","metadata":{}},{"cell_type":"markdown","source":"##### Fitting and predicting the data using Decision Tree model (dt2) with highest accuracy","metadata":{}},{"cell_type":"code","source":"dt2.fit(features_train,target_train)\ntest_data_pred = dt2.predict(features_test)\npd.DataFrame(test_data_pred).value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting histogram for predicted data to see the data distribution.\nplt.hist(test_data_pred)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see our predicted result plot is uniformly distributed and closely resemble to the plot obtained with train data.","metadata":{}},{"cell_type":"markdown","source":"# Naive Bayes","metadata":{}},{"cell_type":"markdown","source":"##### Applying Cross validation to Multinomial Naive Bayes model with 3 folds\n\nWe have used Multinomial Naive Bayes model because our data is large and uniformly distributed; and has multiple classes in the target variable.","metadata":{}},{"cell_type":"code","source":"# importing library to generate Multinomial Naive Bayes model\nfrom sklearn.naive_bayes import MultinomialNB\n \n#Calling the Class\nmnb = MultinomialNB(alpha = 0.02)\n\n# Passing full data X and y because the K-fold will split the data and automatically choose train/test\naccuracies_mnb = cross_val_score(estimator = mnb, X = features_train, y = target_train, cv = 3) \nprint('The mean values of the accuracies is: ',accuracies_mnb.mean()) \nprint('-----------------------------------')\nprint('The mean deviation of the accuracies is: ',accuracies_mnb.std())","metadata":{"scrolled":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The mean value for the accuracies is ~82% with a mean deviation of ~0.2%. That means our model is accurate for ~82% of time.","metadata":{}},{"cell_type":"markdown","source":"##### Fitting and predicting the data using Multinomial Naive Bayes model ","metadata":{}},{"cell_type":"code","source":"mnb.fit(features_train,target_train)\ntest_data_pred2 = mnb.predict(features_test)\npd.DataFrame(test_data_pred2).value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting histogram for predicted data to see the data distribution.\nplt.hist(test_data_pred2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see our predicted result plot is uniformly distributed and closely resemble to the plot obtained with train data.","metadata":{}},{"cell_type":"markdown","source":"Based on above two classifications we can say that decision tree classifier creates better model because of more accuracy than the Naive Bayes model in solving the classification problem of handwritten digits from 0 to 9.","metadata":{}},{"cell_type":"code","source":"#tree.plot_tree(dt2,feature_names = features_test.columns, class_names=['0','1','2','3','4','5','6','7','8','9'], filled = True);","metadata":{},"execution_count":null,"outputs":[]}]}