{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:37.794322Z","iopub.execute_input":"2022-07-10T11:32:37.794792Z","iopub.status.idle":"2022-07-10T11:32:39.009021Z","shell.execute_reply.started":"2022-07-10T11:32:37.794696Z","shell.execute_reply":"2022-07-10T11:32:39.007708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* See also:\n    * https://github.com/sdysch/kaggle/blob/master/tps-july-2022.ipynb\n    * https://www.kaggle.com/code/sdysch/tps-july-2022\n","metadata":{}},{"cell_type":"code","source":"plt.style.use('seaborn')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:39.011439Z","iopub.execute_input":"2022-07-10T11:32:39.011899Z","iopub.status.idle":"2022-07-10T11:32:39.018356Z","shell.execute_reply.started":"2022-07-10T11:32:39.011852Z","shell.execute_reply":"2022-07-10T11:32:39.017276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv', index_col='id')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:39.019450Z","iopub.execute_input":"2022-07-10T11:32:39.020484Z","iopub.status.idle":"2022-07-10T11:32:40.400767Z","shell.execute_reply.started":"2022-07-10T11:32:39.020434Z","shell.execute_reply":"2022-07-10T11:32:40.399488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standardize distributions","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\ndata[data.columns] = scaler.fit_transform(data[data.columns])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:40.403556Z","iopub.execute_input":"2022-07-10T11:32:40.403954Z","iopub.status.idle":"2022-07-10T11:32:40.658814Z","shell.execute_reply.started":"2022-07-10T11:32:40.403919Z","shell.execute_reply":"2022-07-10T11:32:40.657782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dimensionality reduction","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nN_COMPONENTS = data.shape[1]\npca = PCA(n_components=N_COMPONENTS)\ndata_red = pca.fit_transform(data[data.columns])","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:40.659917Z","iopub.execute_input":"2022-07-10T11:32:40.660479Z","iopub.status.idle":"2022-07-10T11:32:40.998377Z","shell.execute_reply.started":"2022-07-10T11:32:40.660442Z","shell.execute_reply":"2022-07-10T11:32:40.996629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot cumulative explained variance ratio against n_components\nfig, ax = plt.subplots(figsize=(15, 7))\ncumulative_sum = np.cumsum(pca.explained_variance_ratio_)\nax.plot(range(1, 1 + len(cumulative_sum)), cumulative_sum, marker='o', linestyle='-', color='b')\nax.axhline(y=1, color='r', linestyle='dashed')","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:41.000662Z","iopub.execute_input":"2022-07-10T11:32:41.001559Z","iopub.status.idle":"2022-07-10T11:32:41.259407Z","shell.execute_reply.started":"2022-07-10T11:32:41.001496Z","shell.execute_reply":"2022-07-10T11:32:41.258527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_COMPONENTS = 25\npca = PCA(n_components=N_COMPONENTS)\ndata_red = pca.fit_transform(data[data.columns])\ndata_red","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:41.260968Z","iopub.execute_input":"2022-07-10T11:32:41.262045Z","iopub.status.idle":"2022-07-10T11:32:41.406687Z","shell.execute_reply.started":"2022-07-10T11:32:41.262002Z","shell.execute_reply":"2022-07-10T11:32:41.405287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Deciding number of clusters","metadata":{}},{"cell_type":"code","source":"SEED = 42\nCLUSTERS = 20\nfrom sklearn.mixture import GaussianMixture\nclusters, bics = list(), list()\nfor cluster in range(2, CLUSTERS):\n    print(f'Components: {cluster}')\n    model = GaussianMixture(n_components=cluster, random_state=SEED)\n    \n    # reduced data\n    # model.fit(data_red)\n    # bics.append(model.bic(data_red))\n    \n    # original data\n    model.fit(data)\n    bics.append(model.bic(data))\n    \n    clusters.append(cluster)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:32:41.408768Z","iopub.execute_input":"2022-07-10T11:32:41.409678Z","iopub.status.idle":"2022-07-10T11:43:11.434532Z","shell.execute_reply.started":"2022-07-10T11:32:41.409624Z","shell.execute_reply":"2022-07-10T11:43:11.433137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(15, 10))\nax.plot(clusters, bics, marker='o', label='PCA')\nax.set_xlabel('Number of components')\nax.set_ylabel('Bayesian information criterion')\nax.legend(loc='best')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:43:11.436847Z","iopub.execute_input":"2022-07-10T11:43:11.437934Z","iopub.status.idle":"2022-07-10T11:43:11.684859Z","shell.execute_reply.started":"2022-07-10T11:43:11.437872Z","shell.execute_reply":"2022-07-10T11:43:11.683920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import GaussianMixture\nmodel = GaussianMixture(n_components=7)\n\n# Fit to reduced data\n# model.fit(data_red)\n# pred = model.predict(data_red)\n\n# Fit to original data\nmodel.fit(data)\npred = model.predict(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:44:11.234710Z","iopub.execute_input":"2022-07-10T11:44:11.235109Z","iopub.status.idle":"2022-07-10T11:44:35.123253Z","shell.execute_reply.started":"2022-07-10T11:44:11.235078Z","shell.execute_reply":"2022-07-10T11:44:35.121890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualise clusters in different projections","metadata":{}},{"cell_type":"code","source":"# plot features, colour by class\ns = 100\nfig, ax = plt.subplots(2, 2, figsize=(15, 15))\nax[0, 0].scatter(data_red[:, 0], data_red[:, 1], c=pred, cmap='gist_rainbow', edgecolor='k', s=s)\nax[0, 0].set_xlabel('PC1')\nax[0, 0].set_ylabel('PC2')\n\nax[0, 1].scatter(data_red[:, 0], data_red[:, 2], c=pred, cmap='gist_rainbow', edgecolor='k', s=s)\nax[0, 1].set_xlabel('PC1')\nax[0, 1].set_ylabel('PC3')\n\nax[1, 0].scatter(data_red[:, 1], data_red[:, 2], c=pred, cmap='gist_rainbow', edgecolor='k', s=s)\nax[1, 0].set_xlabel('PC2')\nax[1, 0].set_ylabel('PC3')\n\nax[1, 1].scatter(data_red[:, 2], data_red[:, 3], c=pred, cmap='gist_rainbow', edgecolor='k', s=s)\nax[1, 1].set_xlabel('PC3')\nax[1, 1].set_ylabel('PC4')\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:46:38.050400Z","iopub.execute_input":"2022-07-10T11:46:38.050826Z","iopub.status.idle":"2022-07-10T11:46:47.386815Z","shell.execute_reply.started":"2022-07-10T11:46:38.050791Z","shell.execute_reply":"2022-07-10T11:46:47.385354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/tabular-playground-series-jul-2022/sample_submission.csv')\nsubmission[\"Predicted\"] = pred\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-10T11:47:21.960827Z","iopub.execute_input":"2022-07-10T11:47:21.961249Z","iopub.status.idle":"2022-07-10T11:47:22.180971Z","shell.execute_reply.started":"2022-07-10T11:47:21.961215Z","shell.execute_reply":"2022-07-10T11:47:22.179594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}