{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":446.709772,"end_time":"2023-10-28T16:22:12.484138","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-10-28T16:14:45.774366","version":"2.4.0"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nfrom sklearn.neighbors import KNeighborsClassifier\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom scipy.stats import randint\nfrom scipy.ndimage import shift, rotate, zoom\nfrom sklearn.decomposition import PCA\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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2023-10-28T16:14:48.293511Z","iopub.status.busy":"2023-10-28T16:14:48.292907Z","iopub.status.idle":"2023-10-28T16:14:50.088368Z","shell.execute_reply":"2023-10-28T16:14:50.087490Z"},"papermill":{"duration":1.8024,"end_time":"2023-10-28T16:14:50.090258","exception":false,"start_time":"2023-10-28T16:14:48.287858","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = f\"{dirname}/{filenames[1]}\"\ntest_path = f\"{dirname}/{filenames[2]}\"\n\ntrain_data = pd.read_csv(train_path)\ntest_data = pd.read_csv(test_path)","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:50.105915Z","iopub.status.busy":"2023-10-28T16:14:50.105156Z","iopub.status.idle":"2023-10-28T16:14:54.138811Z","shell.execute_reply":"2023-10-28T16:14:54.138144Z"},"papermill":{"duration":4.039853,"end_time":"2023-10-28T16:14:54.140809","exception":false,"start_time":"2023-10-28T16:14:50.100956","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_data.drop('label', axis=1).values\ny_train = train_data.label.values\nX_test = test_data.values\nprint(X_train.shape)\nprint(y_train.shape)\nprint(test_data.shape)","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:54.148266Z","iopub.status.busy":"2023-10-28T16:14:54.147967Z","iopub.status.idle":"2023-10-28T16:14:54.207041Z","shell.execute_reply":"2023-10-28T16:14:54.205988Z"},"papermill":{"duration":0.064992,"end_time":"2023-10-28T16:14:54.209103","exception":false,"start_time":"2023-10-28T16:14:54.144111","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:54.216330Z","iopub.status.busy":"2023-10-28T16:14:54.216047Z","iopub.status.idle":"2023-10-28T16:14:54.232728Z","shell.execute_reply":"2023-10-28T16:14:54.231949Z"},"papermill":{"duration":0.022027,"end_time":"2023-10-28T16:14:54.234370","exception":false,"start_time":"2023-10-28T16:14:54.212343","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(1, 10, figsize=(12, 8))\nfor i in range(10):\n    digit = X_train[i].reshape(28, 28)\n    ax[i].imshow(digit, cmap=plt.cm.binary, interpolation='bilinear')\n    ax[i].set_axis_off()\n    ax[i].set_title(y_train[i])","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:54.241933Z","iopub.status.busy":"2023-10-28T16:14:54.241554Z","iopub.status.idle":"2023-10-28T16:14:54.738761Z","shell.execute_reply":"2023-10-28T16:14:54.737668Z"},"papermill":{"duration":0.503105,"end_time":"2023-10-28T16:14:54.740762","exception":false,"start_time":"2023-10-28T16:14:54.237657","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pca = PCA(n_components=50)\n# X_pca = pca.fit_transform(X_train)\n# X_pca_test = pca.fit_transform(test_data)\n# print('Shape before PCA: {}. Shape after PCA: {}'.format(X_train.shape, X_pca.shape))\n# print('Shape before PCA: {}. Shape after PCA: {}'.format(test_data.shape, X_pca_test.shape))","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:54.750138Z","iopub.status.busy":"2023-10-28T16:14:54.749443Z","iopub.status.idle":"2023-10-28T16:14:54.753499Z","shell.execute_reply":"2023-10-28T16:14:54.752578Z"},"papermill":{"duration":0.010683,"end_time":"2023-10-28T16:14:54.755377","exception":false,"start_time":"2023-10-28T16:14:54.744694","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def shift_in_one_direction(image, direction):\n    \"\"\"\n    Shifts an image by one pixel in the specified direction\n    \"\"\"\n    if direction == \"DOWN\":\n        image = shift(image, [1, 0])\n    elif direction == \"UP\":\n        image = shift(image, [-1, 0])\n    elif direction == \"LEFT\":\n        image = shift(image, [0, -1])\n    else:\n        image = shift(image, [0, 1])\n\n    return image\n\n\ndef shift_in_all_directions(image):\n    \"\"\"\n    Shifts an image in all the directions by one pixel\n    \"\"\"\n    reshaped_image = image.reshape(28, 28)\n\n    down_shifted_image = shift_in_one_direction(reshaped_image, \"DOWN\")\n    up_shifted_image = shift_in_one_direction(reshaped_image, \"UP\")\n    left_shifted_image = shift_in_one_direction(reshaped_image, \"LEFT\")\n    right_shifted_image = shift_in_one_direction(reshaped_image, \"RIGHT\")\n\n    return (down_shifted_image, up_shifted_image,\n            left_shifted_image, right_shifted_image)\n\n\ndef rotate_in_all_directions(image, angle):\n    \"\"\"\n    Rotates an image clockwise and anti-clockwise\n    \"\"\"\n    reshaped_image = image.reshape(28, 28)\n    \n    rotated_images = (rotate(reshaped_image, angle, reshape=False),\n                      rotate(reshaped_image, -angle, reshape=False))\n    \n    return rotated_images\n\n\ndef clipped_zoom(image, zoom_ranges):\n    \"\"\"\n    Clips and zooms an image at the specified zooming ranges\n    \"\"\"\n    reshaped_image = image.reshape(28, 28)\n    \n    h, w = reshaped_image.shape\n    \n    zoomed_images = []\n    for zoom_range in zoom_ranges:\n        zh = int(np.round(h / zoom_range))\n        zw = int(np.round(w / zoom_range))\n        top = (h - zh) // 2\n        left = (w - zw) // 2\n        \n        zoomed_images.append(zoom(reshaped_image[top:top+zh, left:left+zw],\n                                  zoom_range))\n    \n    return zoomed_images\n\ndef alter_image(image):\n    \"\"\"\n    Alters an image by shifting, rotating, and zooming it\n    \"\"\"\n    shifted_images = shift_in_all_directions(image)\n    rotated_images = rotate_in_all_directions(image, 10)\n    zoomed_images = clipped_zoom(image, [1.1, 1.2])\n            \n    return np.r_[shifted_images, rotated_images, zoomed_images]\n\nX_train_add = np.apply_along_axis(alter_image, 1, X_train).reshape(-1, 784)\ny_train_add = np.repeat(y_train, 8)\n\nprint(f\"X_train_add shape: {X_train_add.shape}\")\nprint(f\"y_train_add shape: {y_train_add.shape}\")","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:14:54.764212Z","iopub.status.busy":"2023-10-28T16:14:54.763953Z","iopub.status.idle":"2023-10-28T16:15:40.153348Z","shell.execute_reply":"2023-10-28T16:15:40.152371Z"},"papermill":{"duration":45.398355,"end_time":"2023-10-28T16:15:40.157785","exception":false,"start_time":"2023-10-28T16:14:54.759430","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_combined = np.r_[X_train, X_train_add]\ny_train_combined = np.r_[y_train, y_train_add]\n\ndel X_train\ndel X_train_add\ndel y_train\ndel y_train_add\n\nprint(f\"X_train_combined shape: {X_train_combined.shape}\")\nprint(f\"y_train_combined shape: {y_train_combined.shape}\")","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:15:40.166782Z","iopub.status.busy":"2023-10-28T16:15:40.166481Z","iopub.status.idle":"2023-10-28T16:15:40.673679Z","shell.execute_reply":"2023-10-28T16:15:40.672266Z"},"papermill":{"duration":0.51422,"end_time":"2023-10-28T16:15:40.675935","exception":false,"start_time":"2023-10-28T16:15:40.161715","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors=4, weights='distance', n_jobs=-1)\nknn.fit(X_train_combined, y_train_combined)","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:15:40.685024Z","iopub.status.busy":"2023-10-28T16:15:40.684663Z","iopub.status.idle":"2023-10-28T16:15:40.713637Z","shell.execute_reply":"2023-10-28T16:15:40.712765Z"},"papermill":{"duration":0.035578,"end_time":"2023-10-28T16:15:40.715486","exception":false,"start_time":"2023-10-28T16:15:40.679908","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:15:40.724421Z","iopub.status.busy":"2023-10-28T16:15:40.724069Z","iopub.status.idle":"2023-10-28T16:22:11.915255Z","shell.execute_reply":"2023-10-28T16:22:11.914599Z"},"papermill":{"duration":391.197928,"end_time":"2023-10-28T16:22:11.917312","exception":false,"start_time":"2023-10-28T16:15:40.719384","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop = len(test_data)+1\ndf = pd.DataFrame(dict(ImageId=range(1, stop), Label=y_pred))\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.execute_input":"2023-10-28T16:22:11.927016Z","iopub.status.busy":"2023-10-28T16:22:11.926304Z","iopub.status.idle":"2023-10-28T16:22:11.957785Z","shell.execute_reply":"2023-10-28T16:22:11.956971Z"},"papermill":{"duration":0.038524,"end_time":"2023-10-28T16:22:11.960062","exception":false,"start_time":"2023-10-28T16:22:11.921538","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}