{"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 os\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.decomposition import PCA\nfrom sklearn.mixture import GaussianMixture,BayesianGaussianMixture\nfrom sklearn.metrics import silhouette_score\nfrom sklearn.preprocessing import MaxAbsScaler,PowerTransformer, RobustScaler\n\nfrom collections import Counter\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-09T14:48:59.468608Z","iopub.execute_input":"2022-07-09T14:48:59.469017Z","iopub.status.idle":"2022-07-09T14:49:00.527014Z","shell.execute_reply.started":"2022-07-09T14:48:59.468985Z","shell.execute_reply":"2022-07-09T14:49:00.525703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:00.529150Z","iopub.execute_input":"2022-07-09T14:49:00.529603Z","iopub.status.idle":"2022-07-09T14:49:00.538672Z","shell.execute_reply.started":"2022-07-09T14:49:00.529567Z","shell.execute_reply":"2022-07-09T14:49:00.537405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/tabular-playground-series-jul-2022/data.csv\",index_col=\"id\")\nsubmission = pd.read_csv(\"/kaggle/input/tabular-playground-series-jul-2022/sample_submission.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:00.540464Z","iopub.execute_input":"2022-07-09T14:49:00.540908Z","iopub.status.idle":"2022-07-09T14:49:02.220996Z","shell.execute_reply.started":"2022-07-09T14:49:00.540867Z","shell.execute_reply":"2022-07-09T14:49:02.219768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:02.224340Z","iopub.execute_input":"2022-07-09T14:49:02.224749Z","iopub.status.idle":"2022-07-09T14:49:02.271594Z","shell.execute_reply.started":"2022-07-09T14:49:02.224712Z","shell.execute_reply":"2022-07-09T14:49:02.270150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape, data.size","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:02.273383Z","iopub.execute_input":"2022-07-09T14:49:02.274196Z","iopub.status.idle":"2022-07-09T14:49:02.283110Z","shell.execute_reply.started":"2022-07-09T14:49:02.274143Z","shell.execute_reply":"2022-07-09T14:49:02.281825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:02.285042Z","iopub.execute_input":"2022-07-09T14:49:02.285632Z","iopub.status.idle":"2022-07-09T14:49:02.318049Z","shell.execute_reply.started":"2022-07-09T14:49:02.285588Z","shell.execute_reply":"2022-07-09T14:49:02.317227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe(include=None).T","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:02.319582Z","iopub.execute_input":"2022-07-09T14:49:02.320221Z","iopub.status.idle":"2022-07-09T14:49:02.544431Z","shell.execute_reply.started":"2022-07-09T14:49:02.320182Z","shell.execute_reply":"2022-07-09T14:49:02.543581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = list(data.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:02.546043Z","iopub.execute_input":"2022-07-09T14:49:02.546427Z","iopub.status.idle":"2022-07-09T14:49:02.550933Z","shell.execute_reply.started":"2022-07-09T14:49:02.546389Z","shell.execute_reply":"2022-07-09T14:49:02.550027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#colormap = plt.cm.RdBu\nplt.figure(figsize=(18,12))\nsns.heatmap(data.corr(),annot=True,fmt=\".2f\", cmap='Blues')\nplt.show()\n\n\n#plt.title('Pearson Correlation of Features', y=1.05, size=15)\n#sns.heatmap(train.astype(float).corr(),)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:57:50.007863Z","iopub.execute_input":"2022-07-09T15:57:50.008630Z","iopub.status.idle":"2022-07-09T15:57:53.628986Z","shell.execute_reply.started":"2022-07-09T15:57:50.008591Z","shell.execute_reply":"2022-07-09T15:57:53.628246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Abs_scaler = MaxAbsScaler().fit(data)\ndata_standard = Abs_scaler.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:03.503958Z","iopub.execute_input":"2022-07-09T14:49:03.504607Z","iopub.status.idle":"2022-07-09T14:49:03.611898Z","shell.execute_reply.started":"2022-07-09T14:49:03.504567Z","shell.execute_reply":"2022-07-09T14:49:03.610673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"power_transformer = PowerTransformer().fit(data_standard)\ndata_transformed = power_transformer.transform(data_standard)\ndata_transformed = pd.DataFrame(data_transformed,columns = columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:03.613560Z","iopub.execute_input":"2022-07-09T14:49:03.613956Z","iopub.status.idle":"2022-07-09T14:49:07.728266Z","shell.execute_reply.started":"2022-07-09T14:49:03.613920Z","shell.execute_reply":"2022-07-09T14:49:07.726824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(22,32))\nfor index,col in enumerate(columns):\n    plt.subplot(8,4,index+1)\n    sns.kdeplot(x = data[col],shade=\"fill\",color=\"blue\")\n    sns.kdeplot(x = data_transformed[col],shade=\"fill\",color=\"green\")   \nplt.suptitle(\"Features distribution original and scaled\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:07.730256Z","iopub.execute_input":"2022-07-09T14:49:07.730837Z","iopub.status.idle":"2022-07-09T14:49:37.991808Z","shell.execute_reply.started":"2022-07-09T14:49:07.730789Z","shell.execute_reply":"2022-07-09T14:49:37.990839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Robust_scaler = RobustScaler().fit(data)\ndata_standard_r = Robust_scaler.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:37.993021Z","iopub.execute_input":"2022-07-09T14:49:37.993901Z","iopub.status.idle":"2022-07-09T14:49:38.293307Z","shell.execute_reply.started":"2022-07-09T14:49:37.993860Z","shell.execute_reply":"2022-07-09T14:49:38.292039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"power_transformer_r = PowerTransformer().fit(data_standard_r)\ndata_transformed_r = power_transformer_r.transform(data_standard_r)\ndata_transformed_r = pd.DataFrame(data_transformed_r,columns = columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:38.294878Z","iopub.execute_input":"2022-07-09T14:49:38.295269Z","iopub.status.idle":"2022-07-09T14:49:42.625779Z","shell.execute_reply.started":"2022-07-09T14:49:38.295235Z","shell.execute_reply":"2022-07-09T14:49:42.624649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,28))\nfor index,col in enumerate(columns):\n    plt.subplot(8,4,index+1)\n    sns.kdeplot(x = data_transformed[col],shade=\"fill\",color=\"blue\")   \n    sns.kdeplot(x = data_transformed_r[col]) \nplt.suptitle(\"Features distribution Robust vs MaxAbs Scalers\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:49:42.627123Z","iopub.execute_input":"2022-07-09T14:49:42.627601Z","iopub.status.idle":"2022-07-09T14:50:12.140810Z","shell.execute_reply.started":"2022-07-09T14:49:42.627537Z","shell.execute_reply":"2022-07-09T14:50:12.139599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \ngmm_aic = []\ngmm_bic = []\nfor k in tqdm(range(5, 20)):\n    gmm = GaussianMixture(n_components=k,random_state=1,n_init=3).fit(data_transformed)\n    labels_gmm = gmm.predict(data_transformed)\n    \n   \n    gmm_bic.append(gmm.bic(data_transformed))\n    gmm_aic.append(gmm.aic(data_transformed))\n\n \ngmm_bic_gradient = np.gradient(gmm_bic)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T14:50:12.142597Z","iopub.execute_input":"2022-07-09T14:50:12.142971Z","iopub.status.idle":"2022-07-09T15:33:20.087053Z","shell.execute_reply.started":"2022-07-09T14:50:12.142937Z","shell.execute_reply":"2022-07-09T15:33:20.086219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(22,16))\n\nplt.subplot(121)\nplt.plot(range(5,20),gmm_bic,label = \"bic\")\nplt.plot(range(5,20),gmm_aic,label = \"aic\")\nplt.xlabel(\"Cluster Size\")\nplt.ylabel(\"values\")\nplt.legend()\nplt.title(\"GMM AIC & BIC values\")\n\nplt.subplot(122)\nplt.plot(range(5,20),gmm_bic_gradient)\nplt.xlabel(\"Cluster Size\")\nplt.ylabel(\"Gradient BIC value\")\nplt.title(\"Gradient of GMM BIC values\")\n\nplt.subplots_adjust(wspace=0.3, hspace=0.3)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:33:20.088346Z","iopub.execute_input":"2022-07-09T15:33:20.088804Z","iopub.status.idle":"2022-07-09T15:33:20.486416Z","shell.execute_reply.started":"2022-07-09T15:33:20.088772Z","shell.execute_reply":"2022-07-09T15:33:20.485587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = GaussianMixture(n_components=7,random_state=1,n_init=3).fit(data_transformed)\nlabels = model.predict(data_transformed)\nCounter(labels)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:33:20.487566Z","iopub.execute_input":"2022-07-09T15:33:20.488016Z","iopub.status.idle":"2022-07-09T15:34:07.503535Z","shell.execute_reply.started":"2022-07-09T15:33:20.487987Z","shell.execute_reply":"2022-07-09T15:34:07.502349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_b = BayesianGaussianMixture(n_components=7,random_state=1,n_init=3).fit(data_transformed)\nlabels_b = model_b.predict(data_transformed)\nCounter(labels_b)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:34:07.505447Z","iopub.execute_input":"2022-07-09T15:34:07.506438Z","iopub.status.idle":"2022-07-09T15:37:06.689796Z","shell.execute_reply.started":"2022-07-09T15:34:07.506386Z","shell.execute_reply":"2022-07-09T15:37:06.688679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(2).fit_transform(data_transformed)\n\nplt.figure(figsize=(12,12))\nsns.scatterplot(x=pca[:,0],y=pca[:,1],hue=labels_b,palette='tab10')\nplt.title(\"Clusters\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:37:06.691549Z","iopub.execute_input":"2022-07-09T15:37:06.692209Z","iopub.status.idle":"2022-07-09T15:37:10.481865Z","shell.execute_reply.started":"2022-07-09T15:37:06.692165Z","shell.execute_reply":"2022-07-09T15:37:10.480711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"Predicted\"] = labels_b\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-09T15:37:10.483422Z","iopub.execute_input":"2022-07-09T15:37:10.483762Z","iopub.status.idle":"2022-07-09T15:37:10.650075Z","shell.execute_reply.started":"2022-07-09T15:37:10.483729Z","shell.execute_reply":"2022-07-09T15:37:10.648914Z"},"trusted":true},"execution_count":null,"outputs":[]}]}