{"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)\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-28T16:08:40.183307Z","iopub.execute_input":"2023-10-28T16:08:40.183629Z","iopub.status.idle":"2023-10-28T16:08:40.191814Z","shell.execute_reply.started":"2023-10-28T16:08:40.183606Z","shell.execute_reply":"2023-10-28T16:08:40.191035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" Создаем переменные пути и считываем тренировочную и тестовую выборку","metadata":{}},{"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.status.busy":"2023-10-28T16:08:43.321466Z","iopub.execute_input":"2023-10-28T16:08:43.322436Z","iopub.status.idle":"2023-10-28T16:08:45.889551Z","shell.execute_reply.started":"2023-10-28T16:08:43.322394Z","shell.execute_reply":"2023-10-28T16:08:45.888635Z"},"trusted":true},"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.status.busy":"2023-10-28T16:14:06.919132Z","iopub.execute_input":"2023-10-28T16:14:06.919448Z","iopub.status.idle":"2023-10-28T16:14:06.958898Z","shell.execute_reply.started":"2023-10-28T16:14:06.919426Z","shell.execute_reply":"2023-10-28T16:14:06.957947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-28T16:08:54.061552Z","iopub.execute_input":"2023-10-28T16:08:54.061859Z","iopub.status.idle":"2023-10-28T16:08:54.075899Z","shell.execute_reply.started":"2023-10-28T16:08:54.061837Z","shell.execute_reply":"2023-10-28T16:08:54.075118Z"},"trusted":true},"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.status.busy":"2023-10-28T16:08:57.343645Z","iopub.execute_input":"2023-10-28T16:08:57.343952Z","iopub.status.idle":"2023-10-28T16:08:57.823261Z","shell.execute_reply.started":"2023-10-28T16:08:57.343930Z","shell.execute_reply":"2023-10-28T16:08:57.822313Z"},"trusted":true},"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.status.busy":"2023-10-28T11:14:33.321560Z","iopub.execute_input":"2023-10-28T11:14:33.321978Z","iopub.status.idle":"2023-10-28T11:14:42.584305Z","shell.execute_reply.started":"2023-10-28T11:14:33.321950Z","shell.execute_reply":"2023-10-28T11:14:42.583154Z"},"trusted":true},"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.status.busy":"2023-10-28T16:09:01.644372Z","iopub.execute_input":"2023-10-28T16:09:01.644658Z","iopub.status.idle":"2023-10-28T16:09:46.912980Z","shell.execute_reply.started":"2023-10-28T16:09:01.644637Z","shell.execute_reply":"2023-10-28T16:09:46.912013Z"},"trusted":true},"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.status.busy":"2023-10-28T16:10:52.781922Z","iopub.execute_input":"2023-10-28T16:10:52.782225Z","iopub.status.idle":"2023-10-28T16:10:53.304042Z","shell.execute_reply.started":"2023-10-28T16:10:52.782202Z","shell.execute_reply":"2023-10-28T16:10:53.303006Z"},"trusted":true},"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.status.busy":"2023-10-17T16:57:02.935627Z","iopub.execute_input":"2023-10-17T16:57:02.936197Z","iopub.status.idle":"2023-10-17T16:57:03.089968Z","shell.execute_reply.started":"2023-10-17T16:57:02.936167Z","shell.execute_reply":"2023-10-17T16:57:03.089083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T16:57:03.091131Z","iopub.execute_input":"2023-10-17T16:57:03.091653Z","iopub.status.idle":"2023-10-17T16:57:47.225610Z","shell.execute_reply.started":"2023-10-17T16:57:03.091620Z","shell.execute_reply":"2023-10-17T16:57:47.224685Z"},"trusted":true},"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.status.busy":"2023-10-17T16:57:58.428340Z","iopub.execute_input":"2023-10-17T16:57:58.428659Z","iopub.status.idle":"2023-10-17T16:57:58.460457Z","shell.execute_reply.started":"2023-10-17T16:57:58.428635Z","shell.execute_reply":"2023-10-17T16:57:58.459905Z"},"trusted":true},"execution_count":null,"outputs":[]}]}