{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix , classification_report\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-11T13:54:45.774321Z","iopub.execute_input":"2024-03-11T13:54:45.774675Z","iopub.status.idle":"2024-03-11T13:54:45.779466Z","shell.execute_reply.started":"2024-03-11T13:54:45.774648Z","shell.execute_reply":"2024-03-11T13:54:45.778557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:06.712386Z","iopub.execute_input":"2024-03-11T13:37:06.712805Z","iopub.status.idle":"2024-03-11T13:37:10.447885Z","shell.execute_reply.started":"2024-03-11T13:37:06.712763Z","shell.execute_reply":"2024-03-11T13:37:10.446963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = ds['label'].copy()\nx = ds.copy()\nx.drop(columns = 'label' , inplace = True )\ny = y.values\nx = x.values","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:10.44924Z","iopub.execute_input":"2024-03-11T13:37:10.449966Z","iopub.status.idle":"2024-03-11T13:37:10.63359Z","shell.execute_reply.started":"2024-03-11T13:37:10.449938Z","shell.execute_reply":"2024-03-11T13:37:10.632623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = x.reshape(x.shape[0] , 28 , 28)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:10.636171Z","iopub.execute_input":"2024-03-11T13:37:10.636528Z","iopub.status.idle":"2024-03-11T13:37:10.641401Z","shell.execute_reply.started":"2024-03-11T13:37:10.636496Z","shell.execute_reply":"2024-03-11T13:37:10.64031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_centroid(image , x_block , y_block):\n    features = []\n    for i in range ( 0 , image.shape[0] , x_block):\n        for j in range ( 0 , image.shape[1] , y_block):\n            block_end_row = min(i + x_block, image.shape[0])\n            block_end_col = min(j + y_block, image.shape[1])\n            block = image[i:block_end_row, j:block_end_col]\n        \n            rows, cols = block.shape\n            row_coords, col_coords = np.mgrid[0:rows, 0:cols]\n        \n            total_pixels = np.sum(block)\n            if total_pixels ==0:\n                total_pixels =1\n            centroid_row = np.sum(row_coords * block) / total_pixels\n            centroid_col = np.sum(col_coords * block) / total_pixels\n            features.extend([centroid_row, centroid_col])\n    \n    \n    return features","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:10.642448Z","iopub.execute_input":"2024-03-11T13:37:10.642721Z","iopub.status.idle":"2024-03-11T13:37:10.651572Z","shell.execute_reply.started":"2024-03-11T13:37:10.642698Z","shell.execute_reply":"2024-03-11T13:37:10.650681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"block_h = int(input())\nblock_w = int(input())","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:22.4654Z","iopub.execute_input":"2024-03-11T13:37:22.465718Z","iopub.status.idle":"2024-03-11T13:37:25.313184Z","shell.execute_reply.started":"2024-03-11T13:37:22.465695Z","shell.execute_reply":"2024-03-11T13:37:25.312457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\nfor image in x :\n    fetures = get_centroid(image , block_h , block_w)\n    images.append(fetures)\nimages = np.array(images)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:37:27.943457Z","iopub.execute_input":"2024-03-11T13:37:27.944349Z","iopub.status.idle":"2024-03-11T13:45:02.811346Z","shell.execute_reply.started":"2024-03-11T13:37:27.944309Z","shell.execute_reply":"2024-03-11T13:45:02.810303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images.shape","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:45:02.813311Z","iopub.execute_input":"2024-03-11T13:45:02.813944Z","iopub.status.idle":"2024-03-11T13:45:02.820757Z","shell.execute_reply.started":"2024-03-11T13:45:02.813908Z","shell.execute_reply":"2024-03-11T13:45:02.81983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(images, np.asarray(y), test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:56:58.622171Z","iopub.execute_input":"2024-03-11T13:56:58.623115Z","iopub.status.idle":"2024-03-11T13:56:58.674621Z","shell.execute_reply.started":"2024-03-11T13:56:58.623084Z","shell.execute_reply":"2024-03-11T13:56:58.673838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = KNeighborsClassifier(n_neighbors=15)\nclf.fit(X_train , y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:57:01.998283Z","iopub.execute_input":"2024-03-11T13:57:01.998636Z","iopub.status.idle":"2024-03-11T13:57:02.014654Z","shell.execute_reply.started":"2024-03-11T13:57:01.998607Z","shell.execute_reply":"2024-03-11T13:57:02.013828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:57:06.072035Z","iopub.execute_input":"2024-03-11T13:57:06.072368Z","iopub.status.idle":"2024-03-11T13:57:06.078672Z","shell.execute_reply.started":"2024-03-11T13:57:06.072343Z","shell.execute_reply":"2024-03-11T13:57:06.077625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test)\nprint(classification_report(y_pred , y_test))","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:59:15.758308Z","iopub.execute_input":"2024-03-11T13:59:15.759124Z","iopub.status.idle":"2024-03-11T13:59:20.264305Z","shell.execute_reply.started":"2024-03-11T13:59:15.759094Z","shell.execute_reply":"2024-03-11T13:59:20.263248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-03-11T13:57:50.983113Z","iopub.execute_input":"2024-03-11T13:57:50.983715Z","iopub.status.idle":"2024-03-11T13:57:50.989992Z","shell.execute_reply.started":"2024-03-11T13:57:50.983684Z","shell.execute_reply":"2024-03-11T13:57:50.988803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}