{"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 # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport os\nimport seaborn as sns\nfrom sklearn.preprocessing import LabelEncoder ,RobustScaler , PowerTransformer , MaxAbsScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.mixture import BayesianGaussianMixture\nfrom yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.cluster import KMeans\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib.colors import ListedColormap\nfrom sklearn import metrics\nfrom sklearn.manifold import TSNE\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom time import time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T04:14:23.684389Z","iopub.execute_input":"2022-07-14T04:14:23.684703Z","iopub.status.idle":"2022-07-14T04:14:24.919895Z","shell.execute_reply.started":"2022-07-14T04:14:23.684679Z","shell.execute_reply":"2022-07-14T04:14:24.918834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndata.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:14:25.061332Z","iopub.execute_input":"2022-07-14T04:14:25.061884Z","iopub.status.idle":"2022-07-14T04:14:26.438246Z","shell.execute_reply.started":"2022-07-14T04:14:25.061851Z","shell.execute_reply":"2022-07-14T04:14:26.437243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop([\"id\"],axis=1,inplace=True)\nfig = plt.figure(figsize=(20,30));\nfor i,col in enumerate(data.columns):\n    plt.subplot(10,3,i+1);\n    sns.histplot(x=col,data=data);\n    fig.tight_layout()\n    plt.xlabel('');\n    plt.title(col,fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:14:26.534465Z","iopub.execute_input":"2022-07-14T04:14:26.534803Z","iopub.status.idle":"2022-07-14T04:14:51.814363Z","shell.execute_reply.started":"2022-07-14T04:14:26.534780Z","shell.execute_reply":"2022-07-14T04:14:51.813565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = data.columns\n\nminMax = MaxAbsScaler()\nminMax.fit(data)\nXdata = minMax.fit_transform(data)\n\npowerTransformer = PowerTransformer()\npowerTransformer.fit(Xdata)\ndf = powerTransformer.transform(Xdata)\n\ntrain = pd.DataFrame(df,columns=cols)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:14:51.815886Z","iopub.execute_input":"2022-07-14T04:14:51.816319Z","iopub.status.idle":"2022-07-14T04:14:54.738807Z","shell.execute_reply.started":"2022-07-14T04:14:51.816291Z","shell.execute_reply":"2022-07-14T04:14:54.737474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Turn dataframe into arrays\nX = data.values\n\npca = PCA(n_components=7)\npca_2d = pca.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:17:02.862708Z","iopub.execute_input":"2022-07-12T16:17:02.863437Z","iopub.status.idle":"2022-07-12T16:17:03.488203Z","shell.execute_reply.started":"2022-07-12T16:17:02.863379Z","shell.execute_reply":"2022-07-12T16:17:03.486856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_2d.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:17:03.491585Z","iopub.execute_input":"2022-07-12T16:17:03.492860Z","iopub.status.idle":"2022-07-12T16:17:03.507536Z","shell.execute_reply.started":"2022-07-12T16:17:03.492809Z","shell.execute_reply":"2022-07-12T16:17:03.505640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the TSNE and PCA visuals side-by-side\nplt.figure(figsize = (16,11))\n# plt.subplot(121)\nplt.scatter(pca_2d[:,0],pca_2d[:,1],\n            cmap = \"coolwarm\", edgecolor = \"None\", alpha=0.35)\nplt.colorbar()\nplt.title('PCA Scatter Plot')\n# plt.subplot(122)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:17:03.509722Z","iopub.execute_input":"2022-07-12T16:17:03.510646Z","iopub.status.idle":"2022-07-12T16:17:04.473107Z","shell.execute_reply.started":"2022-07-12T16:17:03.510601Z","shell.execute_reply":"2022-07-12T16:17:04.471880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dX = train[:3000]\ndX.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:17:04.475009Z","iopub.execute_input":"2022-07-12T16:17:04.475528Z","iopub.status.idle":"2022-07-12T16:17:04.483647Z","shell.execute_reply.started":"2022-07-12T16:17:04.475481Z","shell.execute_reply":"2022-07-12T16:17:04.482709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=2000, method='exact')\ntsne_results = tsne.fit_transform(dX)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:17:04.485073Z","iopub.execute_input":"2022-07-12T16:17:04.485791Z","iopub.status.idle":"2022-07-12T16:26:13.887872Z","shell.execute_reply.started":"2022-07-12T16:17:04.485753Z","shell.execute_reply":"2022-07-12T16:26:13.886684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(tsne_results[:,0],tsne_results[:,1],  \n            cmap = \"coolwarm\", edgecolor = \"None\", alpha=0.35)\nplt.colorbar()\nplt.title('TSNE Scatter Plot')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:26:13.889637Z","iopub.execute_input":"2022-07-12T16:26:13.890344Z","iopub.status.idle":"2022-07-12T16:26:14.584423Z","shell.execute_reply.started":"2022-07-12T16:26:13.890300Z","shell.execute_reply":"2022-07-12T16:26:14.583082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig = plt.figure(figsize=(25,50))\n# sub, ax  = plt.subplots(6,5)\n# feature = 0\n# for i in range(6):\n#     for j in range(5):\n#         feature+=1\n#         if feature>28:\n#             break\n#         if feature < 10:\n#             feature = \"0\"+str(feature)\n#         ax[i][j].scatter(data.f_00,data[f\"f_{feature}\"])\n#         ax[i][j].set_title(f\"f_00*f_{feature}\")\n#         fig.tight_layout()\n#         feature = int(feature)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T11:09:51.805295Z","iopub.execute_input":"2022-07-11T11:09:51.805596Z","iopub.status.idle":"2022-07-11T11:09:51.810823Z","shell.execute_reply.started":"2022-07-11T11:09:51.805572Z","shell.execute_reply":"2022-07-11T11:09:51.809808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.triu(np.ones_like(data.corr(), dtype=bool))\ncmap = sns.diverging_palette(230, 20, as_cmap=True)\nf, ax = plt.subplots(figsize=(20,20))\nsns.heatmap(data.corr(), mask=mask, vmax=.5,\n            square=True, linewidths=.3, cbar_kws={\"shrink\": .4},annot=True,fmt='.2f')","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:26:14.585926Z","iopub.execute_input":"2022-07-12T16:26:14.586287Z","iopub.status.idle":"2022-07-12T16:26:17.070682Z","shell.execute_reply.started":"2022-07-12T16:26:14.586256Z","shell.execute_reply":"2022-07-12T16:26:17.069573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=3,random_state=42)\npca.fit(data)\nPCA_ds = pd.DataFrame(pca.transform(data), columns=([\"col1\",\"col2\",\"col3\"]))\nfig = plt.figure(figsize=(10,8))\nax = fig.add_subplot(111, projection=\"3d\")\nax.scatter(PCA_ds[\"col1\"],PCA_ds[\"col2\"],PCA_ds[\"col3\"], marker=\"o\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T09:47:33.979876Z","iopub.execute_input":"2022-07-11T09:47:33.981335Z","iopub.status.idle":"2022-07-11T09:47:36.675017Z","shell.execute_reply.started":"2022-07-11T09:47:33.981289Z","shell.execute_reply":"2022-07-11T09:47:36.674098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Elbow_M = KElbowVisualizer(KMeans(random_state=42),k=14)\nElbow_M.fit(data)\nElbow_M.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T16:26:17.073425Z","iopub.execute_input":"2022-07-12T16:26:17.074402Z","iopub.status.idle":"2022-07-12T16:27:40.458643Z","shell.execute_reply.started":"2022-07-12T16:26:17.074369Z","shell.execute_reply":"2022-07-12T16:27:40.457439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predsAry = []\nfor i in range(100):\n    bgm = BayesianGaussianMixture(\n        n_components = 7,#7\n        covariance_type = 'full',\n        random_state = i+1#1\n    )\n    preds = bgm.fit_predict(train)\n    predsAry.append(preds)\n    print(str(i+1) + ' / 100')\n    \ndf_predsAry = pd.DataFrame(predsAry)\ndf_predsAry","metadata":{"execution":{"iopub.status.busy":"2022-07-14T04:14:54.741213Z","iopub.execute_input":"2022-07-14T04:14:54.741446Z","iopub.status.idle":"2022-07-14T05:28:33.844076Z","shell.execute_reply.started":"2022-07-14T04:14:54.741424Z","shell.execute_reply":"2022-07-14T05:28:33.843178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_mode = []\nfor i in range(98000):\n    if df_predsAry.iloc[0:,i].mode().count() == 1:\n        preds_mode.append(df_predsAry.iloc[:,i].mode()[0])\n    else:\n        preds_mode.append(df_predsAry.iloc[0,i])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:28:33.846127Z","iopub.execute_input":"2022-07-14T05:28:33.846382Z","iopub.status.idle":"2022-07-14T05:29:15.205562Z","shell.execute_reply.started":"2022-07-14T05:28:33.846360Z","shell.execute_reply":"2022-07-14T05:29:15.204760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = preds_mode.copy()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:29:15.206904Z","iopub.execute_input":"2022-07-14T05:29:15.207167Z","iopub.status.idle":"2022-07-14T05:29:15.211610Z","shell.execute_reply.started":"2022-07-14T05:29:15.207141Z","shell.execute_reply":"2022-07-14T05:29:15.210537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=2)\nreduced_data = pca.fit_transform(train)\n\ndf = pd.DataFrame(\n    {\n        \"x\": reduced_data[:,0],\n        \"y\": reduced_data[:,1],\n        \"clusters\" : preds\n    }\n)\nplt.figure(figsize=(20, 20))\nsns.scatterplot(x=df[\"x\"], y=df[\"y\"], hue=df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:15:09.674650Z","iopub.execute_input":"2022-07-12T18:15:09.675230Z","iopub.status.idle":"2022-07-12T18:15:13.787493Z","shell.execute_reply.started":"2022-07-12T18:15:09.675186Z","shell.execute_reply":"2022-07-12T18:15:13.786192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.iloc[:4000]","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:15:15.951694Z","iopub.execute_input":"2022-07-12T18:15:15.952029Z","iopub.status.idle":"2022-07-12T18:15:15.990787Z","shell.execute_reply.started":"2022-07-12T18:15:15.952001Z","shell.execute_reply":"2022-07-12T18:15:15.989780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=2000, method='exact')\ntsne_results = tsne.fit_transform(train.iloc[:4000])\n\ndf = pd.DataFrame(\n    {\n        \"x\": tsne_results[:,0],\n        \"y\": tsne_results[:,1],\n        \"clusters\" : preds[:4000]\n    }\n)\nplt.figure(figsize=(20, 20))\nsns.scatterplot(x=df[\"x\"], y=df[\"y\"], hue=df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T18:15:15.992235Z","iopub.execute_input":"2022-07-12T18:15:15.993182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl = sns.countplot(x=tsne_results[\"Clusters\"])\npl.set_title(\"Distribution Of The Clusters\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")\nsub.prediction = preds\nsub.to_csv(\"sub.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:29:15.212870Z","iopub.execute_input":"2022-07-14T05:29:15.213129Z","iopub.status.idle":"2022-07-14T05:29:15.412044Z","shell.execute_reply.started":"2022-07-14T05:29:15.213103Z","shell.execute_reply":"2022-07-14T05:29:15.411278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}