{"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":{"id":"YTqw7Srz01Jl"}},{"cell_type":"code","source":"%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix\nimport pylab as pl\nimport os","metadata":{"id":"9rVDWdSMwZ76","execution":{"iopub.status.busy":"2022-08-19T09:51:35.635127Z","iopub.execute_input":"2022-08-19T09:51:35.635665Z","iopub.status.idle":"2022-08-19T09:51:36.443653Z","shell.execute_reply.started":"2022-08-19T09:51:35.635531Z","shell.execute_reply":"2022-08-19T09:51:36.442610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nval= pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')","metadata":{"id":"ttke7dsx06Wt","execution":{"iopub.status.busy":"2022-08-19T09:52:47.754633Z","iopub.execute_input":"2022-08-19T09:52:47.755019Z","iopub.status.idle":"2022-08-19T09:52:55.348399Z","shell.execute_reply.started":"2022-08-19T09:52:47.754988Z","shell.execute_reply":"2022-08-19T09:52:55.346942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"id":"l9JouuY21AN0","outputId":"4b8b56f9-cac7-49ce-d492-811071d3ea4d","execution":{"iopub.status.busy":"2022-08-19T09:52:55.350393Z","iopub.execute_input":"2022-08-19T09:52:55.350800Z","iopub.status.idle":"2022-08-19T09:52:55.379511Z","shell.execute_reply.started":"2022-08-19T09:52:55.350757Z","shell.execute_reply":"2022-08-19T09:52:55.378393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.rename(columns={'id':'label'}, inplace=True)\ntest.head()","metadata":{"id":"ddvJlj4N10EF","outputId":"04ae02ad-1cc5-40fd-a7b0-972aa402f799","execution":{"iopub.status.busy":"2022-08-19T09:52:55.380833Z","iopub.execute_input":"2022-08-19T09:52:55.381191Z","iopub.status.idle":"2022-08-19T09:52:55.405852Z","shell.execute_reply.started":"2022-08-19T09:52:55.381157Z","shell.execute_reply":"2022-08-19T09:52:55.404597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"id":"yZGApWf3112l","outputId":"68136c4b-4e39-43aa-db65-7f6e9b964dec","execution":{"iopub.status.busy":"2022-08-19T09:52:55.408294Z","iopub.execute_input":"2022-08-19T09:52:55.408642Z","iopub.status.idle":"2022-08-19T09:52:55.430044Z","shell.execute_reply.started":"2022-08-19T09:52:55.408612Z","shell.execute_reply":"2022-08-19T09:52:55.428929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train Shape: ', train.shape)\nprint('Test Shape:',test.shape)\nprint('Submission Shape: ',submission.shape)\nprint('Validation Shape: ',val.shape)","metadata":{"id":"gYcXJvWR13Pw","outputId":"52761666-59d9-4f35-cdff-223b297873cc","execution":{"iopub.status.busy":"2022-08-19T09:52:55.431318Z","iopub.execute_input":"2022-08-19T09:52:55.431669Z","iopub.status.idle":"2022-08-19T09:52:55.437771Z","shell.execute_reply.started":"2022-08-19T09:52:55.431639Z","shell.execute_reply":"2022-08-19T09:52:55.436939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(by='label').size()","metadata":{"id":"gGBJL0XC14u2","outputId":"b7597029-145d-4574-b255-2b3298c4eafd","execution":{"iopub.status.busy":"2022-08-19T09:52:55.439044Z","iopub.execute_input":"2022-08-19T09:52:55.439613Z","iopub.status.idle":"2022-08-19T09:52:55.458533Z","shell.execute_reply.started":"2022-08-19T09:52:55.439579Z","shell.execute_reply":"2022-08-19T09:52:55.457535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare Train and Test","metadata":{"id":"ajRCfoef2UIr"}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train.iloc[:, 1:], train.iloc[:, 0], test_size=0.2)","metadata":{"id":"obHvMldK2R5f","execution":{"iopub.status.busy":"2022-08-19T09:52:55.459839Z","iopub.execute_input":"2022-08-19T09:52:55.460368Z","iopub.status.idle":"2022-08-19T09:52:55.904332Z","shell.execute_reply.started":"2022-08-19T09:52:55.460337Z","shell.execute_reply":"2022-08-19T09:52:55.902888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"id":"9PrpM0za2alE","outputId":"491404fe-6233-47f3-cb66-b4af9306c37b","execution":{"iopub.status.busy":"2022-08-19T09:52:55.905775Z","iopub.execute_input":"2022-08-19T09:52:55.906821Z","iopub.status.idle":"2022-08-19T09:52:55.925152Z","shell.execute_reply.started":"2022-08-19T09:52:55.906782Z","shell.execute_reply":"2022-08-19T09:52:55.924221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.head()","metadata":{"id":"ePyMSvoE2cEh","outputId":"738b9519-e0e1-4cd4-a0e4-c9ef234d2c21","execution":{"iopub.status.busy":"2022-08-19T09:52:55.926335Z","iopub.execute_input":"2022-08-19T09:52:55.927308Z","iopub.status.idle":"2022-08-19T09:52:55.954233Z","shell.execute_reply.started":"2022-08-19T09:52:55.927275Z","shell.execute_reply":"2022-08-19T09:52:55.953074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization","metadata":{"id":"xZpGuu6C2gA0"}},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=10, ncols=10, figsize=(10,10))\nfor i in range(10):\n    num_i = X_train[y_train == i]\n    ax[0][i].set_title(i)\n    for j in range(10):\n        ax[j][i].axis('off')\n        ax[j][i].imshow(num_i.iloc[j, :].to_numpy().astype(np.uint8).reshape(28, 28), cmap='gray')","metadata":{"id":"1FPqVTwR2dzn","outputId":"b17199ab-bc83-4179-82f8-6a50d73bff99","execution":{"iopub.status.busy":"2022-08-19T09:52:55.957337Z","iopub.execute_input":"2022-08-19T09:52:55.957711Z","iopub.status.idle":"2022-08-19T09:52:59.533184Z","shell.execute_reply.started":"2022-08-19T09:52:55.957678Z","shell.execute_reply":"2022-08-19T09:52:59.532309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Machine Learning Algorithm","metadata":{"id":"NtePIP8j3qvs"}},{"cell_type":"markdown","source":"### Logistic Regression","metadata":{"id":"jA0y-Wdv33Gc"}},{"cell_type":"code","source":"# LogisticRegression\nfrom sklearn.linear_model import LogisticRegression\nModelLR = LogisticRegression(C=5, solver='lbfgs', multi_class='multinomial')\nModelLR.fit(X_train, y_train)\n\ny_predLR = ModelLR.predict(X_test)\n\n# Accuracy score\nprint('accuracy is',accuracy_score(y_predLR,y_test))\n\nscore = accuracy_score(y_predLR,y_test)","metadata":{"id":"d_jBpQda2rFn","outputId":"293cecb3-4b8b-4753-dd6c-14ee10c54702","execution":{"iopub.status.busy":"2022-08-19T09:52:59.534543Z","iopub.execute_input":"2022-08-19T09:52:59.535292Z","iopub.status.idle":"2022-08-19T09:53:21.360702Z","shell.execute_reply.started":"2022-08-19T09:52:59.535247Z","shell.execute_reply":"2022-08-19T09:53:21.359453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_predLR)\nprint(cm)","metadata":{"id":"8nKtwB7a358h","outputId":"c6ed3ed5-607f-425a-dae6-2f6590ac73e0","execution":{"iopub.status.busy":"2022-08-19T09:53:21.362816Z","iopub.execute_input":"2022-08-19T09:53:21.363705Z","iopub.status.idle":"2022-08-19T09:53:21.380143Z","shell.execute_reply.started":"2022-08-19T09:53:21.363655Z","shell.execute_reply":"2022-08-19T09:53:21.378891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9,9))\nsns.heatmap(cm, annot=True, fmt=\".3f\", linewidths=.5, square = True, cmap = 'Blues_r');\nplt.ylabel('Actual label');\nplt.xlabel('Predicted label');\nall_sample_title = 'Accuracy Score: {0}'.format(score)\nplt.title(all_sample_title, size = 15);","metadata":{"id":"Q_AfW4lm3-RC","outputId":"9638ad21-9f3e-4d0d-c059-b8f3b3d688df","execution":{"iopub.status.busy":"2022-08-19T09:53:21.384058Z","iopub.execute_input":"2022-08-19T09:53:21.385030Z","iopub.status.idle":"2022-08-19T09:53:22.118437Z","shell.execute_reply.started":"2022-08-19T09:53:21.384966Z","shell.execute_reply":"2022-08-19T09:53:22.117127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Decision Tree","metadata":{"id":"TFA1tnxQ4BlU"}},{"cell_type":"code","source":"# Seed for reproducability\nseed = 1234\nnp.random.seed(seed)","metadata":{"id":"wkKF00MX3_rI","execution":{"iopub.status.busy":"2022-08-19T09:53:22.119867Z","iopub.execute_input":"2022-08-19T09:53:22.120226Z","iopub.status.idle":"2022-08-19T09:53:22.125521Z","shell.execute_reply.started":"2022-08-19T09:53:22.120193Z","shell.execute_reply":"2022-08-19T09:53:22.124598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier, export_graphviz\nDT = DecisionTreeClassifier(max_depth=10, random_state=seed)\nDT.fit(X_train, y_train)","metadata":{"id":"srJ8SgtU4EOK","outputId":"b9d179f1-b299-42d3-98f5-59d5b8dc5152","execution":{"iopub.status.busy":"2022-08-19T09:53:22.126966Z","iopub.execute_input":"2022-08-19T09:53:22.127568Z","iopub.status.idle":"2022-08-19T09:53:28.488446Z","shell.execute_reply.started":"2022-08-19T09:53:22.127520Z","shell.execute_reply":"2022-08-19T09:53:28.487317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predDT = DT.predict(X_test)\nprint('accuracy DT',accuracy_score(y_predDT,y_test))\nscoreDT= accuracy_score(y_predDT,y_test)","metadata":{"id":"cIQeJ0Zy7ElK","outputId":"a3b44ba5-efa3-4405-8fc9-0b429065ff5e","execution":{"iopub.status.busy":"2022-08-19T09:53:28.489886Z","iopub.execute_input":"2022-08-19T09:53:28.490610Z","iopub.status.idle":"2022-08-19T09:53:28.530145Z","shell.execute_reply.started":"2022-08-19T09:53:28.490568Z","shell.execute_reply":"2022-08-19T09:53:28.528776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DTm =confusion_matrix(y_test, y_predDT)\nprint(DTm)","metadata":{"id":"l4Xpxvf-7Mdq","outputId":"234db510-d162-4c74-d671-572390dfa08e","execution":{"iopub.status.busy":"2022-08-19T09:53:28.531545Z","iopub.execute_input":"2022-08-19T09:53:28.531915Z","iopub.status.idle":"2022-08-19T09:53:28.541484Z","shell.execute_reply.started":"2022-08-19T09:53:28.531873Z","shell.execute_reply":"2022-08-19T09:53:28.540398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9,9))\nsns.heatmap(DTm, annot=True, fmt=\".3f\", linewidths=.5, square = True, cmap = 'Blues_r');\nplt.ylabel('Actual label');\nplt.xlabel('Predicted label');\nall_sample_title = 'Accuracy Score Desicion Tree: {0}'.format(scoreDT)\nplt.title(all_sample_title, size = 15);","metadata":{"id":"33jEI9TG84V2","outputId":"6a9b5221-50be-42fd-e8c4-7034f6cc5635","execution":{"iopub.status.busy":"2022-08-19T09:53:28.545398Z","iopub.execute_input":"2022-08-19T09:53:28.545809Z","iopub.status.idle":"2022-08-19T09:53:29.463520Z","shell.execute_reply.started":"2022-08-19T09:53:28.545767Z","shell.execute_reply":"2022-08-19T09:53:29.462425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PCA svm","metadata":{"id":"eMrEcXjH9HVp"}},{"cell_type":"code","source":"from sklearn import svm\nfrom sklearn.decomposition import PCA","metadata":{"id":"U7F7thh09FYy","execution":{"iopub.status.busy":"2022-08-19T09:53:29.465015Z","iopub.execute_input":"2022-08-19T09:53:29.465953Z","iopub.status.idle":"2022-08-19T09:53:29.472648Z","shell.execute_reply.started":"2022-08-19T09:53:29.465904Z","shell.execute_reply":"2022-08-19T09:53:29.471746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=0.7,whiten=True)\nX_train_PCA = pca.fit_transform(X_train)\nX_test_PCA = pca.transform(X_test)","metadata":{"id":"K28f7v2I9K3V","execution":{"iopub.status.busy":"2022-08-19T09:53:29.473947Z","iopub.execute_input":"2022-08-19T09:53:29.475198Z","iopub.status.idle":"2022-08-19T09:53:35.306952Z","shell.execute_reply.started":"2022-08-19T09:53:29.475153Z","shell.execute_reply":"2022-08-19T09:53:35.305647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sv = svm.SVC(kernel='rbf',C=9)\nsv.fit(X_train_PCA , y_train)","metadata":{"id":"BFLkSPlR9MSb","outputId":"d8d44962-d9a0-485e-9899-6e9e881dff33","execution":{"iopub.status.busy":"2022-08-19T09:53:35.311839Z","iopub.execute_input":"2022-08-19T09:53:35.312667Z","iopub.status.idle":"2022-08-19T09:54:09.416768Z","shell.execute_reply.started":"2022-08-19T09:53:35.312618Z","shell.execute_reply":"2022-08-19T09:54:09.415033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predsv = sv.predict(X_test_PCA)","metadata":{"id":"xribmytq9OND","execution":{"iopub.status.busy":"2022-08-19T09:54:09.418952Z","iopub.execute_input":"2022-08-19T09:54:09.419788Z","iopub.status.idle":"2022-08-19T09:54:24.863707Z","shell.execute_reply.started":"2022-08-19T09:54:09.419736Z","shell.execute_reply":"2022-08-19T09:54:24.862508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('accuracy is',accuracy_score(y_predsv,y_test))\nscoreclf= accuracy_score(y_predsv,y_test)","metadata":{"id":"HNWrWY0Z9PwX","outputId":"6acd0ea5-c0bf-4abb-eceb-4597c33dcae4","execution":{"iopub.status.busy":"2022-08-19T09:54:24.865126Z","iopub.execute_input":"2022-08-19T09:54:24.865510Z","iopub.status.idle":"2022-08-19T09:54:24.876262Z","shell.execute_reply.started":"2022-08-19T09:54:24.865477Z","shell.execute_reply":"2022-08-19T09:54:24.875223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### XGBOOST","metadata":{"id":"xwQhXKYg9Trg"}},{"cell_type":"code","source":"from xgboost import XGBClassifier\nmodel = XGBClassifier()\neval_set = [(X_test,y_test)]\nmodel.fit(X_train, y_train, early_stopping_rounds= 5, eval_set=eval_set, verbose=True)\ny_pred = model.predict(X_test)\npredictions = [round(value) for value in y_pred]","metadata":{"id":"Xj8xEyrQ9R0t","outputId":"f11036af-0838-468a-e1ca-dca6d2b9ae9f","execution":{"iopub.status.busy":"2022-08-19T09:54:24.877778Z","iopub.execute_input":"2022-08-19T09:54:24.879044Z","iopub.status.idle":"2022-08-19T10:05:30.268263Z","shell.execute_reply.started":"2022-08-19T09:54:24.879000Z","shell.execute_reply":"2022-08-19T10:05:30.266952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy = accuracy_score(y_test, predictions)\nprint(\"Accuracy XGBOOST: %.2f%%\" % (accuracy * 100.0))","metadata":{"id":"EpInMkX39XfW","outputId":"bb7afc0f-b1f9-4a1c-f057-81a74793365b","execution":{"iopub.status.busy":"2022-08-19T10:05:30.270186Z","iopub.execute_input":"2022-08-19T10:05:30.270949Z","iopub.status.idle":"2022-08-19T10:05:30.285379Z","shell.execute_reply.started":"2022-08-19T10:05:30.270902Z","shell.execute_reply":"2022-08-19T10:05:30.284323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### AdaBoost","metadata":{"id":"af3TZ_wa9cUK"}},{"cell_type":"code","source":"from sklearn.ensemble import AdaBoostClassifier\nModel=AdaBoostClassifier()\nModel.fit(X_train, y_train)\ny_predAda=Model.predict(X_test)\nprint(classification_report(y_test,y_predAda))\nprint(confusion_matrix(y_pred,y_test))\nprint('accuracy is ',accuracy_score(y_predAda,y_test))\nAdaB = accuracy_score(y_predAda,y_test)","metadata":{"id":"uheV45sT9aQ-","outputId":"0f65fb73-6612-4917-ad93-6eb685668f42","execution":{"iopub.status.busy":"2022-08-19T10:05:30.287073Z","iopub.execute_input":"2022-08-19T10:05:30.287654Z","iopub.status.idle":"2022-08-19T10:06:12.157445Z","shell.execute_reply.started":"2022-08-19T10:05:30.287619Z","shell.execute_reply":"2022-08-19T10:06:12.156214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SCORES","metadata":{"id":"ju4Fw30D9jBm"}},{"cell_type":"code","source":"models = pd.DataFrame({\n    'Model': ['LogisticRegression','Decision Tree', 'PCA', 'XGBOOST', \"AdaBoost classifier\"\n              ],\n    'Score': [score,scoreDT,scoreclf,accuracy,AdaB]})\nmodels.sort_values(by='Score', ascending=False)","metadata":{"id":"11FZCzVA9gqu","outputId":"7093670b-b766-42a3-b506-05f18c6d92ff","execution":{"iopub.status.busy":"2022-08-19T10:06:12.159227Z","iopub.execute_input":"2022-08-19T10:06:12.159613Z","iopub.status.idle":"2022-08-19T10:06:12.179933Z","shell.execute_reply.started":"2022-08-19T10:06:12.159573Z","shell.execute_reply":"2022-08-19T10:06:12.178911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(figsize =(10, 5))\nsns.barplot(x='Score', y = 'Model', data = models, palette=\"Set3\")\nplt.title('Machine Learning Algorithm Accuracy Score \\n')\nplt.xlabel('Accuracy Score (%)')\nplt.ylabel('Algorithm')","metadata":{"id":"YZ9yDMw29mX1","outputId":"113b127f-9c99-40b6-8f05-913f34663b26","execution":{"iopub.status.busy":"2022-08-19T10:06:12.181633Z","iopub.execute_input":"2022-08-19T10:06:12.182048Z","iopub.status.idle":"2022-08-19T10:06:12.394527Z","shell.execute_reply.started":"2022-08-19T10:06:12.182014Z","shell.execute_reply":"2022-08-19T10:06:12.393283Z"},"trusted":true},"execution_count":null,"outputs":[]}]}