{"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":"markdown","source":"* ## **TABULAR PLAYGROUND SERIES 2022**","metadata":{}},{"cell_type":"markdown","source":"### TABLE of CONTENTS:\n\n* [Import Libraries](#sec1)\n* [Import CSV](#sec2)\n* [Basic Information](#sec3)\n* [EDA](#sec4)\n* [Robust Scaler](#sec5)\n* [Standard Scaler](#sec6)\n* [PCA](#sec7)\n* [K Means](#sec8)\n* [GMM](#sec9)","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries <a class=\"anchor\" id=\"sec1\"></a>","metadata":{}},{"cell_type":"code","source":"#importing libraries\nimport warnings\nwarnings.filterwarnings('ignore')\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:16.606966Z","iopub.execute_input":"2022-07-11T12:24:16.608147Z","iopub.status.idle":"2022-07-11T12:24:16.614258Z","shell.execute_reply.started":"2022-07-11T12:24:16.608098Z","shell.execute_reply":"2022-07-11T12:24:16.613172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\npd.set_option('display.expand_frame_repr', False)\npd.set_option('max_colwidth', -1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:16.616716Z","iopub.execute_input":"2022-07-11T12:24:16.617707Z","iopub.status.idle":"2022-07-11T12:24:16.627729Z","shell.execute_reply.started":"2022-07-11T12:24:16.617667Z","shell.execute_reply":"2022-07-11T12:24:16.626348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import CSV <a class=\"anchor\" id=\"sec2\"></a>","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:16.629350Z","iopub.execute_input":"2022-07-11T12:24:16.630091Z","iopub.status.idle":"2022-07-11T12:24:17.344899Z","shell.execute_reply.started":"2022-07-11T12:24:16.630036Z","shell.execute_reply":"2022-07-11T12:24:17.344068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.347477Z","iopub.execute_input":"2022-07-11T12:24:17.348131Z","iopub.status.idle":"2022-07-11T12:24:17.374648Z","shell.execute_reply.started":"2022-07-11T12:24:17.348088Z","shell.execute_reply":"2022-07-11T12:24:17.373623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic Information <a class=\"anchor\" id=\"sec3\"></a>","metadata":{}},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.375891Z","iopub.execute_input":"2022-07-11T12:24:17.376191Z","iopub.status.idle":"2022-07-11T12:24:17.382580Z","shell.execute_reply.started":"2022-07-11T12:24:17.376164Z","shell.execute_reply":"2022-07-11T12:24:17.381508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.383796Z","iopub.execute_input":"2022-07-11T12:24:17.384130Z","iopub.status.idle":"2022-07-11T12:24:17.399726Z","shell.execute_reply.started":"2022-07-11T12:24:17.384101Z","shell.execute_reply":"2022-07-11T12:24:17.398911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.401193Z","iopub.execute_input":"2022-07-11T12:24:17.402034Z","iopub.status.idle":"2022-07-11T12:24:17.601011Z","shell.execute_reply.started":"2022-07-11T12:24:17.401995Z","shell.execute_reply":"2022-07-11T12:24:17.599883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.602742Z","iopub.execute_input":"2022-07-11T12:24:17.603442Z","iopub.status.idle":"2022-07-11T12:24:17.624341Z","shell.execute_reply.started":"2022-07-11T12:24:17.603391Z","shell.execute_reply":"2022-07-11T12:24:17.623538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.625755Z","iopub.execute_input":"2022-07-11T12:24:17.627008Z","iopub.status.idle":"2022-07-11T12:24:17.640742Z","shell.execute_reply.started":"2022-07-11T12:24:17.626966Z","shell.execute_reply":"2022-07-11T12:24:17.639986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['int64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.644020Z","iopub.execute_input":"2022-07-11T12:24:17.644584Z","iopub.status.idle":"2022-07-11T12:24:17.662934Z","shell.execute_reply.started":"2022-07-11T12:24:17.644552Z","shell.execute_reply":"2022-07-11T12:24:17.661898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.select_dtypes(include=['float64']).nunique().sort_values(ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.664246Z","iopub.execute_input":"2022-07-11T12:24:17.664787Z","iopub.status.idle":"2022-07-11T12:24:17.775722Z","shell.execute_reply.started":"2022-07-11T12:24:17.664755Z","shell.execute_reply":"2022-07-11T12:24:17.774532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA <a class=\"anchor\" id=\"sec4\"></a>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(25, 25))\nfor i, col in enumerate(df.select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,4, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram Plots for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:17.777352Z","iopub.execute_input":"2022-07-11T12:24:17.777680Z","iopub.status.idle":"2022-07-11T12:24:26.939764Z","shell.execute_reply.started":"2022-07-11T12:24:17.777651Z","shell.execute_reply":"2022-07-11T12:24:26.938689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 18))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.histplot(data=df, x=col, ax=ax)\nplt.suptitle('Histogram for all categorical variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:26.940941Z","iopub.execute_input":"2022-07-11T12:24:26.941221Z","iopub.status.idle":"2022-07-11T12:24:29.890853Z","shell.execute_reply.started":"2022-07-11T12:24:26.941195Z","shell.execute_reply":"2022-07-11T12:24:29.889807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 18))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['int64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all categorical variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:29.892384Z","iopub.execute_input":"2022-07-11T12:24:29.893142Z","iopub.status.idle":"2022-07-11T12:24:30.738897Z","shell.execute_reply.started":"2022-07-11T12:24:29.893097Z","shell.execute_reply":"2022-07-11T12:24:30.737991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40, 40))\nfor i, col in enumerate(df.iloc[:,1:].select_dtypes(include=['float64']).columns):\n    ax = plt.subplot(11,3, i+1)\n    sns.boxplot(data=df, x=col, ax=ax)\nplt.suptitle('Boxplot for all continuous variables')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:30.742133Z","iopub.execute_input":"2022-07-11T12:24:30.742535Z","iopub.status.idle":"2022-07-11T12:24:33.284611Z","shell.execute_reply.started":"2022-07-11T12:24:30.742504Z","shell.execute_reply":"2022-07-11T12:24:33.283875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hence, the data contains outliers","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(50,50))\nsns.heatmap(df.corr(),annot=True)\nplt.title('Correlation Plot')\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:33.285544Z","iopub.execute_input":"2022-07-11T12:24:33.286106Z","iopub.status.idle":"2022-07-11T12:24:37.585401Z","shell.execute_reply.started":"2022-07-11T12:24:33.286071Z","shell.execute_reply":"2022-07-11T12:24:37.584035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Double click on the above image to get clear picture","metadata":{}},{"cell_type":"markdown","source":"# Robust Scaler <a class=\"anchor\" id=\"sec5\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import RobustScaler\nr=RobustScaler()\ndf_r=r.fit_transform(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:37.586689Z","iopub.execute_input":"2022-07-11T12:24:37.587171Z","iopub.status.idle":"2022-07-11T12:24:37.731162Z","shell.execute_reply.started":"2022-07-11T12:24:37.587135Z","shell.execute_reply":"2022-07-11T12:24:37.730084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_r=pd.DataFrame(df_r)\ndf_r.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:37.732849Z","iopub.execute_input":"2022-07-11T12:24:37.733276Z","iopub.status.idle":"2022-07-11T12:24:37.763235Z","shell.execute_reply.started":"2022-07-11T12:24:37.733235Z","shell.execute_reply":"2022-07-11T12:24:37.762197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,30):\n    sns.boxplot(df_r.iloc[:,i])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:37.764645Z","iopub.execute_input":"2022-07-11T12:24:37.765456Z","iopub.status.idle":"2022-07-11T12:24:42.779328Z","shell.execute_reply.started":"2022-07-11T12:24:37.765413Z","shell.execute_reply":"2022-07-11T12:24:42.778152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The data has still outliers.","metadata":{}},{"cell_type":"markdown","source":"# Standard Scaler <a class=\"anchor\" id=\"sec6\"></a>","metadata":{}},{"cell_type":"code","source":"#\n# Scale the dataset; \n#\nfrom sklearn.preprocessing import StandardScaler\nsc = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.780891Z","iopub.execute_input":"2022-07-11T12:24:42.783184Z","iopub.status.idle":"2022-07-11T12:24:42.787101Z","shell.execute_reply.started":"2022-07-11T12:24:42.783127Z","shell.execute_reply":"2022-07-11T12:24:42.786411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.fit(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.788279Z","iopub.execute_input":"2022-07-11T12:24:42.788936Z","iopub.status.idle":"2022-07-11T12:24:42.836850Z","shell.execute_reply.started":"2022-07-11T12:24:42.788906Z","shell.execute_reply":"2022-07-11T12:24:42.835603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_std = sc.transform(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.838025Z","iopub.execute_input":"2022-07-11T12:24:42.838345Z","iopub.status.idle":"2022-07-11T12:24:42.860349Z","shell.execute_reply.started":"2022-07-11T12:24:42.838317Z","shell.execute_reply":"2022-07-11T12:24:42.859316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA <a class=\"anchor\" id=\"sec7\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.862664Z","iopub.execute_input":"2022-07-11T12:24:42.863259Z","iopub.status.idle":"2022-07-11T12:24:42.867283Z","shell.execute_reply.started":"2022-07-11T12:24:42.863229Z","shell.execute_reply":"2022-07-11T12:24:42.866455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_pca = pca.fit_transform(df_std)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.868579Z","iopub.execute_input":"2022-07-11T12:24:42.868956Z","iopub.status.idle":"2022-07-11T12:24:42.993602Z","shell.execute_reply.started":"2022-07-11T12:24:42.868924Z","shell.execute_reply":"2022-07-11T12:24:42.992334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Determine explained variance using explained_variance_ration_ attribute\n#\nexp_var_pca = pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:42.995847Z","iopub.execute_input":"2022-07-11T12:24:42.996756Z","iopub.status.idle":"2022-07-11T12:24:43.002550Z","shell.execute_reply.started":"2022-07-11T12:24:42.996706Z","shell.execute_reply":"2022-07-11T12:24:43.001143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_var_pca","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.004657Z","iopub.execute_input":"2022-07-11T12:24:43.005322Z","iopub.status.idle":"2022-07-11T12:24:43.017348Z","shell.execute_reply.started":"2022-07-11T12:24:43.005269Z","shell.execute_reply":"2022-07-11T12:24:43.016146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cumulative sum of eigenvalues; This will be used to create step plot\n# for visualizing the variance explained by each principal component.\n#\ncum_sum_eigenvalues = np.cumsum(exp_var_pca)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.019439Z","iopub.execute_input":"2022-07-11T12:24:43.020439Z","iopub.status.idle":"2022-07-11T12:24:43.029170Z","shell.execute_reply.started":"2022-07-11T12:24:43.020274Z","shell.execute_reply":"2022-07-11T12:24:43.027798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cum_sum_eigenvalues","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.039317Z","iopub.execute_input":"2022-07-11T12:24:43.040409Z","iopub.status.idle":"2022-07-11T12:24:43.049237Z","shell.execute_reply.started":"2022-07-11T12:24:43.040348Z","shell.execute_reply":"2022-07-11T12:24:43.048096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(range(0,len(exp_var_pca)), exp_var_pca, alpha=0.5, align='center', label='Individual explained variance')\nplt.step(range(0,len(cum_sum_eigenvalues)), cum_sum_eigenvalues, where='mid',label='Cumulative explained variance')\nplt.ylabel('Explained variance ratio')\nplt.xlabel('Principal component index')\nplt.legend(loc='best')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.051301Z","iopub.execute_input":"2022-07-11T12:24:43.052342Z","iopub.status.idle":"2022-07-11T12:24:43.393782Z","shell.execute_reply.started":"2022-07-11T12:24:43.052280Z","shell.execute_reply":"2022-07-11T12:24:43.392651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=0.95,random_state=1000)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.397144Z","iopub.execute_input":"2022-07-11T12:24:43.397486Z","iopub.status.idle":"2022-07-11T12:24:43.402888Z","shell.execute_reply.started":"2022-07-11T12:24:43.397456Z","shell.execute_reply":"2022-07-11T12:24:43.401519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We have selected PCA explaining 95% of variance.","metadata":{}},{"cell_type":"code","source":"df_pca_=pca.fit_transform(df_std)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.404202Z","iopub.execute_input":"2022-07-11T12:24:43.404499Z","iopub.status.idle":"2022-07-11T12:24:43.531595Z","shell.execute_reply.started":"2022-07-11T12:24:43.404474Z","shell.execute_reply":"2022-07-11T12:24:43.530321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Determine explained variance using explained_variance_ration_ attribute\n#\nexp_var_pca = pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.533680Z","iopub.execute_input":"2022-07-11T12:24:43.534568Z","iopub.status.idle":"2022-07-11T12:24:43.539874Z","shell.execute_reply.started":"2022-07-11T12:24:43.534517Z","shell.execute_reply":"2022-07-11T12:24:43.538790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_var_pca ","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.541774Z","iopub.execute_input":"2022-07-11T12:24:43.542617Z","iopub.status.idle":"2022-07-11T12:24:43.554275Z","shell.execute_reply.started":"2022-07-11T12:24:43.542570Z","shell.execute_reply":"2022-07-11T12:24:43.553094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cumulative sum of eigenvalues; This will be used to create step plot\n# for visualizing the variance explained by each principal component.\n#\ncum_sum_eigenvalues = np.cumsum(exp_var_pca)\ncum_sum_eigenvalues","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.556288Z","iopub.execute_input":"2022-07-11T12:24:43.557301Z","iopub.status.idle":"2022-07-11T12:24:43.569276Z","shell.execute_reply.started":"2022-07-11T12:24:43.557246Z","shell.execute_reply":"2022-07-11T12:24:43.568093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.bar(range(0,len(exp_var_pca)), exp_var_pca, alpha=0.5, align='center', label='Individual explained variance')\nplt.step(range(0,len(cum_sum_eigenvalues)), cum_sum_eigenvalues, where='mid',label='Cumulative explained variance')\nplt.ylabel('Explained variance ratio')\nplt.xlabel('Principal component index')\nplt.legend(loc='best')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.571527Z","iopub.execute_input":"2022-07-11T12:24:43.573364Z","iopub.status.idle":"2022-07-11T12:24:43.937347Z","shell.execute_reply.started":"2022-07-11T12:24:43.573315Z","shell.execute_reply":"2022-07-11T12:24:43.936336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new=pd.DataFrame(df_pca_)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.939056Z","iopub.execute_input":"2022-07-11T12:24:43.939715Z","iopub.status.idle":"2022-07-11T12:24:43.948650Z","shell.execute_reply.started":"2022-07-11T12:24:43.939676Z","shell.execute_reply":"2022-07-11T12:24:43.947608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.950567Z","iopub.execute_input":"2022-07-11T12:24:43.950998Z","iopub.status.idle":"2022-07-11T12:24:43.980353Z","shell.execute_reply.started":"2022-07-11T12:24:43.950957Z","shell.execute_reply":"2022-07-11T12:24:43.979435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# K Means <a class=\"anchor\" id=\"sec8\"></a>","metadata":{}},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.cluster import KMeans","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.981861Z","iopub.execute_input":"2022-07-11T12:24:43.982275Z","iopub.status.idle":"2022-07-11T12:24:43.988344Z","shell.execute_reply.started":"2022-07-11T12:24:43.982224Z","shell.execute_reply":"2022-07-11T12:24:43.987053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Elbow Method to determine the number of clusters to be formed:')\nElbow_M = KElbowVisualizer(KMeans(random_state=23), k=(4,12))\nElbow_M.fit(df_new)\nElbow_M.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:24:43.990038Z","iopub.execute_input":"2022-07-11T12:24:43.990986Z","iopub.status.idle":"2022-07-11T12:25:41.932062Z","shell.execute_reply.started":"2022-07-11T12:24:43.990915Z","shell.execute_reply":"2022-07-11T12:25:41.930982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(7)\nkmeans.fit(df_new)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:41.933641Z","iopub.execute_input":"2022-07-11T12:25:41.934731Z","iopub.status.idle":"2022-07-11T12:25:48.382295Z","shell.execute_reply.started":"2022-07-11T12:25:41.934681Z","shell.execute_reply":"2022-07-11T12:25:48.381142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"identified_clusters = kmeans.fit_predict(df_new)\nidentified_clusters","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:48.383960Z","iopub.execute_input":"2022-07-11T12:25:48.384968Z","iopub.status.idle":"2022-07-11T12:25:54.976425Z","shell.execute_reply.started":"2022-07-11T12:25:48.384920Z","shell.execute_reply":"2022-07-11T12:25:54.975549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Predicted']=identified_clusters","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:54.977774Z","iopub.execute_input":"2022-07-11T12:25:54.978940Z","iopub.status.idle":"2022-07-11T12:25:54.984524Z","shell.execute_reply.started":"2022-07-11T12:25:54.978898Z","shell.execute_reply":"2022-07-11T12:25:54.983773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:,['id','Predicted']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:54.986021Z","iopub.execute_input":"2022-07-11T12:25:54.987036Z","iopub.status.idle":"2022-07-11T12:25:55.003624Z","shell.execute_reply.started":"2022-07-11T12:25:54.986991Z","shell.execute_reply":"2022-07-11T12:25:55.002523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=df.loc[:,['id','Predicted']]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:55.004847Z","iopub.execute_input":"2022-07-11T12:25:55.005140Z","iopub.status.idle":"2022-07-11T12:25:55.016467Z","shell.execute_reply.started":"2022-07-11T12:25:55.005114Z","shell.execute_reply":"2022-07-11T12:25:55.015668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:55.017731Z","iopub.execute_input":"2022-07-11T12:25:55.018039Z","iopub.status.idle":"2022-07-11T12:25:55.032454Z","shell.execute_reply.started":"2022-07-11T12:25:55.018004Z","shell.execute_reply":"2022-07-11T12:25:55.031608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('sub_july.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:25:55.033792Z","iopub.execute_input":"2022-07-11T12:25:55.034201Z","iopub.status.idle":"2022-07-11T12:25:55.133862Z","shell.execute_reply.started":"2022-07-11T12:25:55.034162Z","shell.execute_reply":"2022-07-11T12:25:55.132702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GMM <a class=\"anchor\" id=\"sec9\"></a>","metadata":{}},{"cell_type":"code","source":"from sklearn.mixture import GaussianMixture","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:40:43.274236Z","iopub.execute_input":"2022-07-11T12:40:43.274615Z","iopub.status.idle":"2022-07-11T12:40:43.285167Z","shell.execute_reply.started":"2022-07-11T12:40:43.274581Z","shell.execute_reply":"2022-07-11T12:40:43.284115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_components = np.arange(1, 21)\nmodels = [GaussianMixture(n, covariance_type='full', random_state=0).fit(df_new) for n in n_components]\nplt.plot(n_components, [m.bic(df_new) for m in models], label='BIC')\nplt.plot(n_components, [m.aic(df_new) for m in models], label='AIC')\nplt.legend(loc='best')\nplt.xlabel('n_components')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T12:40:49.621872Z","iopub.execute_input":"2022-07-11T12:40:49.622495Z","iopub.status.idle":"2022-07-11T12:53:32.128481Z","shell.execute_reply.started":"2022-07-11T12:40:49.622463Z","shell.execute_reply":"2022-07-11T12:53:32.127211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GM = GaussianMixture(n_components=6, covariance_type='full', random_state=1)\npred = GM.fit_predict(df_new)\ndf_new[\"Clusters\"] = pred\ndf_new['Clusters'].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:02:52.584362Z","iopub.execute_input":"2022-07-11T13:02:52.584701Z","iopub.status.idle":"2022-07-11T13:02:56.149416Z","shell.execute_reply.started":"2022-07-11T13:02:52.584672Z","shell.execute_reply":"2022-07-11T13:02:56.148180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:03:01.723850Z","iopub.execute_input":"2022-07-11T13:03:01.725070Z","iopub.status.idle":"2022-07-11T13:03:01.756131Z","shell.execute_reply.started":"2022-07-11T13:03:01.725013Z","shell.execute_reply":"2022-07-11T13:03:01.755100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns =[\"Predicted\"], inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:08:04.042725Z","iopub.execute_input":"2022-07-11T13:08:04.043111Z","iopub.status.idle":"2022-07-11T13:08:04.054721Z","shell.execute_reply.started":"2022-07-11T13:08:04.043081Z","shell.execute_reply":"2022-07-11T13:08:04.053698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:08:16.202566Z","iopub.execute_input":"2022-07-11T13:08:16.202964Z","iopub.status.idle":"2022-07-11T13:08:16.209677Z","shell.execute_reply.started":"2022-07-11T13:08:16.202931Z","shell.execute_reply":"2022-07-11T13:08:16.208580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Predicted']=pred","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:09:22.628407Z","iopub.execute_input":"2022-07-11T13:09:22.629329Z","iopub.status.idle":"2022-07-11T13:09:22.634650Z","shell.execute_reply.started":"2022-07-11T13:09:22.629289Z","shell.execute_reply":"2022-07-11T13:09:22.633302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2=df.loc[:,['id','Predicted']]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:09:55.076180Z","iopub.execute_input":"2022-07-11T13:09:55.076606Z","iopub.status.idle":"2022-07-11T13:09:55.085469Z","shell.execute_reply.started":"2022-07-11T13:09:55.076569Z","shell.execute_reply":"2022-07-11T13:09:55.084629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:10:05.935865Z","iopub.execute_input":"2022-07-11T13:10:05.936336Z","iopub.status.idle":"2022-07-11T13:10:05.943371Z","shell.execute_reply.started":"2022-07-11T13:10:05.936290Z","shell.execute_reply":"2022-07-11T13:10:05.942579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2.to_csv('sub2_july.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T13:10:48.676040Z","iopub.execute_input":"2022-07-11T13:10:48.676410Z","iopub.status.idle":"2022-07-11T13:10:48.775120Z","shell.execute_reply.started":"2022-07-11T13:10:48.676379Z","shell.execute_reply":"2022-07-11T13:10:48.774016Z"},"trusted":true},"execution_count":null,"outputs":[]}]}