{"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":"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-20T12:53:55.193528Z","iopub.execute_input":"2021-09-20T12:53:55.194620Z","iopub.status.idle":"2021-09-20T12:53:55.293702Z","shell.execute_reply.started":"2021-09-20T12:53:55.194489Z","shell.execute_reply":"2021-09-20T12:53:55.292470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.manifold import TSNE\nfrom matplotlib import offsetbox\nimport seaborn as sns\n\nfrom sklearn.decomposition import PCA\nfrom skimage.feature import hog\n\nfrom tqdm import tqdm\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:53:55.296061Z","iopub.execute_input":"2021-09-20T12:53:55.296420Z","iopub.status.idle":"2021-09-20T12:54:02.253038Z","shell.execute_reply.started":"2021-09-20T12:53:55.296380Z","shell.execute_reply":"2021-09-20T12:54:02.251869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds=pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest_ds=pd.read_csv('../input/Kannada-MNIST/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:02.254608Z","iopub.execute_input":"2021-09-20T12:54:02.254935Z","iopub.status.idle":"2021-09-20T12:54:08.614933Z","shell.execute_reply.started":"2021-09-20T12:54:02.254895Z","shell.execute_reply":"2021-09-20T12:54:08.613825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train=train_ds['label']\nx_train=train_ds.drop(['label'],axis=1)\n\nid_test=test_ds['id']\nx_test=test_ds.drop(['id'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:08.617325Z","iopub.execute_input":"2021-09-20T12:54:08.618097Z","iopub.status.idle":"2021-09-20T12:54:08.794445Z","shell.execute_reply.started":"2021-09-20T12:54:08.618057Z","shell.execute_reply":"2021-09-20T12:54:08.793287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('x_train.npy',x_train)\nnp.save('y_train.npy',y_train)\n\nnp.save('id_test.npy',id_test)\nnp.save('x_test.npy',x_test)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:08.798032Z","iopub.execute_input":"2021-09-20T12:54:08.798406Z","iopub.status.idle":"2021-09-20T12:54:09.130768Z","shell.execute_reply.started":"2021-09-20T12:54:08.798342Z","shell.execute_reply":"2021-09-20T12:54:09.129577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=np.load('x_train.npy')\ny_train=np.load('y_train.npy')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:09.132738Z","iopub.execute_input":"2021-09-20T12:54:09.133087Z","iopub.status.idle":"2021-09-20T12:54:09.304042Z","shell.execute_reply.started":"2021-09-20T12:54:09.133049Z","shell.execute_reply":"2021-09-20T12:54:09.303059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds['label'].value_counts().plot(kind='barh',figsize=(7,7))","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:09.305849Z","iopub.execute_input":"2021-09-20T12:54:09.306176Z","iopub.status.idle":"2021-09-20T12:54:09.607529Z","shell.execute_reply.started":"2021-09-20T12:54:09.306141Z","shell.execute_reply":"2021-09-20T12:54:09.606525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(7,7))\nidx=[]\n\nfor i in range(25):\n    idx.append(np.random.randint(len(y_train)))\n    \nfor i in range(25):\n    label=y_train[idx[i]]\n    img=(x_train[idx[i]]).reshape(28,28)\n    \n    plt.subplot(5, 5, i+1)\n    plt.imshow(img,cmap='gray')\n    plt.title(label)\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:09.609164Z","iopub.execute_input":"2021-09-20T12:54:09.609512Z","iopub.status.idle":"2021-09-20T12:54:10.891163Z","shell.execute_reply.started":"2021-09-20T12:54:09.609485Z","shell.execute_reply":"2021-09-20T12:54:10.890127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scale and visualize the embedding vectors\ndef plot_embedding(X,y, title=None,size=(15,12)):\n    x_min, x_max = np.min(X, 0), np.max(X, 0)\n    X = (X - x_min) / (x_max - x_min)\n\n    plt.figure(figsize=size)\n    ax = plt.subplot(111)\n    for i in range(X.shape[0]):\n        plt.text(X[i, 0], X[i, 1], str(y[i]),\n                 color=plt.cm.Set1(y[i] / 10.),\n                 fontdict={'weight': 'bold', 'size': 9})\n\n    if hasattr(offsetbox, 'AnnotationBbox'):\n        # only print thumbnails with matplotlib > 1.0\n        shown_images = np.array([[1., 1.]])  # just something big\n        for i in range(X.shape[0]):\n            dist = np.sum((X[i] - shown_images) ** 2, 1)\n            if np.min(dist) < 4e-3:\n                # don't show points that are too close\n                continue\n            shown_images = np.r_[shown_images, [X[i]]]\n            imagebox = offsetbox.AnnotationBbox(\n                offsetbox.OffsetImage(x_train[i].reshape(28,28), cmap=plt.cm.gray_r),\n                X[i])\n            ax.add_artist(imagebox)\n    plt.xticks([]), plt.yticks([])\n    if title is not None:\n        plt.title(title)\n    plt.savefig('tsne.png')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:10.892342Z","iopub.execute_input":"2021-09-20T12:54:10.892960Z","iopub.status.idle":"2021-09-20T12:54:10.905828Z","shell.execute_reply.started":"2021-09-20T12:54:10.892923Z","shell.execute_reply":"2021-09-20T12:54:10.904433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(7,7))\nfor i in range(25):\n    label=y_train[idx[i]]\n    img=(x_train[idx[i]]).reshape(28,28)\n    fd, hog_image = hog(img, orientations=10, pixels_per_cell=(2,2),cells_per_block=(1,1), visualize=True)\n    plt.subplot(5, 5, i+1)\n    plt.imshow(hog_image,cmap='gray')\n    plt.title(label)\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:10.907501Z","iopub.execute_input":"2021-09-20T12:54:10.908464Z","iopub.status.idle":"2021-09-20T12:54:13.994107Z","shell.execute_reply.started":"2021-09-20T12:54:10.908368Z","shell.execute_reply":"2021-09-20T12:54:13.993138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(7,7))\nfor i in range(25):\n    label=y_train[idx[i]]\n    img=(x_train[idx[i]]).reshape(28,28)\n    fd, hog_image = hog(img, orientations=8, pixels_per_cell=(4,4),cells_per_block=(1,1), visualize=True)\n    plt.subplot(5, 5, i+1)\n    plt.imshow(hog_image,cmap='gray')\n    plt.title(label)\n    plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:13.995756Z","iopub.execute_input":"2021-09-20T12:54:13.996896Z","iopub.status.idle":"2021-09-20T12:54:16.002606Z","shell.execute_reply.started":"2021-09-20T12:54:13.996837Z","shell.execute_reply":"2021-09-20T12:54:16.001519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_hog=[]\n\nfor i in tqdm(range(len(x_train))):\n    img=x_train[i].reshape(28,28)\n    fd, hog_image = hog(img, orientations=8, pixels_per_cell=(4,4),cells_per_block=(1,1), visualize=True)\n    x_train_hog.append(fd)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T12:54:16.004374Z","iopub.execute_input":"2021-09-20T12:54:16.004773Z","iopub.status.idle":"2021-09-20T13:09:09.473043Z","shell.execute_reply.started":"2021-09-20T12:54:16.004732Z","shell.execute_reply":"2021-09-20T13:09:09.472189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=np.array(x_train_hog)\nY=np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:09:09.476465Z","iopub.execute_input":"2021-09-20T13:09:09.476685Z","iopub.status.idle":"2021-09-20T13:09:09.599999Z","shell.execute_reply.started":"2021-09-20T13:09:09.476659Z","shell.execute_reply":"2021-09-20T13:09:09.598713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_val,y_train,y_val=train_test_split(X,Y,test_size=0.2)\n\nprint(x_train.shape)\nprint(x_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:09:09.604574Z","iopub.execute_input":"2021-09-20T13:09:09.604984Z","iopub.status.idle":"2021-09-20T13:09:09.692315Z","shell.execute_reply.started":"2021-09-20T13:09:09.604949Z","shell.execute_reply":"2021-09-20T13:09:09.691251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf=SVC()\nclf.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:09:09.694175Z","iopub.execute_input":"2021-09-20T13:09:09.694779Z","iopub.status.idle":"2021-09-20T13:10:46.068785Z","shell.execute_reply.started":"2021-09-20T13:09:09.694735Z","shell.execute_reply":"2021-09-20T13:10:46.067882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:10:46.071285Z","iopub.execute_input":"2021-09-20T13:10:46.071647Z","iopub.status.idle":"2021-09-20T13:13:46.041979Z","shell.execute_reply.started":"2021-09-20T13:10:46.071607Z","shell.execute_reply":"2021-09-20T13:13:46.040966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.score(x_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:13:46.043721Z","iopub.execute_input":"2021-09-20T13:13:46.044164Z","iopub.status.idle":"2021-09-20T13:14:30.526665Z","shell.execute_reply.started":"2021-09-20T13:13:46.044115Z","shell.execute_reply":"2021-09-20T13:14:30.525699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=np.load('x_test.npy')\ntest_id=np.load('id_test.npy')","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:14:30.528374Z","iopub.execute_input":"2021-09-20T13:14:30.528728Z","iopub.status.idle":"2021-09-20T13:14:30.550029Z","shell.execute_reply.started":"2021-09-20T13:14:30.528688Z","shell.execute_reply":"2021-09-20T13:14:30.549075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test_hog=[]\n\nfor i in tqdm(range(len(x_test))):\n    img=x_test[i].reshape(28,28)\n    fd, hog_image = hog(img, orientations=8, pixels_per_cell=(4,4),cells_per_block=(1,1), visualize=True)\n    x_test_hog.append(fd)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:14:30.551768Z","iopub.execute_input":"2021-09-20T13:14:30.552171Z","iopub.status.idle":"2021-09-20T13:15:43.564779Z","shell.execute_reply.started":"2021-09-20T13:14:30.552132Z","shell.execute_reply":"2021-09-20T13:15:43.563820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_test=clf.predict(x_test_hog)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:15:43.568176Z","iopub.execute_input":"2021-09-20T13:15:43.568408Z","iopub.status.idle":"2021-09-20T13:16:01.661076Z","shell.execute_reply.started":"2021-09-20T13:15:43.568383Z","shell.execute_reply":"2021-09-20T13:16:01.660116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'id': test_id, 'label': y_pred_test})\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-20T13:16:01.662669Z","iopub.execute_input":"2021-09-20T13:16:01.662985Z","iopub.status.idle":"2021-09-20T13:16:01.684147Z","shell.execute_reply.started":"2021-09-20T13:16:01.662953Z","shell.execute_reply":"2021-09-20T13:16:01.683360Z"},"trusted":true},"execution_count":null,"outputs":[]}]}