{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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":31259,"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-06T01:57:14.422616Z","iopub.execute_input":"2026-02-06T01:57:14.422841Z","iopub.status.idle":"2026-02-06T01:57:15.978313Z","shell.execute_reply.started":"2026-02-06T01:57:14.422818Z","shell.execute_reply":"2026-02-06T01:57:15.977198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsample_submission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T01:57:18.881416Z","iopub.execute_input":"2026-02-06T01:57:18.882178Z","iopub.status.idle":"2026-02-06T01:57:23.796055Z","shell.execute_reply.started":"2026-02-06T01:57:18.882083Z","shell.execute_reply":"2026-02-06T01:57:23.795231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from random import randrange\n\ndef get_random_images_by_label(label: int, number_of_images: int = 10):\n    '''\n    This pulls back a specified number of images in the specified class from the label variable\n    '''\n    all_with_label = train_df[train_df[\"label\"] == label]\n    random_images_indexes = [randrange(len(all_with_label)) for i in range(number_of_images)]\n    return all_with_label.iloc[random_images_indexes, 1:] # 1: removes the label from the trainign data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T04:29:41.321237Z","iopub.execute_input":"2026-02-06T04:29:41.321512Z","iopub.status.idle":"2026-02-06T04:29:41.327311Z","shell.execute_reply.started":"2026-02-06T04:29:41.321489Z","shell.execute_reply":"2026-02-06T04:29:41.325929Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def sum_square_error_1_class(test_index: int, class_label: int):\n    '''\n    This shows the smallest sum squared difference between an image in the training data and the\n    '''\n    return min((get_random_images_by_label(class_label, 80).sub(test_df.iloc[test_index], axis=1) ** 2).sum(axis=1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T04:29:50.629417Z","iopub.execute_input":"2026-02-06T04:29:50.629954Z","iopub.status.idle":"2026-02-06T04:29:50.634768Z","shell.execute_reply.started":"2026-02-06T04:29:50.629925Z","shell.execute_reply":"2026-02-06T04:29:50.633559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def smallest_error_class(test_index: int):\n    '''\n    Iterates across all labels ie 0-9 and returns which class had the most similar image to the test image\n    '''\n    sums_by_class = [sum_square_error_1_class(test_index, i) for i in range(10)]\n    min_by_class = min(sums_by_class)\n    return sums_by_class.index(min_by_class)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-06T04:29:51.157677Z","iopub.execute_input":"2026-02-06T04:29:51.158072Z","iopub.status.idle":"2026-02-06T04:29:51.162845Z","shell.execute_reply.started":"2026-02-06T04:29:51.158044Z","shell.execute_reply":"2026-02-06T04:29:51.161882Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for _, row in sample_submission_df.iterrows():\n    row[\"Label\"] = smallest_error_class(row[\"ImageId\"]-1)\nsample_submission_df.to_csv(\"simplemnist_result.csv\",index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-04T20:58:43.438429Z","iopub.execute_input":"2026-02-04T20:58:43.438891Z","iopub.status.idle":"2026-02-04T23:04:09.411820Z","shell.execute_reply.started":"2026-02-04T20:58:43.438861Z","shell.execute_reply":"2026-02-04T23:04:09.410699Z"}},"outputs":[],"execution_count":null}]}