{"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 matplotlib\nimport matplotlib.pyplot as plt\nfrom matplotlib import colors\n\nimport seaborn as sns\n\nfrom sklearn.cluster import KMeans\nfrom sklearn.mixture import GaussianMixture\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import RobustScaler\n\n\nfrom scipy.stats import shapiro\nfrom termcolor import colored\nfrom scipy import stats","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-06T21:37:27.146741Z","iopub.execute_input":"2022-07-06T21:37:27.147233Z","iopub.status.idle":"2022-07-06T21:37:27.155765Z","shell.execute_reply.started":"2022-07-06T21:37:27.147196Z","shell.execute_reply":"2022-07-06T21:37:27.154259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndf.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:36:36.542663Z","iopub.execute_input":"2022-07-06T21:36:36.543231Z","iopub.status.idle":"2022-07-06T21:36:37.662184Z","shell.execute_reply.started":"2022-07-06T21:36:36.543195Z","shell.execute_reply":"2022-07-06T21:36:37.661248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:36:37.663567Z","iopub.execute_input":"2022-07-06T21:36:37.664137Z","iopub.status.idle":"2022-07-06T21:36:37.686346Z","shell.execute_reply.started":"2022-07-06T21:36:37.664102Z","shell.execute_reply":"2022-07-06T21:36:37.685310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:36:37.688760Z","iopub.execute_input":"2022-07-06T21:36:37.689826Z","iopub.status.idle":"2022-07-06T21:36:37.717009Z","shell.execute_reply.started":"2022-07-06T21:36:37.689771Z","shell.execute_reply":"2022-07-06T21:36:37.715861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (16,8))\nfor i in range(29):\n    feature_name = 'f_0{}'.format(i) if i < 10 else 'f_{}'.format(i) \n    plt.subplot(5, 6, i+1)\n    sns.kdeplot(df[feature_name], fill = True)        \nfigure.tight_layout(h_pad=1.0, w_pad=0.5)\nplt.suptitle('Distribution Plots', y=1.02)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:36:37.719545Z","iopub.execute_input":"2022-07-06T21:36:37.720796Z","iopub.status.idle":"2022-07-06T21:36:53.885619Z","shell.execute_reply.started":"2022-07-06T21:36:37.720739Z","shell.execute_reply":"2022-07-06T21:36:53.884199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (16,8))\nfor i in range(29):\n    feature_name = 'f_0{}'.format(i) if i < 10 else 'f_{}'.format(i) \n    plt.subplot(5, 6, i+1)\n    stats.probplot(df[feature_name], plot=plt)   \n    plt.title(feature_name)\nfigure.tight_layout(h_pad=1.0, w_pad=0.5)\nplt.suptitle('Q-Q Charts', y=1.02)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:36:53.887401Z","iopub.execute_input":"2022-07-06T21:36:53.888713Z","iopub.status.idle":"2022-07-06T21:37:03.157983Z","shell.execute_reply.started":"2022-07-06T21:36:53.888652Z","shell.execute_reply":"2022-07-06T21:37:03.156495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (16,8))\nfor i in range(29):\n    feature_name = 'f_0{}'.format(i) if i < 10 else 'f_{}'.format(i) \n    plt.subplot(5, 6, i+1)\n    sns.boxplot(data=df[feature_name])   \n    plt.title(feature_name)\nfigure.tight_layout(h_pad=1.0, w_pad=0.5)\nplt.suptitle('BOX plot', y=1.02)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:03.159702Z","iopub.execute_input":"2022-07-06T21:37:03.160088Z","iopub.status.idle":"2022-07-06T21:37:05.679001Z","shell.execute_reply.started":"2022-07-06T21:37:03.160051Z","shell.execute_reply":"2022-07-06T21:37:05.677660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = RobustScaler()\nscale = pd.DataFrame(sc.fit_transform(df))\nscale","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:05.681191Z","iopub.execute_input":"2022-07-06T21:37:05.681943Z","iopub.status.idle":"2022-07-06T21:37:05.878651Z","shell.execute_reply.started":"2022-07-06T21:37:05.681894Z","shell.execute_reply":"2022-07-06T21:37:05.877398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(scale.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:05.880264Z","iopub.execute_input":"2022-07-06T21:37:05.880887Z","iopub.status.idle":"2022-07-06T21:37:06.741241Z","shell.execute_reply.started":"2022-07-06T21:37:05.880835Z","shell.execute_reply":"2022-07-06T21:37:06.739751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nX = PCA()\ndecomp = X.fit_transform(scale)\ndecomp.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:06.743141Z","iopub.execute_input":"2022-07-06T21:37:06.743818Z","iopub.status.idle":"2022-07-06T21:37:06.881944Z","shell.execute_reply.started":"2022-07-06T21:37:06.743778Z","shell.execute_reply":"2022-07-06T21:37:06.880641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pcs=pd.DataFrame(decomp)\npcs.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:06.884290Z","iopub.execute_input":"2022-07-06T21:37:06.885256Z","iopub.status.idle":"2022-07-06T21:37:06.927883Z","shell.execute_reply.started":"2022-07-06T21:37:06.885187Z","shell.execute_reply":"2022-07-06T21:37:06.926627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df=pd.DataFrame(decomp[:,0:28])\nnew_df.head()\nsns.heatmap(new_df.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:06.930233Z","iopub.execute_input":"2022-07-06T21:37:06.931180Z","iopub.status.idle":"2022-07-06T21:37:07.498043Z","shell.execute_reply.started":"2022-07-06T21:37:06.931112Z","shell.execute_reply":"2022-07-06T21:37:07.496599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wcss = []\nfor i in range(1, 11):\n    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)\n    kmeans.fit(new_df)\n    wcss.append(kmeans.inertia_)\nplt.plot(range(1, 11), wcss)\nplt.title('The Elbow Method')\nplt.xlabel('Number of clusters')\nplt.ylabel('WCSS')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:37:31.821801Z","iopub.execute_input":"2022-07-06T21:37:31.822234Z","iopub.status.idle":"2022-07-06T21:38:26.341241Z","shell.execute_reply.started":"2022-07-06T21:37:31.822201Z","shell.execute_reply":"2022-07-06T21:38:26.339942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gm = GaussianMixture(n_components=6, random_state=0).fit(new_df)\ny_gm = gm.fit_predict(new_df)\ny_gm","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:38:47.139429Z","iopub.execute_input":"2022-07-06T21:38:47.140845Z","iopub.status.idle":"2022-07-06T21:39:05.104447Z","shell.execute_reply.started":"2022-07-06T21:38:47.140786Z","shell.execute_reply":"2022-07-06T21:39:05.103074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Predicted']=y_gm\nsubmission=df.loc[:,['id','Predicted']]\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:39:07.779010Z","iopub.execute_input":"2022-07-06T21:39:07.779451Z","iopub.status.idle":"2022-07-06T21:39:07.795480Z","shell.execute_reply.started":"2022-07-06T21:39:07.779420Z","shell.execute_reply":"2022-07-06T21:39:07.793977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:,['id','Predicted']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:39:09.992813Z","iopub.execute_input":"2022-07-06T21:39:09.993331Z","iopub.status.idle":"2022-07-06T21:39:10.008500Z","shell.execute_reply.started":"2022-07-06T21:39:09.993279Z","shell.execute_reply":"2022-07-06T21:39:10.007396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl = sns.countplot(x=df[\"Predicted\"])\npl.set_title(\"Distribution Of The Clusters\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:39:12.120409Z","iopub.execute_input":"2022-07-06T21:39:12.121240Z","iopub.status.idle":"2022-07-06T21:39:12.333926Z","shell.execute_reply.started":"2022-07-06T21:39:12.121169Z","shell.execute_reply":"2022-07-06T21:39:12.332374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submissiongm.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-06T21:39:15.071759Z","iopub.execute_input":"2022-07-06T21:39:15.072346Z","iopub.status.idle":"2022-07-06T21:39:15.267186Z","shell.execute_reply.started":"2022-07-06T21:39:15.072294Z","shell.execute_reply":"2022-07-06T21:39:15.265784Z"},"trusted":true},"execution_count":null,"outputs":[]}]}