{"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-30T07:07:19.832932Z","iopub.execute_input":"2022-07-30T07:07:19.833370Z","iopub.status.idle":"2022-07-30T07:07:19.848533Z","shell.execute_reply.started":"2022-07-30T07:07:19.833336Z","shell.execute_reply":"2022-07-30T07:07:19.847089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans\nimport matplotlib.pyplot as plt\nimport seaborn as sns \nfrom sklearn.preprocessing import PowerTransformer\nfrom sklearn.preprocessing import RobustScaler\nfrom sklearn.decomposition import PCA\nfrom yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.mixture import GaussianMixture\nfrom sklearn.cluster import MeanShift\nfrom sklearn.cluster import DBSCAN\nfrom sklearn.neighbors import NearestNeighbors\nimport plotly.express as px\nfrom sklearn.mixture import BayesianGaussianMixture\nfrom sklearn.cluster import AgglomerativeClustering\n%matplotlib inline\nsns.set_theme(style = \"whitegrid\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T07:10:05.590292Z","iopub.execute_input":"2022-07-30T07:10:05.590764Z","iopub.status.idle":"2022-07-30T07:10:05.602758Z","shell.execute_reply.started":"2022-07-30T07:10:05.590727Z","shell.execute_reply":"2022-07-30T07:10:05.601704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')\ndata.set_index('id', inplace = True)\nId = pd.Series(data.index, name = 'Id')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T07:07:32.009914Z","iopub.execute_input":"2022-07-30T07:07:32.010343Z","iopub.status.idle":"2022-07-30T07:07:33.888113Z","shell.execute_reply.started":"2022-07-30T07:07:32.010309Z","shell.execute_reply":"2022-07-30T07:07:33.886685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.shape)\nprint('Number of missing values: ', data.isnull().sum().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:16:21.918224Z","iopub.execute_input":"2022-07-29T09:16:21.918582Z","iopub.status.idle":"2022-07-29T09:16:21.932182Z","shell.execute_reply.started":"2022-07-29T09:16:21.918551Z","shell.execute_reply":"2022-07-29T09:16:21.931174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()\n# all columns are numeric","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:16:21.933442Z","iopub.execute_input":"2022-07-29T09:16:21.934001Z","iopub.status.idle":"2022-07-29T09:16:21.961871Z","shell.execute_reply.started":"2022-07-29T09:16:21.933970Z","shell.execute_reply":"2022-07-29T09:16:21.960800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()\n# we need to scale variables","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:16:21.964052Z","iopub.execute_input":"2022-07-29T09:16:21.964389Z","iopub.status.idle":"2022-07-29T09:16:22.186012Z","shell.execute_reply.started":"2022-07-29T09:16:21.964359Z","shell.execute_reply":"2022-07-29T09:16:22.184909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# distributions of numeric data\ndf = pd.melt(data, value_vars = data.columns)\ng = sns.FacetGrid(df, col = \"variable\", col_wrap = 4, sharex = False, sharey = False)\ng = g.map(sns.histplot, \"value\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T07:15:31.687533Z","iopub.execute_input":"2022-07-30T07:15:31.687961Z","iopub.status.idle":"2022-07-30T07:16:03.396950Z","shell.execute_reply.started":"2022-07-30T07:15:31.687927Z","shell.execute_reply":"2022-07-30T07:16:03.395968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# standardizing\ndata_scaled = data.copy()\ndata_scaled[:] = PowerTransformer().fit_transform(data)\ndata_scaled[:] = RobustScaler().fit_transform(data_scaled)\n\n# distributions of scaled data\ndf = pd.melt(data_scaled, value_vars = data_scaled.columns)\ng = sns.FacetGrid(df, col = \"variable\", col_wrap = 4, sharex = False, sharey = False)\ng = g.map(sns.histplot, \"value\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T07:16:10.255677Z","iopub.execute_input":"2022-07-30T07:16:10.256084Z","iopub.status.idle":"2022-07-30T07:16:46.433508Z","shell.execute_reply.started":"2022-07-30T07:16:10.256052Z","shell.execute_reply":"2022-07-30T07:16:46.432230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data_scaled.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T07:20:40.410698Z","iopub.execute_input":"2022-07-30T07:20:40.411139Z","iopub.status.idle":"2022-07-30T07:20:40.449734Z","shell.execute_reply.started":"2022-07-30T07:20:40.411104Z","shell.execute_reply":"2022-07-30T07:20:40.448384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PCA","metadata":{}},{"cell_type":"code","source":"# PCA\npca = PCA(n_components = 2) # 2-dimensional PCA\n\nsample = X.sample(700)\ntransformed = pd.DataFrame(pca.fit_transform(sample))\nplt.scatter(transformed[0], transformed[1], edgecolors = 'black')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:16:22.284898Z","iopub.execute_input":"2022-07-29T09:16:22.285517Z","iopub.status.idle":"2022-07-29T09:16:22.606934Z","shell.execute_reply.started":"2022-07-29T09:16:22.285484Z","shell.execute_reply":"2022-07-29T09:16:22.605700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### KMeans clustering","metadata":{}},{"cell_type":"code","source":"# Selecting number of clusters - Elbow Method for K means\nmodel = KMeans()\n\n# k is range of number of clusters\nvisualizer = KElbowVisualizer(model, k = (1, 30), timings = True)\nvisualizer.fit(X)        \nvisualizer.show()   \n\n# optimal number of clusters is 8","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:16:22.608682Z","iopub.execute_input":"2022-07-29T09:16:22.609464Z","iopub.status.idle":"2022-07-29T09:25:12.338235Z","shell.execute_reply.started":"2022-07-29T09:16:22.609397Z","shell.execute_reply":"2022-07-29T09:25:12.337310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(n_clusters = 8, random_state = 0)\nkmeans.fit(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:25:12.339705Z","iopub.execute_input":"2022-07-29T09:25:12.340053Z","iopub.status.idle":"2022-07-29T09:25:21.190116Z","shell.execute_reply.started":"2022-07-29T09:25:12.340016Z","shell.execute_reply":"2022-07-29T09:25:21.189014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving for submission\nId = pd.Series(data.index, name = 'Id')\nPredicted = pd.Series(kmeans.labels_, name = 'Predicted')\nsubmission = pd.concat([Id, Predicted], axis = 1)\nsubmission.to_csv('submission1.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T09:25:21.191705Z","iopub.execute_input":"2022-07-29T09:25:21.192150Z","iopub.status.idle":"2022-07-29T09:25:21.357897Z","shell.execute_reply.started":"2022-07-29T09:25:21.192095Z","shell.execute_reply":"2022-07-29T09:25:21.356691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Gaussian mixture model","metadata":{}},{"cell_type":"code","source":"# Gaussian mixture model\ngmm_model = GaussianMixture(n_components = 8)\ngmm_model.fit(X)\n\nPredicted = pd.Series(gmm_model.predict(X), name = 'Predicted')\nsubmission2 = pd.concat([Id, Predicted], axis = 1)\nsubmission2.to_csv('submission_GMM_8.csv', index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# BayesianGaussianMixture\nbgm = BayesianGaussianMixture(n_components = 6, random_state = 1, max_iter = 500, n_init = 3, verbose = 0)\nbgm.fit(X)\nPredicted = pd.Series(bgm.predict(X), name = 'Predicted')\nsubmission3 = pd.concat([Id, Predicted], axis = 1)\nsubmission3.to_csv('submission_BGM_6.csv', index = False)\n\nbgm = BayesianGaussianMixture(n_components = 7, random_state = 1, max_iter = 500, n_init = 3, verbose = 0)\nbgm.fit(X)\nPredicted = pd.Series(bgm.predict(X), name = 'Predicted')\nsubmission3 = pd.concat([Id, Predicted], axis = 1)\nsubmission3.to_csv('submission_BGM_7.csv', index = False)\n\nbgm = BayesianGaussianMixture(n_components = 8, random_state = 1, max_iter = 500, n_init = 3, verbose = 0)\nbgm.fit(X)\nPredicted = pd.Series(bgm.predict(X), name = 'Predicted')\nsubmission3 = pd.concat([Id, Predicted], axis = 1)\nsubmission3.to_csv('submission_BGM_8.csv', index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}