{"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 pandas as pd\nimport seaborn as sb\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.cluster import KMeans\nfrom sklearn.mixture import BayesianGaussianMixture\nfrom sklearn.preprocessing import StandardScaler, PowerTransformer","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:03.603255Z","iopub.execute_input":"2022-07-18T06:08:03.603667Z","iopub.status.idle":"2022-07-18T06:08:03.610403Z","shell.execute_reply.started":"2022-07-18T06:08:03.603631Z","shell.execute_reply":"2022-07-18T06:08:03.608733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')\ndf.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:03.633423Z","iopub.execute_input":"2022-07-18T06:08:03.633892Z","iopub.status.idle":"2022-07-18T06:08:04.483095Z","shell.execute_reply.started":"2022-07-18T06:08:03.633860Z","shell.execute_reply":"2022-07-18T06:08:04.481952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('id', axis = 1)\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:04.485747Z","iopub.execute_input":"2022-07-18T06:08:04.486185Z","iopub.status.idle":"2022-07-18T06:08:04.526147Z","shell.execute_reply.started":"2022-07-18T06:08:04.486143Z","shell.execute_reply":"2022-07-18T06:08:04.524704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:04.527788Z","iopub.execute_input":"2022-07-18T06:08:04.528292Z","iopub.status.idle":"2022-07-18T06:08:04.542327Z","shell.execute_reply.started":"2022-07-18T06:08:04.528262Z","shell.execute_reply":"2022-07-18T06:08:04.541484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:04.544668Z","iopub.execute_input":"2022-07-18T06:08:04.544976Z","iopub.status.idle":"2022-07-18T06:08:04.753639Z","shell.execute_reply.started":"2022-07-18T06:08:04.544948Z","shell.execute_reply":"2022-07-18T06:08:04.752140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:04.755717Z","iopub.execute_input":"2022-07-18T06:08:04.756166Z","iopub.status.idle":"2022-07-18T06:08:04.990208Z","shell.execute_reply.started":"2022-07-18T06:08:04.756125Z","shell.execute_reply":"2022-07-18T06:08:04.989289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(df['f_07'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:04.991530Z","iopub.execute_input":"2022-07-18T06:08:04.992235Z","iopub.status.idle":"2022-07-18T06:08:05.231526Z","shell.execute_reply.started":"2022-07-18T06:08:04.992199Z","shell.execute_reply":"2022-07-18T06:08:05.230525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = list(df.columns)\n#plt.hist()\nfor i in range(len(x)):\n    \n    plt.hist(df[x[i]])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:05.233042Z","iopub.execute_input":"2022-07-18T06:08:05.233785Z","iopub.status.idle":"2022-07-18T06:08:10.931249Z","shell.execute_reply.started":"2022-07-18T06:08:05.233740Z","shell.execute_reply":"2022-07-18T06:08:10.930009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scale = StandardScaler()\ndf = pd.DataFrame(scale.fit_transform(df), columns = x)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:10.934616Z","iopub.execute_input":"2022-07-18T06:08:10.935711Z","iopub.status.idle":"2022-07-18T06:08:11.034340Z","shell.execute_reply.started":"2022-07-18T06:08:10.935672Z","shell.execute_reply":"2022-07-18T06:08:11.032949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pt = PowerTransformer()\npt.fit_transform(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:11.036147Z","iopub.execute_input":"2022-07-18T06:08:11.036852Z","iopub.status.idle":"2022-07-18T06:08:15.041812Z","shell.execute_reply.started":"2022-07-18T06:08:11.036794Z","shell.execute_reply":"2022-07-18T06:08:15.040626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:15.047558Z","iopub.execute_input":"2022-07-18T06:08:15.048251Z","iopub.status.idle":"2022-07-18T06:08:15.074433Z","shell.execute_reply.started":"2022-07-18T06:08:15.048197Z","shell.execute_reply":"2022-07-18T06:08:15.073537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BGM = BayesianGaussianMixture(n_components=7,covariance_type='full',random_state=1).fit(df)\n#BGM.fit_predict(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:08:15.075895Z","iopub.execute_input":"2022-07-18T06:08:15.076398Z","iopub.status.idle":"2022-07-18T06:09:39.372149Z","shell.execute_reply.started":"2022-07-18T06:08:15.076362Z","shell.execute_reply":"2022-07-18T06:09:39.370591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BGM.means_","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:09:39.374731Z","iopub.execute_input":"2022-07-18T06:09:39.375866Z","iopub.status.idle":"2022-07-18T06:09:39.396832Z","shell.execute_reply.started":"2022-07-18T06:09:39.375804Z","shell.execute_reply":"2022-07-18T06:09:39.395147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BGM.means_.shape[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:09:39.402580Z","iopub.execute_input":"2022-07-18T06:09:39.403011Z","iopub.status.idle":"2022-07-18T06:09:39.411012Z","shell.execute_reply.started":"2022-07-18T06:09:39.402975Z","shell.execute_reply":"2022-07-18T06:09:39.409549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#plt.style.use('ggplot')\nplt.figure(figsize=(15,6))\nfor i in range(len(BGM.means_)):\n    plt.scatter(np.arange(df.shape[1]), BGM.means_[i])\nplt.xticks(ticks=np.arange(df.shape[1]), labels=df.columns)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:09:39.412811Z","iopub.execute_input":"2022-07-18T06:09:39.413308Z","iopub.status.idle":"2022-07-18T06:09:39.770058Z","shell.execute_reply.started":"2022-07-18T06:09:39.413273Z","shell.execute_reply":"2022-07-18T06:09:39.768756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_features = ['f_07','f_08','f_09','f_10','f_11','f_12','f_13','f_22','f_23','f_24','f_25','f_26','f_27','f_28']","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:09:39.771879Z","iopub.execute_input":"2022-07-18T06:09:39.772420Z","iopub.status.idle":"2022-07-18T06:09:39.778904Z","shell.execute_reply.started":"2022-07-18T06:09:39.772373Z","shell.execute_reply":"2022-07-18T06:09:39.777975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_total = list(df.columns)\nlen(best_features)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:11:37.405226Z","iopub.execute_input":"2022-07-18T06:11:37.405655Z","iopub.status.idle":"2022-07-18T06:11:37.414402Z","shell.execute_reply.started":"2022-07-18T06:11:37.405621Z","shell.execute_reply":"2022-07-18T06:11:37.412829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_drop = list(set(df_total) - set(best_features))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:12:18.427661Z","iopub.execute_input":"2022-07-18T06:12:18.428026Z","iopub.status.idle":"2022-07-18T06:12:18.433354Z","shell.execute_reply.started":"2022-07-18T06:12:18.427997Z","shell.execute_reply":"2022-07-18T06:12:18.432488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(list_drop, axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:22:23.987730Z","iopub.execute_input":"2022-07-18T06:22:23.988121Z","iopub.status.idle":"2022-07-18T06:22:23.997394Z","shell.execute_reply.started":"2022-07-18T06:22:23.988091Z","shell.execute_reply":"2022-07-18T06:22:23.996554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:22:38.997184Z","iopub.execute_input":"2022-07-18T06:22:38.997657Z","iopub.status.idle":"2022-07-18T06:22:39.025339Z","shell.execute_reply.started":"2022-07-18T06:22:38.997620Z","shell.execute_reply":"2022-07-18T06:22:39.024544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from sklearn.metrics import silhouette_score as ss\nli = [x for x in range(2,16)]\nfor cls in li:\n    cluster = KMeans(n_clusters = cls).fit(df)\n    \n    predC = cluster.predict(df)\n    score = round(ss(df ,predC),3)\n    print(cls,':',score)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:22:58.134394Z","iopub.execute_input":"2022-07-18T06:22:58.135289Z","iopub.status.idle":"2022-07-18T06:22:58.142922Z","shell.execute_reply.started":"2022-07-18T06:22:58.135247Z","shell.execute_reply":"2022-07-18T06:22:58.141516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wcss = []\nfor k in range(1,10):\n    km = KMeans(n_clusters = k).fit(df)\n    wcss.append(km.inertia_)\nplt.plot(wcss,'-or')\nplt.xlabel('K')\nplt.ylabel('WCSS')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:22:58.185674Z","iopub.execute_input":"2022-07-18T06:22:58.186064Z","iopub.status.idle":"2022-07-18T06:23:36.973270Z","shell.execute_reply.started":"2022-07-18T06:22:58.186033Z","shell.execute_reply":"2022-07-18T06:23:36.970500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\n\nElbow_M = KElbowVisualizer(KMeans(), k=(4,13))\nElbow_M.fit(df)\nElbow_M.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:23:36.975741Z","iopub.execute_input":"2022-07-18T06:23:36.976232Z","iopub.status.idle":"2022-07-18T06:24:37.837226Z","shell.execute_reply.started":"2022-07-18T06:23:36.976188Z","shell.execute_reply":"2022-07-18T06:24:37.835858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"km = KMeans(n_clusters = 7).fit(df)\npredKM = km.predict(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:24:37.838998Z","iopub.execute_input":"2022-07-18T06:24:37.839313Z","iopub.status.idle":"2022-07-18T06:24:43.080576Z","shell.execute_reply.started":"2022-07-18T06:24:37.839283Z","shell.execute_reply":"2022-07-18T06:24:43.079348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predKM)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:24:43.083525Z","iopub.execute_input":"2022-07-18T06:24:43.083883Z","iopub.status.idle":"2022-07-18T06:24:43.092362Z","shell.execute_reply.started":"2022-07-18T06:24:43.083852Z","shell.execute_reply":"2022-07-18T06:24:43.090233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gmm = BayesianGaussianMixture(n_components = 7, n_init = 5, random_state = 20).fit(df)\npredGMM = gmm.predict(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:25:39.357960Z","iopub.execute_input":"2022-07-18T06:25:39.358362Z","iopub.status.idle":"2022-07-18T06:29:33.332847Z","shell.execute_reply.started":"2022-07-18T06:25:39.358329Z","shell.execute_reply":"2022-07-18T06:29:33.331507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predGMM)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T06:29:33.335428Z","iopub.execute_input":"2022-07-18T06:29:33.336777Z","iopub.status.idle":"2022-07-18T06:29:33.345175Z","shell.execute_reply.started":"2022-07-18T06:29:33.336722Z","shell.execute_reply":"2022-07-18T06:29:33.343996Z"},"trusted":true},"execution_count":null,"outputs":[]}]}