{"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-07-29T19:53:07.462456Z","iopub.execute_input":"2022-07-29T19:53:07.463665Z","iopub.status.idle":"2022-07-29T19:53:07.494001Z","shell.execute_reply.started":"2022-07-29T19:53:07.463536Z","shell.execute_reply":"2022-07-29T19:53:07.492580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**GET DATA**","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:07.496313Z","iopub.execute_input":"2022-07-29T19:53:07.497106Z","iopub.status.idle":"2022-07-29T19:53:09.104683Z","shell.execute_reply.started":"2022-07-29T19:53:07.497062Z","shell.execute_reply":"2022-07-29T19:53:09.103505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CHECK IF ANY NAN DATA CONTAINS**","metadata":{}},{"cell_type":"code","source":"nan_keys = [];\nfor key in data.keys():\n    if data[key].isna().all() == False:\n        pass\n    else:\n        nan_keys.append()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:16.824209Z","iopub.execute_input":"2022-07-29T19:53:16.824676Z","iopub.status.idle":"2022-07-29T19:53:16.844852Z","shell.execute_reply.started":"2022-07-29T19:53:16.824642Z","shell.execute_reply":"2022-07-29T19:53:16.843329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"We see that here is no any null element.\"\"\"\nprint(nan_keys)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:18.029382Z","iopub.execute_input":"2022-07-29T19:53:18.030342Z","iopub.status.idle":"2022-07-29T19:53:18.035958Z","shell.execute_reply.started":"2022-07-29T19:53:18.030293Z","shell.execute_reply":"2022-07-29T19:53:18.034802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"We can subtract id column from data.\"\"\"\nid_column = data[\"id\"]\ndata.drop(columns = [\"id\"],inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:20.744734Z","iopub.execute_input":"2022-07-29T19:53:20.745769Z","iopub.status.idle":"2022-07-29T19:53:20.761769Z","shell.execute_reply.started":"2022-07-29T19:53:20.745712Z","shell.execute_reply":"2022-07-29T19:53:20.760496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PLOT DISTRIBUTION OF DATA**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns \nplt.figure(dpi = 75,figsize = (12,6))\nsns.boxplot(data = data)\nplt.grid(True)\nplt.title(\"Distribution of data\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:21.306184Z","iopub.execute_input":"2022-07-29T19:53:21.307624Z","iopub.status.idle":"2022-07-29T19:53:22.782737Z","shell.execute_reply.started":"2022-07-29T19:53:21.307575Z","shell.execute_reply":"2022-07-29T19:53:22.781451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**STANDARTIZATION OF DATA**","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nSS = StandardScaler()\nscaled_data = SS.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:25.848130Z","iopub.execute_input":"2022-07-29T19:53:25.849800Z","iopub.status.idle":"2022-07-29T19:53:26.014830Z","shell.execute_reply.started":"2022-07-29T19:53:25.849737Z","shell.execute_reply":"2022-07-29T19:53:26.013448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_data = pd.DataFrame(scaled_data,columns = data.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:26.133508Z","iopub.execute_input":"2022-07-29T19:53:26.133949Z","iopub.status.idle":"2022-07-29T19:53:26.140222Z","shell.execute_reply.started":"2022-07-29T19:53:26.133915Z","shell.execute_reply":"2022-07-29T19:53:26.139048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaled_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:26.469313Z","iopub.execute_input":"2022-07-29T19:53:26.469748Z","iopub.status.idle":"2022-07-29T19:53:26.498687Z","shell.execute_reply.started":"2022-07-29T19:53:26.469714Z","shell.execute_reply":"2022-07-29T19:53:26.497320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**PCA(PRINCIPAL COMPONENT ANALYSIS)**","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components = 7)\npca_data = pca.fit_transform(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:33.269416Z","iopub.execute_input":"2022-07-29T19:53:33.269772Z","iopub.status.idle":"2022-07-29T19:53:34.362173Z","shell.execute_reply.started":"2022-07-29T19:53:33.269744Z","shell.execute_reply":"2022-07-29T19:53:34.360632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_data = pd.DataFrame(pca_data,columns = [\"P0\",\"P1\",\"P2\",\"P3\",\"P4\",\"P5\",\"P6\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:34.364322Z","iopub.execute_input":"2022-07-29T19:53:34.365087Z","iopub.status.idle":"2022-07-29T19:53:34.373336Z","shell.execute_reply.started":"2022-07-29T19:53:34.365042Z","shell.execute_reply":"2022-07-29T19:53:34.371679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_data","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:37.648773Z","iopub.execute_input":"2022-07-29T19:53:37.649226Z","iopub.status.idle":"2022-07-29T19:53:37.668535Z","shell.execute_reply.started":"2022-07-29T19:53:37.649191Z","shell.execute_reply":"2022-07-29T19:53:37.667260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**USE K MEANS CLUSTERING**","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans\nK_means = KMeans(n_clusters = 7,init = \"k-means++\",n_init = 10,max_iter = 300,random_state = 42)\npreds = K_means.fit_predict(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:47.419926Z","iopub.execute_input":"2022-07-29T19:53:47.420403Z","iopub.status.idle":"2022-07-29T19:53:56.030304Z","shell.execute_reply.started":"2022-07-29T19:53:47.420364Z","shell.execute_reply":"2022-07-29T19:53:56.029253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_data[\"Predicted_Clusters\"] = preds","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:53:58.337652Z","iopub.execute_input":"2022-07-29T19:53:58.338072Z","iopub.status.idle":"2022-07-29T19:53:58.346602Z","shell.execute_reply.started":"2022-07-29T19:53:58.338041Z","shell.execute_reply":"2022-07-29T19:53:58.344808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_data","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:57:19.060014Z","iopub.execute_input":"2022-07-29T19:57:19.061136Z","iopub.status.idle":"2022-07-29T19:57:19.082756Z","shell.execute_reply.started":"2022-07-29T19:57:19.061094Z","shell.execute_reply":"2022-07-29T19:57:19.081344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**RESULT**","metadata":{}},{"cell_type":"code","source":"result = pd.DataFrame({\"id\" : id_column,\"Predicted\" : preds.ravel()})\nresult.to_csv(\"preds_clustering.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:54:15.888163Z","iopub.execute_input":"2022-07-29T19:54:15.889473Z","iopub.status.idle":"2022-07-29T19:54:16.020967Z","shell.execute_reply.started":"2022-07-29T19:54:15.889421Z","shell.execute_reply":"2022-07-29T19:54:16.019798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:54:16.269809Z","iopub.execute_input":"2022-07-29T19:54:16.272747Z","iopub.status.idle":"2022-07-29T19:54:16.284836Z","shell.execute_reply.started":"2022-07-29T19:54:16.272703Z","shell.execute_reply":"2022-07-29T19:54:16.284025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**VISUALIZATION OF CLUSTERING**","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\ngraph = sns.PairGrid(pca_data,vars = [\"P0\",\"P1\",\"P2\",\"P3\"],hue = \"Predicted_Clusters\",palette = \"tab10\")\ngraph.map_diag(sns.histplot)\ngraph.map_offdiag(sns.scatterplot)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T19:59:39.352549Z","iopub.execute_input":"2022-07-29T19:59:39.353093Z","iopub.status.idle":"2022-07-29T20:00:13.529602Z","shell.execute_reply.started":"2022-07-29T19:59:39.353046Z","shell.execute_reply":"2022-07-29T20:00:13.528354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}