{"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 seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T02:51:11.436147Z","iopub.execute_input":"2022-07-25T02:51:11.436618Z","iopub.status.idle":"2022-07-25T02:51:12.690734Z","shell.execute_reply.started":"2022-07-25T02:51:11.436525Z","shell.execute_reply":"2022-07-25T02:51:12.689426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T02:51:14.746576Z","iopub.execute_input":"2022-07-25T02:51:14.747166Z","iopub.status.idle":"2022-07-25T02:51:16.326208Z","shell.execute_reply.started":"2022-07-25T02:51:14.747106Z","shell.execute_reply":"2022-07-25T02:51:16.325265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T02:51:20.041489Z","iopub.execute_input":"2022-07-25T02:51:20.041915Z","iopub.status.idle":"2022-07-25T02:51:20.051748Z","shell.execute_reply.started":"2022-07-25T02:51:20.041878Z","shell.execute_reply":"2022-07-25T02:51:20.050345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T02:51:22.224252Z","iopub.execute_input":"2022-07-25T02:51:22.224657Z","iopub.status.idle":"2022-07-25T02:51:22.239391Z","shell.execute_reply.started":"2022-07-25T02:51:22.224625Z","shell.execute_reply":"2022-07-25T02:51:22.238208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T02:51:24.445981Z","iopub.execute_input":"2022-07-25T02:51:24.446641Z","iopub.status.idle":"2022-07-25T02:51:24.481181Z","shell.execute_reply.started":"2022-07-25T02:51:24.446581Z","shell.execute_reply":"2022-07-25T02:51:24.479896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (25 , 25))\nsns.heatmap(data.corr().round(2) , cmap = 'coolwarm' , annot = True , vmin = -1 , vmax = 1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T02:51:27.975875Z","iopub.execute_input":"2022-07-25T02:51:27.976305Z","iopub.status.idle":"2022-07-25T02:51:31.876247Z","shell.execute_reply.started":"2022-07-25T02:51:27.976269Z","shell.execute_reply":"2022-07-25T02:51:31.873141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = data.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:14:12.529752Z","iopub.execute_input":"2022-07-25T03:14:12.531549Z","iopub.status.idle":"2022-07-25T03:14:12.547012Z","shell.execute_reply.started":"2022-07-25T03:14:12.531421Z","shell.execute_reply":"2022-07-25T03:14:12.545293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler , RobustScaler , PowerTransformer\nsc = StandardScaler()\ndata = sc.fit_transform(data)\npt = PowerTransformer()\ndata = pt.fit_transform(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:14:13.811094Z","iopub.execute_input":"2022-07-25T03:14:13.812519Z","iopub.status.idle":"2022-07-25T03:14:18.080265Z","shell.execute_reply.started":"2022-07-25T03:14:13.812469Z","shell.execute_reply":"2022-07-25T03:14:18.079233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.DataFrame(data , columns = col)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:14:19.240745Z","iopub.execute_input":"2022-07-25T03:14:19.241908Z","iopub.status.idle":"2022-07-25T03:14:19.280916Z","shell.execute_reply.started":"2022-07-25T03:14:19.241847Z","shell.execute_reply":"2022-07-25T03:14:19.278565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/competitions/tabular-playground-series-jul-2022/discussion/334808","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:45:47.247931Z","iopub.execute_input":"2022-07-25T03:45:47.248418Z","iopub.status.idle":"2022-07-25T03:45:47.254556Z","shell.execute_reply.started":"2022-07-25T03:45:47.248363Z","shell.execute_reply":"2022-07-25T03:45:47.253107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfrom sklearn.cluster import KMeans\n#implementing Elbow method\ndistortions = []\nfor i in range(1, 11):\n    kme = KMeans(n_clusters = i , max_iter = 100 , n_init = 10\n               , tol = 1e-04 , random_state = 0 , init = 'random')\n    kme.fit(data)\n    distortions.append(kme.inertia_)\nplt.plot(range(1,11) , distortions , marker = '+')\nplt.xlabel('Number of Clusters')\nplt.ylabel('Distortions')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:45:01.353289Z","iopub.execute_input":"2022-07-25T03:45:01.353760Z","iopub.status.idle":"2022-07-25T03:45:37.159479Z","shell.execute_reply.started":"2022-07-25T03:45:01.353725Z","shell.execute_reply":"2022-07-25T03:45:37.157981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"km = KMeans(n_clusters = 7 , max_iter = 100 ,n_init =10 , tol = 1e-04 , random_state = 0\n           , init = 'random')\ny = km.fit_predict(data)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:46:02.947317Z","iopub.execute_input":"2022-07-25T03:46:02.948513Z","iopub.status.idle":"2022-07-25T03:46:06.645596Z","shell.execute_reply.started":"2022-07-25T03:46:02.948465Z","shell.execute_reply":"2022-07-25T03:46:06.644310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/tabular-playground-series-jul-2022/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:29:47.374921Z","iopub.execute_input":"2022-07-25T04:29:47.375352Z","iopub.status.idle":"2022-07-25T04:29:47.444634Z","shell.execute_reply.started":"2022-07-25T04:29:47.375315Z","shell.execute_reply":"2022-07-25T04:29:47.443563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub.Predicted = y\n# sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:46:15.681911Z","iopub.execute_input":"2022-07-25T03:46:15.682782Z","iopub.status.idle":"2022-07-25T03:46:15.687769Z","shell.execute_reply.started":"2022-07-25T03:46:15.682730Z","shell.execute_reply":"2022-07-25T03:46:15.686605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub.Predicted.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:46:18.871548Z","iopub.execute_input":"2022-07-25T03:46:18.871949Z","iopub.status.idle":"2022-07-25T03:46:18.876908Z","shell.execute_reply.started":"2022-07-25T03:46:18.871917Z","shell.execute_reply":"2022-07-25T03:46:18.875746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sub.to_csv('submission.csv' , index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:46:21.866735Z","iopub.execute_input":"2022-07-25T03:46:21.867172Z","iopub.status.idle":"2022-07-25T03:46:21.872778Z","shell.execute_reply.started":"2022-07-25T03:46:21.867134Z","shell.execute_reply":"2022-07-25T03:46:21.871472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<!-- Implementing BayesianGMMClassifier -->","metadata":{}},{"cell_type":"markdown","source":"# Implementing BayesianGMM mixture","metadata":{}},{"cell_type":"code","source":"useful_cols = [\n    \"f_07\",\n    \"f_08\",\n    \"f_09\",\n    \"f_10\",\n    \"f_11\",\n    \"f_12\",\n    \"f_13\",\n    \"f_22\",\n    \"f_23\",\n    \"f_24\",\n    \"f_25\",\n    \"f_26\",\n    \"f_27\",\n    \"f_28\",\n]","metadata":{"execution":{"iopub.status.busy":"2022-07-25T03:49:42.126952Z","iopub.execute_input":"2022-07-25T03:49:42.127372Z","iopub.status.idle":"2022-07-25T03:49:42.133598Z","shell.execute_reply.started":"2022-07-25T03:49:42.127338Z","shell.execute_reply":"2022-07-25T03:49:42.132259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install scikit-lego","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:05:53.775473Z","iopub.execute_input":"2022-07-25T04:05:53.775905Z","iopub.status.idle":"2022-07-25T04:06:09.458362Z","shell.execute_reply.started":"2022-07-25T04:05:53.775873Z","shell.execute_reply":"2022-07-25T04:06:09.457130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import GaussianMixture, BayesianGaussianMixture\nfrom sklego.mixture import BayesianGMMClassifier, GMMClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:06:21.600706Z","iopub.execute_input":"2022-07-25T04:06:21.601504Z","iopub.status.idle":"2022-07-25T04:06:21.615556Z","shell.execute_reply.started":"2022-07-25T04:06:21.601458Z","shell.execute_reply":"2022-07-25T04:06:21.614495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgm = BayesianGaussianMixture(\n    n_components=7,\n    max_iter=300,\n    n_init=10,\n    random_state=2,\n    verbose_interval=100,\n)\n\nbgm_labels = bgm.fit_predict(data[useful_cols])\nbgm_proba = bgm.predict_proba(data[useful_cols])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:07:15.074043Z","iopub.execute_input":"2022-07-25T04:07:15.074454Z","iopub.status.idle":"2022-07-25T04:14:54.096971Z","shell.execute_reply.started":"2022-07-25T04:07:15.074420Z","shell.execute_reply":"2022-07-25T04:14:54.095461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgm_labels","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:29:39.069762Z","iopub.execute_input":"2022-07-25T04:29:39.070153Z","iopub.status.idle":"2022-07-25T04:29:39.079208Z","shell.execute_reply.started":"2022-07-25T04:29:39.070122Z","shell.execute_reply":"2022-07-25T04:29:39.077880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.Predicted = bgm_labels","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:30:11.838844Z","iopub.execute_input":"2022-07-25T04:30:11.839279Z","iopub.status.idle":"2022-07-25T04:30:11.846680Z","shell.execute_reply.started":"2022-07-25T04:30:11.839242Z","shell.execute_reply":"2022-07-25T04:30:11.845349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv' , index = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:30:26.675489Z","iopub.execute_input":"2022-07-25T04:30:26.675893Z","iopub.status.idle":"2022-07-25T04:30:26.846410Z","shell.execute_reply.started":"2022-07-25T04:30:26.675860Z","shell.execute_reply":"2022-07-25T04:30:26.844857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}