{"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\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import PCA\nfrom sklearn.mixture import GaussianMixture, BayesianGaussianMixture\nfrom sklearn.preprocessing import PowerTransformer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-14T07:21:54.924493Z","iopub.execute_input":"2022-07-14T07:21:54.925361Z","iopub.status.idle":"2022-07-14T07:21:55.936563Z","shell.execute_reply.started":"2022-07-14T07:21:54.925266Z","shell.execute_reply":"2022-07-14T07:21:55.935609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:55.938798Z","iopub.execute_input":"2022-07-14T07:21:55.939478Z","iopub.status.idle":"2022-07-14T07:21:57.148848Z","shell.execute_reply.started":"2022-07-14T07:21:55.939436Z","shell.execute_reply":"2022-07-14T07:21:57.147712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:57.150518Z","iopub.execute_input":"2022-07-14T07:21:57.150961Z","iopub.status.idle":"2022-07-14T07:21:57.161390Z","shell.execute_reply.started":"2022-07-14T07:21:57.150918Z","shell.execute_reply":"2022-07-14T07:21:57.160032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:57.164512Z","iopub.execute_input":"2022-07-14T07:21:57.164987Z","iopub.status.idle":"2022-07-14T07:21:57.204874Z","shell.execute_reply.started":"2022-07-14T07:21:57.164934Z","shell.execute_reply":"2022-07-14T07:21:57.203673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of missing values: \", df.isna().sum().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:57.206545Z","iopub.execute_input":"2022-07-14T07:21:57.206984Z","iopub.status.idle":"2022-07-14T07:21:57.218911Z","shell.execute_reply.started":"2022-07-14T07:21:57.206934Z","shell.execute_reply":"2022-07-14T07:21:57.217697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(columns=['id']).describe().T\\\n        .style.bar(subset=['mean'])\\\n        .background_gradient(subset=['std'], cmap='Greens')\\\n        .background_gradient(subset=['50%'], cmap='BuGn')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:57.220652Z","iopub.execute_input":"2022-07-14T07:21:57.220941Z","iopub.status.idle":"2022-07-14T07:21:57.515203Z","shell.execute_reply.started":"2022-07-14T07:21:57.220913Z","shell.execute_reply":"2022-07-14T07:21:57.514032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize=(16, 18))\nfeatCount = 29\nfor i in range(featCount):\n    if i < 10:\n        feat_name = 'f_0' + str(i)\n    else:\n        feat_name = 'f_' + str(i)\n    plt.subplot(10, 3, i+1)\n    plt.hist(df[feat_name], bins=100)\n    plt.title(f'{feat_name}')\nfigure.tight_layout(h_pad=1.0, w_pad=1.0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:21:57.516619Z","iopub.execute_input":"2022-07-14T07:21:57.516950Z","iopub.status.idle":"2022-07-14T07:22:06.426684Z","shell.execute_reply.started":"2022-07-14T07:21:57.516919Z","shell.execute_reply":"2022-07-14T07:22:06.425462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = df.corr().round(2)\nplt.figure(figsize=(20,10))\nsns.heatmap(corr, vmin=-1, vmax=1, center=0, square=False, annot=True, cmap='coolwarm')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:06.428375Z","iopub.execute_input":"2022-07-14T07:22:06.428711Z","iopub.status.idle":"2022-07-14T07:22:09.776684Z","shell.execute_reply.started":"2022-07-14T07:22:06.428680Z","shell.execute_reply":"2022-07-14T07:22:09.775485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = 7 # Best K (Formally found by Elbow Method. In my case, bruteforce :D)\nSEED = 42 # Random seed: the answer to life the universe and everything.","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:09.778355Z","iopub.execute_input":"2022-07-14T07:22:09.778668Z","iopub.status.idle":"2022-07-14T07:22:09.783639Z","shell.execute_reply.started":"2022-07-14T07:22:09.778641Z","shell.execute_reply":"2022-07-14T07:22:09.782500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_scaled = df.copy()\ncols = list(train_scaled.columns)\ntrain_scaled[cols] = PowerTransformer().fit_transform(train_scaled[cols])\nbest_data =['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-14T07:22:09.788000Z","iopub.execute_input":"2022-07-14T07:22:09.789308Z","iopub.status.idle":"2022-07-14T07:22:13.738736Z","shell.execute_reply.started":"2022-07-14T07:22:09.789231Z","shell.execute_reply":"2022-07-14T07:22:13.737293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How our data looks:","metadata":{}},{"cell_type":"code","source":"pca = PCA(random_state = SEED, whiten = True)\nX_pca = pca.fit_transform(train_scaled)\nPCA_df = pd.DataFrame({\"PCA_1\" : X_pca[:,0], \"PCA_2\" : X_pca[:,1]})\nplt.figure(figsize=(14, 14))\nsns.scatterplot(data = PCA_df, x = \"PCA_1\", y = \"PCA_2\", s=3);","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:13.740054Z","iopub.execute_input":"2022-07-14T07:22:13.740404Z","iopub.status.idle":"2022-07-14T07:22:14.293573Z","shell.execute_reply.started":"2022-07-14T07:22:13.740373Z","shell.execute_reply":"2022-07-14T07:22:14.292035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"_____","metadata":{}},{"cell_type":"markdown","source":"## Algorithms","metadata":{}},{"cell_type":"markdown","source":"### Fuzzy C-Means Clustering","metadata":{"execution":{"iopub.status.busy":"2022-07-14T01:48:39.545819Z","iopub.execute_input":"2022-07-14T01:48:39.546842Z","iopub.status.idle":"2022-07-14T01:48:39.557249Z","shell.execute_reply.started":"2022-07-14T01:48:39.546801Z","shell.execute_reply":"2022-07-14T01:48:39.555890Z"}}},{"cell_type":"markdown","source":"Something is *very* wrong with this. Is it an implementation issue?","metadata":{}},{"cell_type":"code","source":"!pip install fuzzy-c-means","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:14.295231Z","iopub.execute_input":"2022-07-14T07:22:14.296840Z","iopub.status.idle":"2022-07-14T07:22:27.953041Z","shell.execute_reply.started":"2022-07-14T07:22:14.296803Z","shell.execute_reply":"2022-07-14T07:22:27.951939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fcmeans import FCM\nfc_X_scaled = np.array(train_scaled[best_data])\nfc_means = FCM(n_clusters=7) # we use two cluster as an example\nfc_means.fit(fc_X_scaled) ## X, numpy array. rows:samples columns:features\npreds_1 = fc_means.predict(fc_X_scaled)\npca = PCA(n_components=2)\nreduced_data = pca.fit_transform(fc_X_scaled)\ndf = pd.DataFrame({\"x\" : reduced_data[:,0], \"y\" : reduced_data[:,1], \"clusters\" : preds_1})\nplt.figure(figsize=(20, 10))\nsns.scatterplot(x=df[\"x\"], y=df[\"y\"], hue=df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:27.954688Z","iopub.execute_input":"2022-07-14T07:22:27.955140Z","iopub.status.idle":"2022-07-14T07:22:34.199867Z","shell.execute_reply.started":"2022-07-14T07:22:27.955097Z","shell.execute_reply":"2022-07-14T07:22:34.198651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### K-Means Clustering","metadata":{}},{"cell_type":"code","source":"k_means = KMeans(n_clusters=K ,n_init=5, random_state=SEED)\npreds_2 = k_means.fit_predict(train_scaled[best_data])\npca = PCA(n_components=2)\nreduced_data = pca.fit_transform(train_scaled[best_data])\ndf = pd.DataFrame({\"x\" : reduced_data[:,0], \"y\" : reduced_data[:,1], \"clusters\" : preds_2})\nplt.figure(figsize=(20, 10))\nsns.scatterplot(x=df[\"x\"], y=df[\"y\"], hue=df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:34.201336Z","iopub.execute_input":"2022-07-14T07:22:34.201660Z","iopub.status.idle":"2022-07-14T07:22:40.598828Z","shell.execute_reply.started":"2022-07-14T07:22:34.201630Z","shell.execute_reply":"2022-07-14T07:22:40.597572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Bayesian Gaussian Mixture","metadata":{}},{"cell_type":"code","source":"gmm = BayesianGaussianMixture(n_components=K, n_init=5, random_state=SEED)\npreds_3 = gmm.fit_predict(train_scaled[best_data])\n\npca = PCA(n_components=2)\nreduced_data = pca.fit_transform(train_scaled[best_data])\ndf = pd.DataFrame({\"x\" : reduced_data[:,0], \"y\" : reduced_data[:,1], \"clusters\" : preds_3})\nplt.figure(figsize=(20, 10))\nsns.scatterplot(x=df[\"x\"], y=df[\"y\"], hue=df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:22:40.600139Z","iopub.execute_input":"2022-07-14T07:22:40.600523Z","iopub.status.idle":"2022-07-14T07:25:55.152201Z","shell.execute_reply.started":"2022-07-14T07:22:40.600489Z","shell.execute_reply":"2022-07-14T07:25:55.151288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")\nsubmission[\"Predicted\"] = preds_3 # bgmm\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:25:55.153690Z","iopub.execute_input":"2022-07-14T07:25:55.154334Z","iopub.status.idle":"2022-07-14T07:25:55.198144Z","shell.execute_reply.started":"2022-07-14T07:25:55.154298Z","shell.execute_reply":"2022-07-14T07:25:55.196951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T07:25:55.199537Z","iopub.execute_input":"2022-07-14T07:25:55.199855Z","iopub.status.idle":"2022-07-14T07:25:55.362449Z","shell.execute_reply.started":"2022-07-14T07:25:55.199826Z","shell.execute_reply":"2022-07-14T07:25:55.361578Z"},"trusted":true},"execution_count":null,"outputs":[]}]}