{"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":"raw","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### Introduction\n\nFor this challenge, you are given (simulated) manufacturing control data that can be clustered into different control states. Your task is to cluster the data into these control states. You are not given any training data, and you are not told how many possible control states there are. This is a completely unsupervised problem, one you might encounter in a real-world setting.\n","metadata":{}},{"cell_type":"markdown","source":"### import necessary libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport time\n\nfrom sklearn.cluster import KMeans\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.mixture import GaussianMixture, BayesianGaussianMixture\nfrom sklearn.decomposition import PCA\n\nimport plotly.express as px\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:41.417239Z","iopub.execute_input":"2022-07-29T13:28:41.417762Z","iopub.status.idle":"2022-07-29T13:28:41.426465Z","shell.execute_reply.started":"2022-07-29T13:28:41.417722Z","shell.execute_reply":"2022-07-29T13:28:41.424962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### load the data","metadata":{}},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/tabular-playground-series-jul-2022/data.csv\")\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:41.434195Z","iopub.execute_input":"2022-07-29T13:28:41.434655Z","iopub.status.idle":"2022-07-29T13:28:42.251487Z","shell.execute_reply.started":"2022-07-29T13:28:41.434615Z","shell.execute_reply":"2022-07-29T13:28:42.249874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\n The shape of the data:\", data.shape)\nprint(\"\\n The information of the dataset\", data.info())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.253330Z","iopub.execute_input":"2022-07-29T13:28:42.253831Z","iopub.status.idle":"2022-07-29T13:28:42.278452Z","shell.execute_reply.started":"2022-07-29T13:28:42.253793Z","shell.execute_reply":"2022-07-29T13:28:42.277030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.281467Z","iopub.execute_input":"2022-07-29T13:28:42.282450Z","iopub.status.idle":"2022-07-29T13:28:42.497850Z","shell.execute_reply.started":"2022-07-29T13:28:42.282412Z","shell.execute_reply":"2022-07-29T13:28:42.496697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.500209Z","iopub.execute_input":"2022-07-29T13:28:42.500622Z","iopub.status.idle":"2022-07-29T13:28:42.515277Z","shell.execute_reply.started":"2022-07-29T13:28:42.500583Z","shell.execute_reply":"2022-07-29T13:28:42.514221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for any duplicated data types\ndata.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.516802Z","iopub.execute_input":"2022-07-29T13:28:42.517271Z","iopub.status.idle":"2022-07-29T13:28:42.751108Z","shell.execute_reply.started":"2022-07-29T13:28:42.517238Z","shell.execute_reply":"2022-07-29T13:28:42.749607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete id row\ndata.drop('id', axis=1, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.752902Z","iopub.execute_input":"2022-07-29T13:28:42.753461Z","iopub.status.idle":"2022-07-29T13:28:42.766771Z","shell.execute_reply.started":"2022-07-29T13:28:42.753409Z","shell.execute_reply":"2022-07-29T13:28:42.765012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data types\n\ndata.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.768922Z","iopub.execute_input":"2022-07-29T13:28:42.769329Z","iopub.status.idle":"2022-07-29T13:28:42.783891Z","shell.execute_reply.started":"2022-07-29T13:28:42.769294Z","shell.execute_reply":"2022-07-29T13:28:42.782665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### EDA","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,13))\n\nfor i in range(7):\n    plt.subplot(4,2,i+1)\n    feature_num = i+7\n    sns.countplot(x=data.iloc[:, feature_num])\n    \n    plt.title(f'Feature: 0{feature_num}')\n    plt.xlim([-1, 44])\n    plt.ylim([0, 11000])\n    plt.xticks(np.arange(0,44,2))\n    plt.xlabel('')\n\nfig.suptitle('Discrete feature distributions', size=20)\nfig.tight_layout() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:42.785658Z","iopub.execute_input":"2022-07-29T13:28:42.786430Z","iopub.status.idle":"2022-07-29T13:28:44.534133Z","shell.execute_reply.started":"2022-07-29T13:28:42.786393Z","shell.execute_reply":"2022-07-29T13:28:44.532678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# continuos feature\n\ncont_feats=[f'f_0{i}' for i in range(7)]\ncont_feats=cont_feats + [f'f_{i}' for i in range(14,29)]\n\nfig=plt.figure(figsize=(15,14))\n\nfor i, f in enumerate(cont_feats):\n    plt.subplot(6,4,i+1)\n    feature_num = i+7\n    sns.histplot(x=data[f])\n    \n    plt.title(f'Feature: 0{f}')\n    plt.xlabel('')\n\nfig.suptitle('Continuous feature distributions', size=20)\nfig.tight_layout() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:44.535841Z","iopub.execute_input":"2022-07-29T13:28:44.536348Z","iopub.status.idle":"2022-07-29T13:28:54.436219Z","shell.execute_reply.started":"2022-07-29T13:28:44.536299Z","shell.execute_reply":"2022-07-29T13:28:54.434936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# correlations\n\nplt.figure(figsize=(8,6))\nsns.heatmap(data.corr().abs(), cmap='Greens', vmin=0, vmax=1)\nplt.title('Absolute correlations')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:54.440881Z","iopub.execute_input":"2022-07-29T13:28:54.441740Z","iopub.status.idle":"2022-07-29T13:28:55.268537Z","shell.execute_reply.started":"2022-07-29T13:28:54.441700Z","shell.execute_reply":"2022-07-29T13:28:55.267262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Elbow method\n\nFind the number of cluster using elbow method\n","metadata":{}},{"cell_type":"code","source":"%%time\n\ninertias = []\nfor k in range(1,15):\n    km = KMeans(n_clusters=k)\n    km.fit(data.iloc[:10000,:])\n    inertias.append(km.inertia_)\n\n# Plot inertias\nplt.figure(figsize=(16,6))\nplt.plot(range(1,15), inertias, 'bx-')\nplt.xlabel('Number of clusters, k')\nplt.ylabel('Inertia')\nplt.title('Elbow method')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:55.271747Z","iopub.execute_input":"2022-07-29T13:28:55.272650Z","iopub.status.idle":"2022-07-29T13:29:22.854105Z","shell.execute_reply.started":"2022-07-29T13:28:55.272606Z","shell.execute_reply":"2022-07-29T13:29:22.852744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Lets take cluster value of 7.","metadata":{}},{"cell_type":"markdown","source":"### Modelling","metadata":{}},{"cell_type":"code","source":"scaled_data = pd.DataFrame(StandardScaler().fit_transform(data))\nscaled_data.columns = data.columns\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:29:22.855932Z","iopub.execute_input":"2022-07-29T13:29:22.856306Z","iopub.status.idle":"2022-07-29T13:29:22.919664Z","shell.execute_reply.started":"2022-07-29T13:29:22.856264Z","shell.execute_reply":"2022-07-29T13:29:22.918301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kMeans cluster\n\n# Baseline\nmodel_km = KMeans(n_clusters=7, random_state=0)\npreds_km = model_km.fit_predict(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:48:57.265233Z","iopub.execute_input":"2022-07-29T13:48:57.267151Z","iopub.status.idle":"2022-07-29T13:49:04.240317Z","shell.execute_reply.started":"2022-07-29T13:48:57.267100Z","shell.execute_reply":"2022-07-29T13:49:04.238881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gaussian mixture\n\n# Baseline\nmodel_gmm = GaussianMixture(n_components=7, random_state=0)\npreds_gmm = model_gmm.fit_predict(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:49:04.242266Z","iopub.execute_input":"2022-07-29T13:49:04.242674Z","iopub.status.idle":"2022-07-29T13:49:15.848354Z","shell.execute_reply.started":"2022-07-29T13:49:04.242638Z","shell.execute_reply":"2022-07-29T13:49:15.846838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Baysesian Mixture\n\n# Baseline\nmodel_bgmm = BayesianGaussianMixture(n_components=7, covariance_type='full', max_iter=100, n_init=5, init_params='random', random_state=0)\npreds_bgmm = model_bgmm.fit_predict(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:49:15.857436Z","iopub.execute_input":"2022-07-29T13:49:15.858845Z","iopub.status.idle":"2022-07-29T13:55:12.648899Z","shell.execute_reply.started":"2022-07-29T13:49:15.858761Z","shell.execute_reply":"2022-07-29T13:55:12.647417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualizations","metadata":{}},{"cell_type":"code","source":"# Countplot\nplt.figure(figsize=(10,4))\nsns.countplot(x=preds_bgmm)\nplt.title('Predicted clusters')","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:55:12.651686Z","iopub.execute_input":"2022-07-29T13:55:12.652610Z","iopub.status.idle":"2022-07-29T13:55:12.887594Z","shell.execute_reply.started":"2022-07-29T13:55:12.652557Z","shell.execute_reply":"2022-07-29T13:55:12.886327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### PCA","metadata":{}},{"cell_type":"code","source":"\n# PCA\npca = PCA(n_components=3)\ncomponents = pca.fit_transform(scaled_data)\n\n# 3D scatterplot\nfig = px.scatter_3d(\n    components, x=0, y=1, z=2, color=preds_bgmm, size=0.1*np.ones(len(scaled_data)), opacity = 1,\n    title='PCA plot in 3D',\n    labels={'0': 'PC 1', '1': 'PC 2', '2': 'PC 3'},\n    width=650, height=500\n)\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:57:59.064124Z","iopub.execute_input":"2022-07-29T13:57:59.064596Z","iopub.status.idle":"2022-07-29T13:58:00.739480Z","shell.execute_reply.started":"2022-07-29T13:57:59.064557Z","shell.execute_reply":"2022-07-29T13:58:00.738066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/tabular-playground-series-jul-2022/sample_submission.csv')\nsub['Predicted'] = preds_bgmm\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:58:34.086284Z","iopub.execute_input":"2022-07-29T13:58:34.086739Z","iopub.status.idle":"2022-07-29T13:58:34.210386Z","shell.execute_reply.started":"2022-07-29T13:58:34.086702Z","shell.execute_reply":"2022-07-29T13:58:34.209116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}