{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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-16T03:27:38.055453Z","iopub.execute_input":"2023-10-16T03:27:38.056491Z","iopub.status.idle":"2023-10-16T03:27:38.838145Z","shell.execute_reply.started":"2023-10-16T03:27:38.056451Z","shell.execute_reply":"2023-10-16T03:27:38.837183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:27:44.139819Z","iopub.execute_input":"2023-10-16T03:27:44.140319Z","iopub.status.idle":"2023-10-16T03:27:46.027861Z","shell.execute_reply.started":"2023-10-16T03:27:44.140293Z","shell.execute_reply":"2023-10-16T03:27:46.027003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The shape of the data is:\",df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:27:50.484538Z","iopub.execute_input":"2023-10-16T03:27:50.484839Z","iopub.status.idle":"2023-10-16T03:27:50.488621Z","shell.execute_reply.started":"2023-10-16T03:27:50.484819Z","shell.execute_reply":"2023-10-16T03:27:50.487886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:27:54.969426Z","iopub.execute_input":"2023-10-16T03:27:54.969691Z","iopub.status.idle":"2023-10-16T03:27:55.007288Z","shell.execute_reply.started":"2023-10-16T03:27:54.969674Z","shell.execute_reply":"2023-10-16T03:27:55.006634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:02.544893Z","iopub.execute_input":"2023-10-16T03:28:02.545181Z","iopub.status.idle":"2023-10-16T03:28:03.937632Z","shell.execute_reply.started":"2023-10-16T03:28:02.545164Z","shell.execute_reply":"2023-10-16T03:28:03.937116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = df['label']\ndf.drop('label', axis = 1, inplace = True)\nind = np.random.randint(0, 20000)\nplt.figure(figsize = (20, 5))\ngrid_data = np.array(df.iloc[ind]).reshape(28,28)\nplt.imshow(grid_data, interpolation = None, cmap = 'gray')\nplt.show()\nprint(label[ind])","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:08.439805Z","iopub.execute_input":"2023-10-16T03:28:08.440120Z","iopub.status.idle":"2023-10-16T03:28:08.642877Z","shell.execute_reply.started":"2023-10-16T03:28:08.440088Z","shell.execute_reply":"2023-10-16T03:28:08.641681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nstd_df = scaler.fit_transform(df)\nstd_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:13.204731Z","iopub.execute_input":"2023-10-16T03:28:13.205020Z","iopub.status.idle":"2023-10-16T03:28:13.543492Z","shell.execute_reply.started":"2023-10-16T03:28:13.204999Z","shell.execute_reply":"2023-10-16T03:28:13.542581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"covar_mat = np.matmul(std_df.T, std_df)\ncovar_mat.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:17.954539Z","iopub.execute_input":"2023-10-16T03:28:17.954816Z","iopub.status.idle":"2023-10-16T03:28:18.323780Z","shell.execute_reply.started":"2023-10-16T03:28:17.954795Z","shell.execute_reply":"2023-10-16T03:28:18.323095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.linalg import eigh\nvalues, vectors = eigh(covar_mat, eigvals = (782, 783))\nprint(\"Dimensions of Eigen vector:\", vectors.shape)\nvectors = vectors.T\nprint(\"Dimensions of Eigen vector:\", vectors.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:21.414575Z","iopub.execute_input":"2023-10-16T03:28:21.414895Z","iopub.status.idle":"2023-10-16T03:28:21.493315Z","shell.execute_reply.started":"2023-10-16T03:28:21.414872Z","shell.execute_reply":"2023-10-16T03:28:21.492527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = np.matmul(vectors, std_df.T)\nprint(\"vectros:\", vectors.shape, \"n\", \"std_df:\", std_df.T.shape, \"n\", \"final_df:\", final_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:25.734525Z","iopub.execute_input":"2023-10-16T03:28:25.734837Z","iopub.status.idle":"2023-10-16T03:28:25.755643Z","shell.execute_reply.started":"2023-10-16T03:28:25.734816Z","shell.execute_reply":"2023-10-16T03:28:25.754927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_dfT = np.vstack((final_df, label)).T\ndataFrame = pd.DataFrame(final_dfT, columns = ['pca_1', 'pca_2', 'label'])\ndataFrame","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:28.944555Z","iopub.execute_input":"2023-10-16T03:28:28.944821Z","iopub.status.idle":"2023-10-16T03:28:28.956855Z","shell.execute_reply.started":"2023-10-16T03:28:28.944801Z","shell.execute_reply":"2023-10-16T03:28:28.956082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.FacetGrid(dataFrame, hue = 'label', height = 8).map(sns.scatterplot, 'pca_1', 'pca_2').add_legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-16T03:28:33.364799Z","iopub.execute_input":"2023-10-16T03:28:33.365260Z","iopub.status.idle":"2023-10-16T03:28:34.197407Z","shell.execute_reply.started":"2023-10-16T03:28:33.365231Z","shell.execute_reply":"2023-10-16T03:28:34.196594Z"},"trusted":true},"execution_count":null,"outputs":[]}]}