{"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)\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":"2022-10-02T03:55:16.369739Z","iopub.execute_input":"2022-10-02T03:55:16.370155Z","iopub.status.idle":"2022-10-02T03:55:16.380119Z","shell.execute_reply.started":"2022-10-02T03:55:16.370122Z","shell.execute_reply":"2022-10-02T03:55:16.378699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:55:17.579256Z","iopub.execute_input":"2022-10-02T03:55:17.580530Z","iopub.status.idle":"2022-10-02T03:55:18.815817Z","shell.execute_reply.started":"2022-10-02T03:55:17.580472Z","shell.execute_reply":"2022-10-02T03:55:18.814511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = 'Data'\ntrain_csv = [\"train_0.csv\", \"train_1.csv\", \"train_2.csv\", \"train_3.csv\", \"train_4.csv\", \"train_5.csv\", \"train_6.csv\", \"train_7.csv\", \"train_8.csv\", \"train_9.csv\"]\ntrain_0 = pd.read_csv(os.path.join(dirname, train_csv[0]))\n# train_1 = pd.read_csv(os.path.join(dirname, train_csv[1]))\n# train_2 = pd.read_csv(os.path.join(dirname, train_csv[2]))\n# train_3 = pd.read_csv(os.path.join(dirname, train_csv[3]))\n# train_4 = pd.read_csv(os.path.join(dirname, train_csv[4]))\n# train_5 = pd.read_csv(os.path.join(dirname, train_csv[5]))\n# train_6 = pd.read_csv(os.path.join(dirname, train_csv[6]))\n# train_7 = pd.read_csv(os.path.join(dirname, train_csv[7]))\n# train_8 = pd.read_csv(os.path.join(dirname, train_csv[8]))\n# train_9 = pd.read_csv(os.path.join(dirname, train_csv[9]))\ntest_data = pd.read_csv(os.path.join(dirname, 'test.csv'))\ntrain_data = train_0\n# train_data = pd.concat([train_0, train_1, train_2], ignore_index=True, axis=0)\n# del train_0","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:55:18.817918Z","iopub.execute_input":"2022-10-02T03:55:18.818701Z","iopub.status.idle":"2022-10-02T03:56:06.233650Z","shell.execute_reply.started":"2022-10-02T03:55:18.818655Z","shell.execute_reply":"2022-10-02T03:56:06.232379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y1 = train_data[train_data.columns[-1]]\nx1 = train_data[train_data.columns[3:-4]]","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:56:06.235512Z","iopub.execute_input":"2022-10-02T03:56:06.235933Z","iopub.status.idle":"2022-10-02T03:56:06.590985Z","shell.execute_reply.started":"2022-10-02T03:56:06.235896Z","shell.execute_reply":"2022-10-02T03:56:06.589604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = x1.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:56:06.593089Z","iopub.execute_input":"2022-10-02T03:56:06.593683Z","iopub.status.idle":"2022-10-02T03:56:07.109013Z","shell.execute_reply.started":"2022-10-02T03:56:06.593630Z","shell.execute_reply":"2022-10-02T03:56:07.107630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=16)\npca.fit(x1)","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:56:07.112101Z","iopub.execute_input":"2022-10-02T03:56:07.112483Z","iopub.status.idle":"2022-10-02T03:56:24.096958Z","shell.execute_reply.started":"2022-10-02T03:56:07.112446Z","shell.execute_reply":"2022-10-02T03:56:24.095715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PC_values = np.arange(pca.n_components_) + 1\nplt.plot(PC_values, pca.explained_variance_ratio_, 'o-', linewidth=2, color='blue')\nplt.title('Scree Plot')\nplt.xlabel('Principal Component')\nplt.ylabel('Variance Explained')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:56:24.098736Z","iopub.execute_input":"2022-10-02T03:56:24.099839Z","iopub.status.idle":"2022-10-02T03:56:24.346441Z","shell.execute_reply.started":"2022-10-02T03:56:24.099787Z","shell.execute_reply":"2022-10-02T03:56:24.344827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PC_values = np.arange(pca.n_components_) + 1\nvariance = pca.explained_variance_ratio_\nenergy = np.cumsum(variance)/sum(variance)\nplt.plot(PC_values, energy, 'o-', linewidth=2, color='blue')\nplt.title('Percent Energy')\nplt.xlabel('Principal Component')\nplt.ylabel('Percent Energy')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T03:56:46.050128Z","iopub.execute_input":"2022-10-02T03:56:46.050635Z","iopub.status.idle":"2022-10-02T03:56:46.284968Z","shell.execute_reply.started":"2022-10-02T03:56:46.050592Z","shell.execute_reply":"2022-10-02T03:56:46.284062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like 14is the right number of principal components. I ususally try to get ~95% energy ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}