{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-07T05:36:10.460373Z","iopub.execute_input":"2022-07-07T05:36:10.460830Z","iopub.status.idle":"2022-07-07T05:36:10.494762Z","shell.execute_reply.started":"2022-07-07T05:36:10.460739Z","shell.execute_reply":"2022-07-07T05:36:10.493845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.manifold import TSNE\nfrom sklearn.cluster import KMeans","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:36:13.481727Z","iopub.execute_input":"2022-07-07T05:36:13.482151Z","iopub.status.idle":"2022-07-07T05:36:14.285207Z","shell.execute_reply.started":"2022-07-07T05:36:13.482117Z","shell.execute_reply":"2022-07-07T05:36:14.283890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import Source Data","metadata":{}},{"cell_type":"code","source":"train= pd.read_csv('/kaggle/input/tabular-playground-series-jul-2022/data.csv', sep=',', index_col='id')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T05:59:26.673667Z","iopub.execute_input":"2022-07-07T05:59:26.674106Z","iopub.status.idle":"2022-07-07T05:59:27.536737Z","shell.execute_reply.started":"2022-07-07T05:59:26.674061Z","shell.execute_reply":"2022-07-07T05:59:27.535517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub= pd.read_csv('/kaggle/input/tabular-playground-series-jul-2022/sample_submission.csv', sep=',', index_col='Id')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:23:43.936471Z","iopub.execute_input":"2022-07-07T07:23:43.936857Z","iopub.status.idle":"2022-07-07T07:23:43.978070Z","shell.execute_reply.started":"2022-07-07T07:23:43.936826Z","shell.execute_reply":"2022-07-07T07:23:43.977000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Columns Distribution","metadata":{}},{"cell_type":"code","source":"desc = train.describe().reset_index().iloc[1:,:]\n\nfig, ax = plt.subplots(5,7, figsize=(25,25))\n\nfig.suptitle('Train features Describe', fontsize=30)\n\nfor i,x in enumerate(desc.columns[1:]):\n    \n    \n    if i<7:\n        a=i\n        b=0\n    if i>=7 and i<14:\n        a=i-7\n        b=1\n    if i>=14 and i<21:\n        a=i-14\n        b=2\n    if i>=21 and i<28:\n        a=i-21\n        b=3\n    if i>=28 and i<35:\n        a=i-28\n        b=4\n    \n    \n    ax[b,a].plot(desc['index'],desc[x], marker='.', color='purple', label='train')\n    ax[b,a].set_title(x)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:36:34.148196Z","iopub.execute_input":"2022-07-07T05:36:34.148604Z","iopub.status.idle":"2022-07-07T05:36:37.885593Z","shell.execute_reply.started":"2022-07-07T05:36:34.148566Z","shell.execute_reply":"2022-07-07T05:36:37.884235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfig, ax = plt.subplots(5,7, figsize=(25,25))\n\nfig.suptitle('Feature Distribution', fontsize=30)\n\nfor i,x in enumerate(desc.columns[1:]):\n    \n    \n    if i<7:\n        a=i\n        b=0\n    if i>=7 and i<14:\n        a=i-7\n        b=1\n    if i>=14 and i<21:\n        a=i-14\n        b=2\n    if i>=21 and i<28:\n        a=i-21\n        b=3\n    if i>=28 and i<35:\n        a=i-28\n        b=4\n    \n    ax[b,a].hist(train[x], color='lime', label='train', alpha=0.8)\n    ax[b,a].set_title(x)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:37:50.619734Z","iopub.execute_input":"2022-07-07T05:37:50.620206Z","iopub.status.idle":"2022-07-07T05:37:54.745241Z","shell.execute_reply.started":"2022-07-07T05:37:50.620168Z","shell.execute_reply":"2022-07-07T05:37:54.743780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['mean'] = train.mean(axis=1)\ntrain['std'] = train.std(axis=1)\nplt.figure(figsize=(30,10))\n_=plt.title('Mean vs Std by row- Not scaled', fontsize=30)\n_=plt.scatter(train['mean'], train['std'], color = 'purple', alpha=0.5, edgecolor='lime')\n_=plt.xlabel('Mean', fontsize=10)\n_=plt.ylabel('Std', fontsize=10)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:59:35.787248Z","iopub.execute_input":"2022-07-07T05:59:35.787627Z","iopub.status.idle":"2022-07-07T05:59:36.396751Z","shell.execute_reply.started":"2022-07-07T05:59:35.787597Z","shell.execute_reply":"2022-07-07T05:59:36.395457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(columns=['mean','std'])\nscaler = RobustScaler()\ntrain1 = pd.DataFrame(scaler.fit_transform(train), columns=train.columns, index=train.index)\ntrain1['mean'] = train.mean(axis=1)\ntrain1['std'] = train.std(axis=1)\nplt.figure(figsize=(30,10))\n_=plt.title('Mean vs Std by row- Standard Scaler', fontsize=30)\n_=plt.scatter(train1['mean'], train1['std'], color = 'lime', alpha=0.5, edgecolor='darkgreen')\n_=plt.xlabel('Mean', fontsize=10)\n_=plt.ylabel('Std', fontsize=10)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:59:40.145362Z","iopub.execute_input":"2022-07-07T05:59:40.145734Z","iopub.status.idle":"2022-07-07T05:59:40.919500Z","shell.execute_reply.started":"2022-07-07T05:59:40.145703Z","shell.execute_reply":"2022-07-07T05:59:40.918127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,5))\n_=train.nunique().plot(linewidth=2, marker='o', color='darkblue')\n_=plt.title('#unique values by column', fontsize=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T05:49:31.594612Z","iopub.execute_input":"2022-07-07T05:49:31.595023Z","iopub.status.idle":"2022-07-07T05:49:31.911724Z","shell.execute_reply.started":"2022-07-07T05:49:31.594988Z","shell.execute_reply":"2022-07-07T05:49:31.910346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,5))\n_=train.nunique(axis=1).plot(linewidth=2, marker='o', color='darkblue', alpha=0.5)\n_=plt.title('#unique values by row', fontsize=30)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T05:50:35.192522Z","iopub.execute_input":"2022-07-07T05:50:35.192909Z","iopub.status.idle":"2022-07-07T05:50:41.525345Z","shell.execute_reply.started":"2022-07-07T05:50:35.192860Z","shell.execute_reply":"2022-07-07T05:50:41.524059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = ['f_07', 'f_08','f_09', 'f_10', 'f_11', 'f_12', 'f_13']","metadata":{"execution":{"iopub.status.busy":"2022-07-07T05:55:49.600012Z","iopub.execute_input":"2022-07-07T05:55:49.600404Z","iopub.status.idle":"2022-07-07T05:55:49.605411Z","shell.execute_reply.started":"2022-07-07T05:55:49.600373Z","shell.execute_reply":"2022-07-07T05:55:49.604204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PCA","metadata":{}},{"cell_type":"code","source":"pca = PCA()\ntrain1 = train1.drop(columns=['mean','std'])\ntrain2 = pd.DataFrame(pca.fit_transform(train1[col]), index=train1.index)\nplt.figure(figsize=(15,5))\n_=plt.plot(pca.explained_variance_ratio_.cumsum(), linewidth=4, linestyle='--', color='darkred')\n_=plt.title('Explained variance Ration by PCA components', fontsize=30)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T05:59:47.978859Z","iopub.execute_input":"2022-07-07T05:59:47.979388Z","iopub.status.idle":"2022-07-07T05:59:48.246995Z","shell.execute_reply.started":"2022-07-07T05:59:47.979339Z","shell.execute_reply":"2022-07-07T05:59:48.246152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(4,4, figsize=(25,25))\n\nfig.suptitle('4 PCA components', fontsize=30)\n\nfor i in range(0,16):\n    \n    \n    if i<4:\n        a=i\n        b=0\n    if i>=4 and i<8:\n        a=i-4\n        b=1\n    if i>=8 and i<12:\n        a=i-8\n        b=2\n    if i>=12 and i<16:\n        a=i-12\n        b=3\n        \n    ax[b,a].scatter(train2[b],train2[a], color='darkgreen', label='train', alpha=0.5, edgecolor='lime')\n    ax[b,a].set_title(str(b)+'-'+str(a))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T06:03:11.244893Z","iopub.execute_input":"2022-07-07T06:03:11.245385Z","iopub.status.idle":"2022-07-07T06:03:16.352255Z","shell.execute_reply.started":"2022-07-07T06:03:11.245344Z","shell.execute_reply":"2022-07-07T06:03:16.350959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(20,20))\nax = plt.axes(projection='3d')\n_=plt.title('PCA 3d Components', fontsize=30)\n_=ax.scatter3D(train2[0], train2[1], train2[2], c=train2[0],cmap='viridis');\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:02.568396Z","iopub.execute_input":"2022-07-07T07:05:02.568797Z","iopub.status.idle":"2022-07-07T07:05:04.803335Z","shell.execute_reply.started":"2022-07-07T07:05:02.568764Z","shell.execute_reply":"2022-07-07T07:05:04.802133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TSNE","metadata":{}},{"cell_type":"code","source":"tsne = TSNE(n_components=3, learning_rate='auto', init='random', verbose=1)\ntrain3 = pd.DataFrame(tsne.fit_transform(train1[col]), index=train1.index)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-07T06:04:03.308477Z","iopub.execute_input":"2022-07-07T06:04:03.308897Z","iopub.status.idle":"2022-07-07T06:55:10.013338Z","shell.execute_reply.started":"2022-07-07T06:04:03.308849Z","shell.execute_reply":"2022-07-07T06:55:10.012357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:55:10.018020Z","iopub.execute_input":"2022-07-07T06:55:10.018382Z","iopub.status.idle":"2022-07-07T06:55:10.031347Z","shell.execute_reply.started":"2022-07-07T06:55:10.018351Z","shell.execute_reply":"2022-07-07T06:55:10.029756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('TSNE components 0 and 1', fontsize=30)\n_=plt.scatter(train3[0], train3[1], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:03:42.988818Z","iopub.execute_input":"2022-07-07T07:03:42.989629Z","iopub.status.idle":"2022-07-07T07:03:45.188195Z","shell.execute_reply.started":"2022-07-07T07:03:42.989562Z","shell.execute_reply":"2022-07-07T07:03:45.187027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('TSNE components 0 and 2', fontsize=30)\n_=plt.scatter(train3[0], train3[2], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_2', fontsize=10)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:03:55.891624Z","iopub.execute_input":"2022-07-07T07:03:55.892058Z","iopub.status.idle":"2022-07-07T07:03:58.081270Z","shell.execute_reply.started":"2022-07-07T07:03:55.892021Z","shell.execute_reply":"2022-07-07T07:03:58.080400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('TSNE components 1 and 2', fontsize=30)\n_=plt.scatter(train3[1], train3[2], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Compinents_1', fontsize=10)\n_=plt.ylabel('Components_2', fontsize=10)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:04:03.193596Z","iopub.execute_input":"2022-07-07T07:04:03.194558Z","iopub.status.idle":"2022-07-07T07:04:05.663367Z","shell.execute_reply.started":"2022-07-07T07:04:03.194514Z","shell.execute_reply":"2022-07-07T07:04:05.661985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(20,20))\nax = plt.axes(projection='3d')\n_=plt.title('TSNE 3d Components', fontsize=30)\n_=ax.scatter3D(train3[0], train3[1], train3[2], c=train3[0],cmap='viridis');\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:04:10.463890Z","iopub.execute_input":"2022-07-07T07:04:10.464267Z","iopub.status.idle":"2022-07-07T07:04:12.671396Z","shell.execute_reply.started":"2022-07-07T07:04:10.464236Z","shell.execute_reply":"2022-07-07T07:04:12.670136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3['mean']= train.mean(axis=1)\ntrain3['std']= train.std(axis=1)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:05:25.591258Z","iopub.execute_input":"2022-07-07T07:05:25.591656Z","iopub.status.idle":"2022-07-07T07:05:25.658660Z","shell.execute_reply.started":"2022-07-07T07:05:25.591623Z","shell.execute_reply":"2022-07-07T07:05:25.657316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('Row std/Mean vs Components_0', fontsize=30)\n_=plt.scatter(train3['std']/train3['mean'], train3[0], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Row std/Mean', fontsize=10)\n_=plt.ylabel('Component_0', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:28.505964Z","iopub.execute_input":"2022-07-07T07:05:28.506441Z","iopub.status.idle":"2022-07-07T07:05:30.774338Z","shell.execute_reply.started":"2022-07-07T07:05:28.506404Z","shell.execute_reply":"2022-07-07T07:05:30.773100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('Row std/Mean vs Components_1', fontsize=30)\n_=plt.scatter(train3['std']/train3['mean'], train3[1], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Row std/Mean', fontsize=10)\n_=plt.ylabel('Component_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:34.859134Z","iopub.execute_input":"2022-07-07T07:05:34.859502Z","iopub.status.idle":"2022-07-07T07:05:37.028157Z","shell.execute_reply.started":"2022-07-07T07:05:34.859472Z","shell.execute_reply":"2022-07-07T07:05:37.027160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,10))\n_=plt.title('Row std/Mean vs Components_1', fontsize=30)\n_=plt.scatter(train3['std']/train3['mean'], train3[2], alpha=0.5, c=train3[0],cmap='viridis')\n_=plt.xlabel('Row std/Mean', fontsize=10)\n_=plt.ylabel('Component_2', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:40.289985Z","iopub.execute_input":"2022-07-07T07:05:40.290392Z","iopub.status.idle":"2022-07-07T07:05:42.528430Z","shell.execute_reply.started":"2022-07-07T07:05:40.290361Z","shell.execute_reply":"2022-07-07T07:05:42.527064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(20,20))\nax = plt.axes(projection='3d')\n_=plt.title('TSNE 3d Components', fontsize=30)\n_=ax.scatter3D(train3[0], train3[1], train3['std']/train3['mean'], c=train3[0],cmap='viridis');\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:45.717929Z","iopub.execute_input":"2022-07-07T07:05:45.718431Z","iopub.status.idle":"2022-07-07T07:05:47.856219Z","shell.execute_reply.started":"2022-07-07T07:05:45.718390Z","shell.execute_reply":"2022-07-07T07:05:47.854962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(20,20))\nax = plt.axes(projection='3d')\n_=plt.title('TSNE 3d Components', fontsize=30)\n_=ax.scatter3D(train3[0], train3[2], train3['std']/train3['mean'], c=train3[0],cmap='viridis');\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:51.914582Z","iopub.execute_input":"2022-07-07T07:05:51.914985Z","iopub.status.idle":"2022-07-07T07:05:54.050014Z","shell.execute_reply.started":"2022-07-07T07:05:51.914951Z","shell.execute_reply":"2022-07-07T07:05:54.048816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(20,20))\nax = plt.axes(projection='3d')\n_=plt.title('TSNE 3d Components', fontsize=30)\n_=ax.scatter3D(train3[1], train3[2], train3['std']/train3['mean'], c=train3[0],cmap='viridis');\n_=plt.xlabel('Compinents_0', fontsize=10)\n_=plt.ylabel('Components_1', fontsize=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:05:59.374176Z","iopub.execute_input":"2022-07-07T07:05:59.374571Z","iopub.status.idle":"2022-07-07T07:06:01.606155Z","shell.execute_reply.started":"2022-07-07T07:05:59.374536Z","shell.execute_reply":"2022-07-07T07:06:01.604525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Kmeans","metadata":{}},{"cell_type":"code","source":"list=[]\nfor i in range(1,15):\n    kmeans = KMeans(n_clusters=i, random_state=0)\n    kmeans.fit(train[col])\n    list.append(kmeans.inertia_)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:06:14.103864Z","iopub.execute_input":"2022-07-07T07:06:14.104265Z","iopub.status.idle":"2022-07-07T07:07:20.750400Z","shell.execute_reply.started":"2022-07-07T07:06:14.104234Z","shell.execute_reply":"2022-07-07T07:07:20.748755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(30,8))\n_=plt.plot(range(1,15),list, color='darkred', linestyle='--', linewidth=5)\n_=plt.title('Kemans Elbow Method on train DF', fontsize=30)\n_=plt.xticks(range(1,15))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:07:20.752820Z","iopub.execute_input":"2022-07-07T07:07:20.753256Z","iopub.status.idle":"2022-07-07T07:07:21.025255Z","shell.execute_reply.started":"2022-07-07T07:07:20.753216Z","shell.execute_reply":"2022-07-07T07:07:21.023849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list=[]\nfor i in range(1,15):\n    kmeans = KMeans(n_clusters=i, random_state=0)\n    kmeans.fit(train1[col])\n    list.append(kmeans.inertia_)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:08:01.471266Z","iopub.execute_input":"2022-07-07T07:08:01.471653Z","iopub.status.idle":"2022-07-07T07:09:03.397380Z","shell.execute_reply.started":"2022-07-07T07:08:01.471620Z","shell.execute_reply":"2022-07-07T07:09:03.396027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_=plt.figure(figsize=(30,8))\n_=plt.plot(range(1,15),list, color='darkgreen', linestyle='--', linewidth=5)\n_=plt.title('Kemans Elbow Method on Scaled train DF', fontsize=30)\n_=plt.xticks(range(1,15))","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:09:03.402376Z","iopub.execute_input":"2022-07-07T07:09:03.402758Z","iopub.status.idle":"2022-07-07T07:09:03.623992Z","shell.execute_reply.started":"2022-07-07T07:09:03.402724Z","shell.execute_reply":"2022-07-07T07:09:03.622496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(2,7):\n    k = KMeans(n_clusters=i, random_state=0)\n    k.fit(train[col])\n    output = k.predict(train[col])\n    plt.figure(figsize=(30,10))\n    _=plt.title('Kmeans Clusters', fontsize=30)\n    _=plt.scatter(train3['std'], train3['mean'], alpha=0.5, c=output,cmap='viridis')\n    _=plt.xlabel('std', fontsize=10)\n    _=plt.ylabel('mean', fontsize=10)\n    _=plt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:09:03.625716Z","iopub.execute_input":"2022-07-07T07:09:03.626149Z","iopub.status.idle":"2022-07-07T07:09:30.544076Z","shell.execute_reply.started":"2022-07-07T07:09:03.626114Z","shell.execute_reply":"2022-07-07T07:09:30.543114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train2.columns= ['c0','c1','c2','c3','c4','c5','c6']","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:10:34.360123Z","iopub.execute_input":"2022-07-07T07:10:34.360558Z","iopub.status.idle":"2022-07-07T07:10:34.368205Z","shell.execute_reply.started":"2022-07-07T07:10:34.360522Z","shell.execute_reply":"2022-07-07T07:10:34.366004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3 = train3[[0,1,2]]","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:11:40.412662Z","iopub.execute_input":"2022-07-07T07:11:40.413117Z","iopub.status.idle":"2022-07-07T07:11:40.420797Z","shell.execute_reply.started":"2022-07-07T07:11:40.413077Z","shell.execute_reply":"2022-07-07T07:11:40.419451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train3.columns=['comp0','comp1','comp2']\ntrain4 = pd.concat([train[col], train3, train2], axis=1)\nfor i in range(2,10):\n    k = KMeans(n_clusters=i, random_state=0)\n    k.fit(train4)\n    output = k.predict(train4)\n    plt.figure(figsize=(30,10))\n    _=plt.title('Kmeans Clusters', fontsize=30)\n    _=plt.scatter(train3['comp0'], train3['comp1'], alpha=0.5, c=output,cmap='viridis')\n    _=plt.xlabel('tsne_comp0', fontsize=10)\n    _=plt.ylabel('tsne_comp1', fontsize=10)\n    _=plt.legend()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:12:30.183743Z","iopub.execute_input":"2022-07-07T07:12:30.184187Z","iopub.status.idle":"2022-07-07T07:13:25.260366Z","shell.execute_reply.started":"2022-07-07T07:12:30.184150Z","shell.execute_reply":"2022-07-07T07:13:25.259071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train4 = pd.concat([train[col], train3], axis=1)\nfor i in range(2,10):\n    k = KMeans(n_clusters=i, random_state=0)\n    k.fit(train4)\n    output = k.predict(train4)\n    plt.figure(figsize=(30,10))\n    _=plt.title('Kmeans Clusters', fontsize=30)\n    _=plt.scatter(train3['comp0'], train3['comp2'], alpha=0.5, c=output,cmap='viridis')\n    _=plt.xlabel('tsne_comp0', fontsize=10)\n    _=plt.ylabel('tsne_comp2', fontsize=10)\n    _=plt.legend()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-07T07:13:25.262378Z","iopub.execute_input":"2022-07-07T07:13:25.262754Z","iopub.status.idle":"2022-07-07T07:14:15.528266Z","shell.execute_reply.started":"2022-07-07T07:13:25.262721Z","shell.execute_reply":"2022-07-07T07:14:15.526693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fuzzy Kmeans","metadata":{}},{"cell_type":"code","source":"!pip install fuzzy-c-means","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-07-07T07:14:21.569076Z","iopub.execute_input":"2022-07-07T07:14:21.569459Z","iopub.status.idle":"2022-07-07T07:14:36.410740Z","shell.execute_reply.started":"2022-07-07T07:14:21.569427Z","shell.execute_reply":"2022-07-07T07:14:36.409525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"using fuzzy kmenas implementation for python\n\n@software{dias2019fuzzy,\n  author       = {Madson Luiz Dantas Dias},\n  title        = {fuzzy-c-means: An implementation of Fuzzy $C$-means clustering algorithm.},\n  month        = may,\n  year         = 2019,\n  publisher    = {Zenodo},\n  doi          = {10.5281/zenodo.3066222},\n  url          = {https://git.io/fuzzy-c-means}","metadata":{}},{"cell_type":"code","source":"from fcmeans import FCM","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:14:36.413629Z","iopub.execute_input":"2022-07-07T07:14:36.414042Z","iopub.status.idle":"2022-07-07T07:14:36.472774Z","shell.execute_reply.started":"2022-07-07T07:14:36.414004Z","shell.execute_reply":"2022-07-07T07:14:36.471662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(2,10):\n    fuzz = FCM(n_cluster=i)\n    fuzz.fit(train4.to_numpy())\n    centers = fuzz.centers\n    output = fuzz.predict(train4.to_numpy())\n    plt.figure(figsize=(30,10))\n    _=plt.title('Fuzzy Kmeans Clusters '+str(i), fontsize=30)\n    _=plt.scatter(train3['comp0'], train3['comp2'], alpha=0.5, c=output,cmap='viridis')\n    _=plt.xlabel('tsne_comp0', fontsize=10)\n    _=plt.ylabel('tsne_comp2', fontsize=10)\n    _=plt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:14:36.474442Z","iopub.execute_input":"2022-07-07T07:14:36.475153Z","iopub.status.idle":"2022-07-07T07:17:02.373753Z","shell.execute_reply.started":"2022-07-07T07:14:36.475108Z","shell.execute_reply":"2022-07-07T07:17:02.372243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(2,10):\n    fuzz = FCM(n_cluster=i)\n    fuzz.fit(train4.to_numpy())\n    centers = fuzz.centers\n    output = fuzz.predict(train4.to_numpy())\n    plt.figure(figsize=(30,10))\n    _=plt.title('Fuzzy Kmeans Clusters '+str(i), fontsize=30)\n    _=plt.scatter(train3['comp0'], train3['comp1'], alpha=0.5, c=output,cmap='viridis')\n    _=plt.xlabel('tsne_comp0', fontsize=10)\n    _=plt.ylabel('tsne_comp1', fontsize=10)\n    _=plt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:17:02.376382Z","iopub.execute_input":"2022-07-07T07:17:02.376785Z","iopub.status.idle":"2022-07-07T07:19:27.338128Z","shell.execute_reply.started":"2022-07-07T07:17:02.376748Z","shell.execute_reply":"2022-07-07T07:19:27.336985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WIP: Submission","metadata":{}},{"cell_type":"code","source":"fuzz = FCM(n_cluster=2)\nfuzz.fit(train4.to_numpy())\ncenters = fuzz.centers\noutput = fuzz.predict(train4.to_numpy())","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:23:51.541279Z","iopub.execute_input":"2022-07-07T07:23:51.541665Z","iopub.status.idle":"2022-07-07T07:24:07.667028Z","shell.execute_reply.started":"2022-07-07T07:23:51.541633Z","shell.execute_reply":"2022-07-07T07:24:07.664969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Predicted']=output","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:24:07.669859Z","iopub.execute_input":"2022-07-07T07:24:07.671464Z","iopub.status.idle":"2022-07-07T07:24:07.678695Z","shell.execute_reply.started":"2022-07-07T07:24:07.671383Z","shell.execute_reply":"2022-07-07T07:24:07.677344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:24:07.681004Z","iopub.execute_input":"2022-07-07T07:24:07.681959Z","iopub.status.idle":"2022-07-07T07:24:07.698009Z","shell.execute_reply.started":"2022-07-07T07:24:07.681905Z","shell.execute_reply":"2022-07-07T07:24:07.696549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = sub.reset_index()\nsub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:24:07.701553Z","iopub.execute_input":"2022-07-07T07:24:07.702269Z","iopub.status.idle":"2022-07-07T07:24:07.871960Z","shell.execute_reply.started":"2022-07-07T07:24:07.702209Z","shell.execute_reply":"2022-07-07T07:24:07.870589Z"},"trusted":true},"execution_count":null,"outputs":[]}]}