{"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":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\n\nfrom sklearn.decomposition import PCA\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-20T08:20:17.592411Z","iopub.execute_input":"2022-07-20T08:20:17.592758Z","iopub.status.idle":"2022-07-20T08:20:17.600811Z","shell.execute_reply.started":"2022-07-20T08:20:17.592730Z","shell.execute_reply":"2022-07-20T08:20:17.599830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Data","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/mercedes-benz-greener-manufacturing/train.csv.zip')","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:06:37.815181Z","iopub.execute_input":"2022-07-20T08:06:37.815558Z","iopub.status.idle":"2022-07-20T08:06:37.988053Z","shell.execute_reply.started":"2022-07-20T08:06:37.815529Z","shell.execute_reply":"2022-07-20T08:06:37.986551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check Data","metadata":{}},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:06:53.870527Z","iopub.execute_input":"2022-07-20T08:06:53.870920Z","iopub.status.idle":"2022-07-20T08:06:54.415641Z","shell.execute_reply.started":"2022-07-20T08:06:53.870889Z","shell.execute_reply":"2022-07-20T08:06:54.413206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:07:01.737904Z","iopub.execute_input":"2022-07-20T08:07:01.738490Z","iopub.status.idle":"2022-07-20T08:07:01.771891Z","shell.execute_reply.started":"2022-07-20T08:07:01.738452Z","shell.execute_reply":"2022-07-20T08:07:01.770887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Divide Data into X and y","metadata":{}},{"cell_type":"code","source":"y = train['y']\nX = train.drop(['ID','y'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:11:04.503301Z","iopub.execute_input":"2022-07-20T08:11:04.503644Z","iopub.status.idle":"2022-07-20T08:11:04.511471Z","shell.execute_reply.started":"2022-07-20T08:11:04.503619Z","shell.execute_reply":"2022-07-20T08:11:04.510537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Keep Only Numerical Features","metadata":{}},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:23:51.410796Z","iopub.execute_input":"2022-07-20T08:23:51.411312Z","iopub.status.idle":"2022-07-20T08:23:51.422405Z","shell.execute_reply.started":"2022-07-20T08:23:51.411272Z","shell.execute_reply":"2022-07-20T08:23:51.421459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_features = X.columns[X.dtypes == 'int64'].values\nnumerical_features","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:15:55.192798Z","iopub.execute_input":"2022-07-20T08:15:55.193169Z","iopub.status.idle":"2022-07-20T08:15:55.201862Z","shell.execute_reply.started":"2022-07-20T08:15:55.193132Z","shell.execute_reply":"2022-07-20T08:15:55.200957Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### X - Take only Numerical Features","metadata":{}},{"cell_type":"code","source":"X = X[numerical_features]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:16:13.945124Z","iopub.execute_input":"2022-07-20T08:16:13.946222Z","iopub.status.idle":"2022-07-20T08:16:13.953811Z","shell.execute_reply.started":"2022-07-20T08:16:13.946187Z","shell.execute_reply":"2022-07-20T08:16:13.952743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA ","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components=2)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:16:22.849005Z","iopub.execute_input":"2022-07-20T08:16:22.849376Z","iopub.status.idle":"2022-07-20T08:16:22.854203Z","shell.execute_reply.started":"2022-07-20T08:16:22.849348Z","shell.execute_reply":"2022-07-20T08:16:22.853240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.fit(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:16:25.121145Z","iopub.execute_input":"2022-07-20T08:16:25.122456Z","iopub.status.idle":"2022-07-20T08:16:25.285234Z","shell.execute_reply.started":"2022-07-20T08:16:25.122408Z","shell.execute_reply":"2022-07-20T08:16:25.284481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pca.explained_variance_ratio_)\nprint(pca.explained_variance_)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:29:38.523888Z","iopub.execute_input":"2022-07-20T08:29:38.524494Z","iopub.status.idle":"2022-07-20T08:29:38.529835Z","shell.execute_reply.started":"2022-07-20T08:29:38.524463Z","shell.execute_reply":"2022-07-20T08:29:38.528411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pca_array = pca.transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:28:09.611541Z","iopub.execute_input":"2022-07-20T08:28:09.611932Z","iopub.status.idle":"2022-07-20T08:28:09.628034Z","shell.execute_reply.started":"2022-07-20T08:28:09.611903Z","shell.execute_reply":"2022-07-20T08:28:09.627230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pca = pd.DataFrame(X_pca_array, columns =['PCA1', 'PCA2'])\nX_pca.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T08:28:44.547335Z","iopub.execute_input":"2022-07-20T08:28:44.547714Z","iopub.status.idle":"2022-07-20T08:28:44.559176Z","shell.execute_reply.started":"2022-07-20T08:28:44.547686Z","shell.execute_reply":"2022-07-20T08:28:44.558528Z"},"trusted":true},"execution_count":null,"outputs":[]}]}