{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-06T17:29:09.338269Z","iopub.execute_input":"2022-07-06T17:29:09.338650Z","iopub.status.idle":"2022-07-06T17:29:09.344314Z","shell.execute_reply.started":"2022-07-06T17:29:09.338620Z","shell.execute_reply":"2022-07-06T17:29:09.343132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('seaborn')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:09.362294Z","iopub.execute_input":"2022-07-06T17:29:09.362690Z","iopub.status.idle":"2022-07-06T17:29:09.368003Z","shell.execute_reply.started":"2022-07-06T17:29:09.362649Z","shell.execute_reply":"2022-07-06T17:29:09.366642Z"},"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-06T17:29:09.383333Z","iopub.execute_input":"2022-07-06T17:29:09.383746Z","iopub.status.idle":"2022-07-06T17:29:10.089976Z","shell.execute_reply.started":"2022-07-06T17:29:09.383711Z","shell.execute_reply":"2022-07-06T17:29:10.089118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Initial look at data","metadata":{}},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:10.091568Z","iopub.execute_input":"2022-07-06T17:29:10.092089Z","iopub.status.idle":"2022-07-06T17:29:10.285449Z","shell.execute_reply.started":"2022-07-06T17:29:10.092056Z","shell.execute_reply":"2022-07-06T17:29:10.284292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:10.288361Z","iopub.execute_input":"2022-07-06T17:29:10.288696Z","iopub.status.idle":"2022-07-06T17:29:10.302776Z","shell.execute_reply.started":"2022-07-06T17:29:10.288667Z","shell.execute_reply":"2022-07-06T17:29:10.301392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Individual histograms","metadata":{}},{"cell_type":"code","source":"nrows, ncols = 5, 6\nfig, ax = plt.subplots(nrows, ncols, figsize=(20, 20))\nax = ax.ravel()\nfor i, var in enumerate(data.columns):\n    bins = 50\n    ax[i].hist(data[var], density=True, bins=bins)\n    ax[i].set_xlabel(var)\nax = ax.reshape(nrows, ncols)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:10.305670Z","iopub.execute_input":"2022-07-06T17:29:10.306009Z","iopub.status.idle":"2022-07-06T17:29:15.749650Z","shell.execute_reply.started":"2022-07-06T17:29:10.305979Z","shell.execute_reply":"2022-07-06T17:29:15.748267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlations","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(20, 20))\nsns.heatmap(data.corr(), annot=True, ax=ax, cmap=sns.color_palette(\"coolwarm\", as_cmap=True))","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:15.751221Z","iopub.execute_input":"2022-07-06T17:29:15.751723Z","iopub.status.idle":"2022-07-06T17:29:19.291028Z","shell.execute_reply.started":"2022-07-06T17:29:15.751673Z","shell.execute_reply":"2022-07-06T17:29:19.289841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* None of the features are particularly correlated\n* Mixture of continuous and discrete features\n* Investigate outliers","metadata":{}},{"cell_type":"code","source":"nrows, ncols = 5, 6\nfig, ax = plt.subplots(nrows, ncols, figsize=(20, 20))\nax = ax.ravel()\nfor i, var in enumerate(data.columns):\n    # ax[i].boxplot(data[var], vert=False)\n    sns.boxplot(x=data[var], ax=ax[i])\nax = ax.reshape(nrows, ncols)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:19.292390Z","iopub.execute_input":"2022-07-06T17:29:19.292761Z","iopub.status.idle":"2022-07-06T17:29:21.495348Z","shell.execute_reply.started":"2022-07-06T17:29:19.292715Z","shell.execute_reply":"2022-07-06T17:29:21.494257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# try power transform to reduce outlier impact\nfrom sklearn.preprocessing import PowerTransformer\ntransformer = PowerTransformer(method='yeo-johnson')\ndata_transformed = data.copy(deep=True)\ndata_transformed[data.columns] = transformer.fit_transform(data_transformed[data.columns])\n\nnrows, ncols = 5, 6\nfig, ax = plt.subplots(nrows, ncols, figsize=(20, 20))\nax = ax.ravel()\nfor i, var in enumerate(data_transformed.columns):\n    bins = 50\n    ax[i].hist(data_transformed[var], bins=bins, density=True)\n    ax[i].set_xlabel(var)\nax = ax.reshape(nrows, ncols)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:21.496799Z","iopub.execute_input":"2022-07-06T17:29:21.497701Z","iopub.status.idle":"2022-07-06T17:29:30.444544Z","shell.execute_reply.started":"2022-07-06T17:29:21.497657Z","shell.execute_reply":"2022-07-06T17:29:30.443721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows, ncols = 5, 6\nfig, ax = plt.subplots(nrows, ncols, figsize=(20, 20))\nax = ax.ravel()\nfor i, var in enumerate(data_transformed.columns):\n    sns.boxplot(x=data_transformed[var], ax=ax[i])\nax = ax.reshape(nrows, ncols)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:30.445877Z","iopub.execute_input":"2022-07-06T17:29:30.446688Z","iopub.status.idle":"2022-07-06T17:29:32.979150Z","shell.execute_reply.started":"2022-07-06T17:29:30.446655Z","shell.execute_reply":"2022-07-06T17:29:32.977954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# K-means clustering","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\ndata_transformed[data_transformed.columns] = scaler.fit_transform(data_transformed[data_transformed.columns])","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:32.980782Z","iopub.execute_input":"2022-07-06T17:29:32.981471Z","iopub.status.idle":"2022-07-06T17:29:33.052856Z","shell.execute_reply.started":"2022-07-06T17:29:32.981427Z","shell.execute_reply":"2022-07-06T17:29:33.051722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans\nCLUSTERS = 20\nSEED = 42","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:33.057278Z","iopub.execute_input":"2022-07-06T17:29:33.057690Z","iopub.status.idle":"2022-07-06T17:29:33.063339Z","shell.execute_reply.started":"2022-07-06T17:29:33.057655Z","shell.execute_reply":"2022-07-06T17:29:33.061807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# save inertia for \"elbow plot\"\ninertias = list()\nclusters = list()\nfor cluster in range(2, CLUSTERS):\n    model = KMeans(n_clusters=cluster, random_state=SEED)\n    model.fit(data_transformed)\n    inertias.append(model.inertia_)\n    clusters.append(cluster)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:33.065115Z","iopub.execute_input":"2022-07-06T17:29:33.066175Z","iopub.status.idle":"2022-07-06T17:29:33.088062Z","shell.execute_reply.started":"2022-07-06T17:29:33.066127Z","shell.execute_reply":"2022-07-06T17:29:33.078836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"fig, ax = plt.subplots(1, 1, figsize=(15, 10))\nax.plot(clusters, inertias, marker='o')\nax.set_xlabel('Number of clusters')\nax.set_ylabel('Inertia')\nfig.show()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:33.089935Z","iopub.execute_input":"2022-07-06T17:29:33.094350Z","iopub.status.idle":"2022-07-06T17:29:33.102182Z","shell.execute_reply.started":"2022-07-06T17:29:33.094292Z","shell.execute_reply":"2022-07-06T17:29:33.100925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dimensionality reduction","metadata":{"execution":{"iopub.status.busy":"2022-07-03T17:28:34.042762Z","iopub.execute_input":"2022-07-03T17:28:34.04318Z","iopub.status.idle":"2022-07-03T17:28:34.051454Z","shell.execute_reply.started":"2022-07-03T17:28:34.043144Z","shell.execute_reply":"2022-07-03T17:28:34.050629Z"}}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nN_COMPONENTS = data_transformed.shape[1]\npca = PCA(n_components=N_COMPONENTS)\ndata_red = pca.fit_transform(data_transformed[data_transformed.columns])\n\n# 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-06T17:50:51.589772Z","iopub.execute_input":"2022-07-06T17:50:51.590156Z","iopub.status.idle":"2022-07-06T17:50:51.938952Z","shell.execute_reply.started":"2022-07-06T17:50:51.590123Z","shell.execute_reply":"2022-07-06T17:50:51.937585Z"},"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_transformed[data_transformed.columns])\ndata_red","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:33.475651Z","iopub.execute_input":"2022-07-06T17:29:33.475985Z","iopub.status.idle":"2022-07-06T17:29:33.604619Z","shell.execute_reply.started":"2022-07-06T17:29:33.475956Z","shell.execute_reply":"2022-07-06T17:29:33.602729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save inertia for \"elbow plot\"\ninertias_red = list()\nclusters_red = list()\nCLUSTERS = 30\nfor cluster in range(2, CLUSTERS):\n    model = KMeans(n_clusters=cluster, random_state=SEED)\n    model.fit(data_red)\n    inertias_red.append(model.inertia_)\n    clusters_red.append(cluster)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:29:33.609836Z","iopub.execute_input":"2022-07-06T17:29:33.610873Z","iopub.status.idle":"2022-07-06T17:38:17.124473Z","shell.execute_reply.started":"2022-07-06T17:29:33.610808Z","shell.execute_reply":"2022-07-06T17:38:17.123330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(15, 10))\nax.plot(clusters_red, inertias_red, marker='o', label='PCA')\n# ax.plot(inertias, marker='o', label='Nominal')\nax.set_xlabel('Number of clusters')\nax.set_ylabel('Inertia')\nax.legend(loc='best')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:38:17.125752Z","iopub.execute_input":"2022-07-06T17:38:17.126072Z","iopub.status.idle":"2022-07-06T17:38:17.286700Z","shell.execute_reply.started":"2022-07-06T17:38:17.126045Z","shell.execute_reply":"2022-07-06T17:38:17.285608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualise clusters","metadata":{}},{"cell_type":"code","source":"# arbitrary choice\nmodel = KMeans(n_clusters=7)\nmodel.fit(data_red)\npred = model.predict(data_red)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:38:17.288446Z","iopub.execute_input":"2022-07-06T17:38:17.289433Z","iopub.status.idle":"2022-07-06T17:38:23.294639Z","shell.execute_reply.started":"2022-07-06T17:38:17.289398Z","shell.execute_reply":"2022-07-06T17:38:23.292854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot features, colour by class\nfig, ax = plt.subplots(1, 1, figsize=(15, 15))\nax.scatter(data_red[:, 0], data_red[:, 1], c=pred, cmap='gist_rainbow', edgecolor='k', s=150)\n\nax.set_xlabel('PC1')\nax.set_ylabel('PC2')","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:38:23.297119Z","iopub.execute_input":"2022-07-06T17:38:23.297550Z","iopub.status.idle":"2022-07-06T17:38:25.707353Z","shell.execute_reply.started":"2022-07-06T17:38:23.297513Z","shell.execute_reply":"2022-07-06T17:38:25.706445Z"},"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.head()\nsubmission[\"Predicted\"] = pred\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:38:25.708503Z","iopub.execute_input":"2022-07-06T17:38:25.709236Z","iopub.status.idle":"2022-07-06T17:38:25.851019Z","shell.execute_reply.started":"2022-07-06T17:38:25.709188Z","shell.execute_reply":"2022-07-06T17:38:25.849939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Predicted'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T17:38:25.852467Z","iopub.execute_input":"2022-07-06T17:38:25.852826Z","iopub.status.idle":"2022-07-06T17:38:25.863794Z","shell.execute_reply.started":"2022-07-06T17:38:25.852796Z","shell.execute_reply":"2022-07-06T17:38:25.862602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}