{"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":"# common imports\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# PCA\nfrom sklearn.decomposition import PCA","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-04T13:56:00.880264Z","iopub.execute_input":"2022-11-04T13:56:00.880681Z","iopub.status.idle":"2022-11-04T13:56:00.972168Z","shell.execute_reply.started":"2022-11-04T13:56:00.880649Z","shell.execute_reply":"2022-11-04T13:56:00.970805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:00.976348Z","iopub.execute_input":"2022-11-04T13:56:00.976798Z","iopub.status.idle":"2022-11-04T13:56:04.824029Z","shell.execute_reply.started":"2022-11-04T13:56:00.976760Z","shell.execute_reply":"2022-11-04T13:56:04.822905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df.drop(columns=[\"label\"])\nlabels = df[\"label\"]\n\nprint(\"Training set shape: \", train.shape)\nprint(\"Target variable shape:\", labels.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:04.825586Z","iopub.execute_input":"2022-11-04T13:56:04.826090Z","iopub.status.idle":"2022-11-04T13:56:04.955033Z","shell.execute_reply.started":"2022-11-04T13:56:04.826043Z","shell.execute_reply":"2022-11-04T13:56:04.953647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function to plot a number of digits\ndef plot_digits(data, images_per_row=5):\n    for i in range(len(data)):\n        ax = plt.subplot(images_per_row, images_per_row, i+1)\n        \n        digit = data[i]\n        digit_img = digit.reshape(28, 28)\n        \n        plt.imshow(digit_img, cmap=plt.cm.Blues)\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:04.958543Z","iopub.execute_input":"2022-11-04T13:56:04.959090Z","iopub.status.idle":"2022-11-04T13:56:04.979167Z","shell.execute_reply.started":"2022-11-04T13:56:04.959039Z","shell.execute_reply":"2022-11-04T13:56:04.977958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_digits(train[:25].to_numpy(), images_per_row=5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:04.980933Z","iopub.execute_input":"2022-11-04T13:56:04.981620Z","iopub.status.idle":"2022-11-04T13:56:05.875641Z","shell.execute_reply.started":"2022-11-04T13:56:04.981584Z","shell.execute_reply":"2022-11-04T13:56:05.874173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preserve 95% of variance\npca = PCA(n_components=0.95)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:05.877922Z","iopub.execute_input":"2022-11-04T13:56:05.878910Z","iopub.status.idle":"2022-11-04T13:56:05.884675Z","shell.execute_reply.started":"2022-11-04T13:56:05.878793Z","shell.execute_reply":"2022-11-04T13:56:05.883395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# easy!\ntrain_reduced = pca.fit_transform(train)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:05.886613Z","iopub.execute_input":"2022-11-04T13:56:05.887421Z","iopub.status.idle":"2022-11-04T13:56:11.545435Z","shell.execute_reply.started":"2022-11-04T13:56:05.887368Z","shell.execute_reply":"2022-11-04T13:56:11.544379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"New shape of the training set: \", train_reduced.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:11.548288Z","iopub.execute_input":"2022-11-04T13:56:11.549622Z","iopub.status.idle":"2022-11-04T13:56:11.559196Z","shell.execute_reply.started":"2022-11-04T13:56:11.549564Z","shell.execute_reply":"2022-11-04T13:56:11.557715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Dimensionality reduced to: \", pca.components_.T.shape[1], \"dimensions\")","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:11.561636Z","iopub.execute_input":"2022-11-04T13:56:11.563084Z","iopub.status.idle":"2022-11-04T13:56:11.579385Z","shell.execute_reply.started":"2022-11-04T13:56:11.563030Z","shell.execute_reply":"2022-11-04T13:56:11.577848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = 0\nfor i, evr in enumerate(pca.explained_variance_ratio_):\n    print(f\"Ratio of variance preserved by PC{i+1}: \", evr)\n    s += evr\n    \nprint(\"\\nTotal variance preserved: \", s)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:11.583756Z","iopub.execute_input":"2022-11-04T13:56:11.585288Z","iopub.status.idle":"2022-11-04T13:56:11.600335Z","shell.execute_reply.started":"2022-11-04T13:56:11.585223Z","shell.execute_reply":"2022-11-04T13:56:11.598769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_recovered = pca.inverse_transform(train_reduced)","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:11.602037Z","iopub.execute_input":"2022-11-04T13:56:11.602532Z","iopub.status.idle":"2022-11-04T13:56:12.006882Z","shell.execute_reply.started":"2022-11-04T13:56:11.602481Z","shell.execute_reply":"2022-11-04T13:56:12.005979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_digits(train_recovered[:25], images_per_row=5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-04T13:56:12.008153Z","iopub.execute_input":"2022-11-04T13:56:12.008713Z","iopub.status.idle":"2022-11-04T13:56:13.023231Z","shell.execute_reply.started":"2022-11-04T13:56:12.008677Z","shell.execute_reply":"2022-11-04T13:56:13.022175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}