{"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":"# 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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-31T21:56:01.411552Z","iopub.execute_input":"2022-07-31T21:56:01.412027Z","iopub.status.idle":"2022-07-31T21:56:01.444580Z","shell.execute_reply.started":"2022-07-31T21:56:01.411930Z","shell.execute_reply":"2022-07-31T21:56:01.443757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:01.449723Z","iopub.execute_input":"2022-07-31T21:56:01.450302Z","iopub.status.idle":"2022-07-31T21:56:01.454683Z","shell.execute_reply.started":"2022-07-31T21:56:01.450262Z","shell.execute_reply":"2022-07-31T21:56:01.453572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\nsubmission = pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:01.480613Z","iopub.execute_input":"2022-07-31T21:56:01.481688Z","iopub.status.idle":"2022-07-31T21:56:02.928258Z","shell.execute_reply.started":"2022-07-31T21:56:01.481647Z","shell.execute_reply":"2022-07-31T21:56:02.927095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_cols = [ x for (x, y) in data.dtypes.items() if y == \"int64\" ]\nnum_cols = [ x for (x, y) in data.dtypes.items() if y == \"float64\" ]\n\ncat_data = data[cat_cols]\nnum_data = data[num_cols]","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:02.930424Z","iopub.execute_input":"2022-07-31T21:56:02.930882Z","iopub.status.idle":"2022-07-31T21:56:02.951230Z","shell.execute_reply.started":"2022-07-31T21:56:02.930838Z","shell.execute_reply":"2022-07-31T21:56:02.949881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_data","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:02.953145Z","iopub.execute_input":"2022-07-31T21:56:02.953638Z","iopub.status.idle":"2022-07-31T21:56:02.985075Z","shell.execute_reply.started":"2022-07-31T21:56:02.953592Z","shell.execute_reply":"2022-07-31T21:56:02.983873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_data","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:02.988081Z","iopub.execute_input":"2022-07-31T21:56:02.989110Z","iopub.status.idle":"2022-07-31T21:56:03.041318Z","shell.execute_reply.started":"2022-07-31T21:56:02.989064Z","shell.execute_reply":"2022-07-31T21:56:03.040079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(\"id\", axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:03.043208Z","iopub.execute_input":"2022-07-31T21:56:03.043974Z","iopub.status.idle":"2022-07-31T21:56:03.060016Z","shell.execute_reply.started":"2022-07-31T21:56:03.043926Z","shell.execute_reply":"2022-07-31T21:56:03.058314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nnorm_data = scaler.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:03.061889Z","iopub.execute_input":"2022-07-31T21:56:03.062555Z","iopub.status.idle":"2022-07-31T21:56:04.340263Z","shell.execute_reply.started":"2022-07-31T21:56:03.062518Z","shell.execute_reply":"2022-07-31T21:56:04.338859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans\n\nmodel = KMeans(n_clusters=3)\nmodel.fit(norm_data)\npredictions = model.predict(norm_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:04.342318Z","iopub.execute_input":"2022-07-31T21:56:04.343054Z","iopub.status.idle":"2022-07-31T21:56:09.705982Z","shell.execute_reply.started":"2022-07-31T21:56:04.343008Z","shell.execute_reply":"2022-07-31T21:56:09.704295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\npca = PCA(n_components=2)\nred_data = pca.fit_transform(norm_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:09.708141Z","iopub.execute_input":"2022-07-31T21:56:09.713811Z","iopub.status.idle":"2022-07-31T21:56:10.525330Z","shell.execute_reply.started":"2022-07-31T21:56:09.713746Z","shell.execute_reply":"2022-07-31T21:56:10.523931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(\n    {\n        \"x\" : red_data[:,0],\n        \"y\" : red_data[:,1],\n        \"clusters\" : predictions\n    \n    }\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:10.527299Z","iopub.execute_input":"2022-07-31T21:56:10.528218Z","iopub.status.idle":"2022-07-31T21:56:10.550837Z","shell.execute_reply.started":"2022-07-31T21:56:10.528160Z","shell.execute_reply":"2022-07-31T21:56:10.548795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"Predicted\"] = predictions\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:10.556333Z","iopub.execute_input":"2022-07-31T21:56:10.558037Z","iopub.status.idle":"2022-07-31T21:56:10.697854Z","shell.execute_reply.started":"2022-07-31T21:56:10.557966Z","shell.execute_reply":"2022-07-31T21:56:10.696203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-31T21:56:10.699403Z","iopub.status.idle":"2022-07-31T21:56:10.700235Z","shell.execute_reply.started":"2022-07-31T21:56:10.699875Z","shell.execute_reply":"2022-07-31T21:56:10.699914Z"},"trusted":true},"execution_count":null,"outputs":[]}]}