{"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":"**The aim of this notebook is to achieve high accuracy on mnist with least number of features. We have used HOG algorithm to extract features with 37 features used in the classification process **","metadata":{}},{"cell_type":"code","source":"\n\nimport numpy as np \nimport pandas as pd \n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:05.730208Z","iopub.execute_input":"2022-03-26T15:29:05.730832Z","iopub.status.idle":"2022-03-26T15:29:05.758649Z","shell.execute_reply.started":"2022-03-26T15:29:05.730699Z","shell.execute_reply":"2022-03-26T15:29:05.758002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\ndf_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:13.399988Z","iopub.execute_input":"2022-03-26T15:29:13.400711Z","iopub.status.idle":"2022-03-26T15:29:20.264642Z","shell.execute_reply.started":"2022-03-26T15:29:13.400666Z","shell.execute_reply":"2022-03-26T15:29:20.263990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = df_train.drop(columns='label').values\ntest_imgs = df_test.values\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:34.450064Z","iopub.execute_input":"2022-03-26T15:29:34.450802Z","iopub.status.idle":"2022-03-26T15:29:34.539155Z","shell.execute_reply.started":"2022-03-26T15:29:34.450755Z","shell.execute_reply":"2022-03-26T15:29:34.538203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Store train labels in a list**","metadata":{}},{"cell_type":"code","source":"train_labels = df_train['label']\n\ntrain_labels_list = []\nfor lb in train_labels:\n    train_labels_list.append(lb)\n    \nprint(len(train_labels_list))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:44.921561Z","iopub.execute_input":"2022-03-26T15:29:44.921896Z","iopub.status.idle":"2022-03-26T15:29:44.941122Z","shell.execute_reply.started":"2022-03-26T15:29:44.921854Z","shell.execute_reply":"2022-03-26T15:29:44.940194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Store train images in a list**","metadata":{}},{"cell_type":"code","source":"train_images_list = [np.asarray(image, dtype=np.uint8).reshape([28, 28]) for image in train_imgs]\nprint(len(train_images_list))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:50.630239Z","iopub.execute_input":"2022-03-26T15:29:50.631102Z","iopub.status.idle":"2022-03-26T15:29:50.841364Z","shell.execute_reply.started":"2022-03-26T15:29:50.631056Z","shell.execute_reply":"2022-03-26T15:29:50.840430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Show dataset details**","metadata":{}},{"cell_type":"code","source":"\nprint('type(images): ', type(train_images_list))\nprint('len(images): ', len(train_images_list))\nprint()\n\nprint('type(labels): ', type(train_labels_list))\nprint('len(labels): ', len(train_labels_list))\nprint()\n\n# Show samples of the dataset\n\nfig = plt.figure(figsize=(15, 4))\n\nfor i in range(4):\n\n    print('----------------------------------------')\n    print()\n\n    print('type(images[', i, ']):', type(train_images_list[i]))\n    print('images[', i, '].dtype:', train_images_list[i].dtype)\n    print('images[', i, '].min():', train_images_list[i].min())\n    print('images[', i, '].max():', train_images_list[i].max())\n    print('images[', i, '].shape = (HEIGHT, WIDTH):', train_images_list[i].shape)\n    print()\n\n    print('type(labels[', i, ']):', type(train_labels_list[i]))\n    print('labels[', i, ']:', train_labels_list[i])\n    print()\n\n    fig.add_subplot(1, 4, i + 1)\n    plt.title('labels[' + str(i) + '] = ' + str(train_labels_list[i]))\n    plt.imshow(train_images_list[i], cmap='gray')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:29:56.020179Z","iopub.execute_input":"2022-03-26T15:29:56.020598Z","iopub.status.idle":"2022-03-26T15:29:56.570620Z","shell.execute_reply.started":"2022-03-26T15:29:56.020539Z","shell.execute_reply":"2022-03-26T15:29:56.569712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Store test images in a list**","metadata":{}},{"cell_type":"code","source":"test_images_list = [np.asarray(image, dtype=np.uint8).reshape([28, 28]) for image in test_imgs]\nprint(len(test_images_list))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:02.840529Z","iopub.execute_input":"2022-03-26T15:30:02.840830Z","iopub.status.idle":"2022-03-26T15:30:02.987864Z","shell.execute_reply.started":"2022-03-26T15:30:02.840797Z","shell.execute_reply":"2022-03-26T15:30:02.987029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Resize**","metadata":{}},{"cell_type":"code","source":"from skimage.transform import resize\nimport cv2\n\ndef resizeImage(image):\n\n    image = cv2.resize(image, (64,64))\n    return image\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:10.299756Z","iopub.execute_input":"2022-03-26T15:30:10.300062Z","iopub.status.idle":"2022-03-26T15:30:10.900625Z","shell.execute_reply.started":"2022-03-26T15:30:10.300028Z","shell.execute_reply":"2022-03-26T15:30:10.899720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**HOG features**","metadata":{}},{"cell_type":"code","source":"from skimage.feature import hog\n\ndef fd_hog(image):\n  \n    fv_hog = hog(image, orientations=3, pixels_per_cell=(16, 16),cells_per_block=(4,4),block_norm= 'L1-sqrt' ,transform_sqrt = False)\n\n    return fv_hog","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:13.629869Z","iopub.execute_input":"2022-03-26T15:30:13.630185Z","iopub.status.idle":"2022-03-26T15:30:13.814091Z","shell.execute_reply.started":"2022-03-26T15:30:13.630152Z","shell.execute_reply":"2022-03-26T15:30:13.813261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extract HOG faetures**","metadata":{}},{"cell_type":"code","source":"feat_train = np.array([fd_hog(resizeImage(im)) for im in train_images_list])\nfeat_test = np.array([fd_hog(resizeImage(im)) for im in test_images_list])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:19.511340Z","iopub.execute_input":"2022-03-26T15:30:19.511846Z","iopub.status.idle":"2022-03-26T15:30:42.516617Z","shell.execute_reply.started":"2022-03-26T15:30:19.511796Z","shell.execute_reply":"2022-03-26T15:30:42.515918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"[INFO] The number of training features ...\" + str(feat_train.shape[1]))\nprint(\"[INFO] The number of testing features ...\" + str(feat_test.shape[1]))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:44.930159Z","iopub.execute_input":"2022-03-26T15:30:44.930669Z","iopub.status.idle":"2022-03-26T15:30:44.936618Z","shell.execute_reply.started":"2022-03-26T15:30:44.930596Z","shell.execute_reply":"2022-03-26T15:30:44.935747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform a training and testing split, using \n\n\n################################################################################\n################################################################################\n\n(trainX, testX, trainY, testY) = train_test_split(feat_train, train_labels_list,test_size=0.3,random_state=0)\nprint(\"[INFO] The number of features ...\" + str(feat_train.shape[1]))\nprint(\"[INFO] The number of training samples ...\" + str(trainX.shape[0]))\nprint(\"[INFO] The number of training samples ...\" + str(testX.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:30:52.840696Z","iopub.execute_input":"2022-03-26T15:30:52.841347Z","iopub.status.idle":"2022-03-26T15:30:52.866976Z","shell.execute_reply.started":"2022-03-26T15:30:52.841296Z","shell.execute_reply":"2022-03-26T15:30:52.866280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Classification with SVM**","metadata":{}},{"cell_type":"code","source":"\n\n################################################################################\n################################################################################\n\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import accuracy_score\n\nfrom sklearn.svm import SVC\nfrom sklearn.pipeline import Pipeline\n\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.decomposition import PCA\n\nimport time\n\n# Use Principal Component Analysis to reduce dimensionality\n# and improve generalization\npca = PCA(n_components =37)\n# Use a RBF SVC\nsvm = SVC(C= 11.64390150447387,kernel='rbf',gamma= 5.264307845370188)\n# Combine PCA and SVC to a pipeline\npipe = Pipeline(steps=[('pca', pca), ('svm', svm)])\n\n\nstart_time = time.time()\nmodel = pipe.fit(trainX, trainY)\nprint(\"--- %s seconds ---\" % (time.time() - start_time))\n\n# make predictions on our data and show a classification report 5.403\nprint(\"[INFO] evaluating...\")\npredictions = model.predict(testX)\n#print(testY,predictions3,le.classes_)\n\n\nreport = classification_report(testY, predictions,digits=4)\n\nprint(report)\n\nacc = accuracy_score(testY, predictions)\nprint(\"Accuracy: %.4f%%\" % (acc * 100.0))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:31:11.230950Z","iopub.execute_input":"2022-03-26T15:31:11.231834Z","iopub.status.idle":"2022-03-26T15:31:26.829301Z","shell.execute_reply.started":"2022-03-26T15:31:11.231788Z","shell.execute_reply":"2022-03-26T15:31:26.828251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install autokeras\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:31:45.720775Z","iopub.execute_input":"2022-03-26T15:31:45.721612Z","iopub.status.idle":"2022-03-26T15:33:15.721493Z","shell.execute_reply.started":"2022-03-26T15:31:45.721567Z","shell.execute_reply":"2022-03-26T15:33:15.720672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\nimport autokeras as ak\n\n# It tries 10 different models.\nclf = ak.StructuredDataClassifier(overwrite=True, max_trials=10)\n# Feed the structured data classifier with training data.\nclf.fit(trainX, np.array(trainY))\n\nprint(clf.evaluate(testX,  np.array(testY)))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:38:14.330385Z","iopub.execute_input":"2022-03-26T15:38:14.331350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(np.array(trainY))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T15:37:11.390069Z","iopub.execute_input":"2022-03-26T15:37:11.390525Z","iopub.status.idle":"2022-03-26T15:37:11.399481Z","shell.execute_reply.started":"2022-03-26T15:37:11.390477Z","shell.execute_reply":"2022-03-26T15:37:11.398818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"y_pred =  model.predict(feat_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-24T13:06:00.385702Z","iopub.execute_input":"2022-03-24T13:06:00.385996Z","iopub.status.idle":"2022-03-24T13:06:12.163071Z","shell.execute_reply.started":"2022-03-24T13:06:00.385965Z","shell.execute_reply":"2022-03-24T13:06:12.16245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.DataFrame(y_pred, columns=['Label'])\ndf_submission.insert(0, 'ImageId', range(1, 1 + len(y_pred)))\ndf_submission.to_csv('submission.csv', index=False)\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-24T13:06:54.389836Z","iopub.execute_input":"2022-03-24T13:06:54.390474Z","iopub.status.idle":"2022-03-24T13:06:54.436143Z","shell.execute_reply.started":"2022-03-24T13:06:54.390419Z","shell.execute_reply":"2022-03-24T13:06:54.435248Z"},"trusted":true},"execution_count":null,"outputs":[]}]}