{"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 numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\npd.set_option('display.max_columns',None)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:55.477404Z","iopub.execute_input":"2022-08-12T10:17:55.477931Z","iopub.status.idle":"2022-08-12T10:17:56.597794Z","shell.execute_reply.started":"2022-08-12T10:17:55.477824Z","shell.execute_reply":"2022-08-12T10:17:56.596896Z"},"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-08-12T10:17:56.599890Z","iopub.execute_input":"2022-08-12T10:17:56.600410Z","iopub.status.idle":"2022-08-12T10:17:57.926658Z","shell.execute_reply.started":"2022-08-12T10:17:56.600368Z","shell.execute_reply":"2022-08-12T10:17:57.925466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:57.928171Z","iopub.execute_input":"2022-08-12T10:17:57.928606Z","iopub.status.idle":"2022-08-12T10:17:57.971838Z","shell.execute_reply.started":"2022-08-12T10:17:57.928565Z","shell.execute_reply":"2022-08-12T10:17:57.970478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# query to find no. of rows and columns \ndf.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:57.974425Z","iopub.execute_input":"2022-08-12T10:17:57.975908Z","iopub.status.idle":"2022-08-12T10:17:57.982740Z","shell.execute_reply.started":"2022-08-12T10:17:57.975870Z","shell.execute_reply":"2022-08-12T10:17:57.981583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:57.984704Z","iopub.execute_input":"2022-08-12T10:17:57.985464Z","iopub.status.idle":"2022-08-12T10:17:58.020964Z","shell.execute_reply.started":"2022-08-12T10:17:57.985415Z","shell.execute_reply":"2022-08-12T10:17:58.019577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#query to check null values\ndf.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:58.022178Z","iopub.execute_input":"2022-08-12T10:17:58.023302Z","iopub.status.idle":"2022-08-12T10:17:58.037588Z","shell.execute_reply.started":"2022-08-12T10:17:58.023254Z","shell.execute_reply":"2022-08-12T10:17:58.036373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#query to check duplicates\ndf.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:58.038735Z","iopub.execute_input":"2022-08-12T10:17:58.040867Z","iopub.status.idle":"2022-08-12T10:17:58.296935Z","shell.execute_reply.started":"2022-08-12T10:17:58.040832Z","shell.execute_reply":"2022-08-12T10:17:58.295785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping id column\ndf.drop('id',axis =1,inplace =True)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:58.298588Z","iopub.execute_input":"2022-08-12T10:17:58.299574Z","iopub.status.idle":"2022-08-12T10:17:58.312287Z","shell.execute_reply.started":"2022-08-12T10:17:58.299537Z","shell.execute_reply":"2022-08-12T10:17:58.311149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:58.313815Z","iopub.execute_input":"2022-08-12T10:17:58.314975Z","iopub.status.idle":"2022-08-12T10:17:58.400118Z","shell.execute_reply.started":"2022-08-12T10:17:58.314930Z","shell.execute_reply":"2022-08-12T10:17:58.398571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting all the features\nfor feature in df.columns:\n    plt.hist(df[feature])\n    plt.xlabel(feature)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:17:58.412129Z","iopub.execute_input":"2022-08-12T10:17:58.416643Z","iopub.status.idle":"2022-08-12T10:18:04.887267Z","shell.execute_reply.started":"2022-08-12T10:17:58.416567Z","shell.execute_reply":"2022-08-12T10:18:04.886153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''for feature in df.columns:\n    sns.histplot(data =df[feature],bins =30,kde =True)\n    plt.show()'''","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:04.889633Z","iopub.execute_input":"2022-08-12T10:18:04.889971Z","iopub.status.idle":"2022-08-12T10:18:04.898325Z","shell.execute_reply.started":"2022-08-12T10:18:04.889942Z","shell.execute_reply":"2022-08-12T10:18:04.897041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import PowerTransformer\npt = PowerTransformer()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:04.900296Z","iopub.execute_input":"2022-08-12T10:18:04.900843Z","iopub.status.idle":"2022-08-12T10:18:04.970131Z","shell.execute_reply.started":"2022-08-12T10:18:04.900744Z","shell.execute_reply":"2022-08-12T10:18:04.969062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df =pd.DataFrame(pt.fit_transform(df),columns =df.columns)\nnew_df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:04.971447Z","iopub.execute_input":"2022-08-12T10:18:04.971904Z","iopub.status.idle":"2022-08-12T10:18:08.782119Z","shell.execute_reply.started":"2022-08-12T10:18:04.971864Z","shell.execute_reply":"2022-08-12T10:18:08.780946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feature in new_df.columns:\n    plt.hist(new_df[feature])\n    plt.xlabel(feature)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:08.783738Z","iopub.execute_input":"2022-08-12T10:18:08.784091Z","iopub.status.idle":"2022-08-12T10:18:15.754825Z","shell.execute_reply.started":"2022-08-12T10:18:08.784061Z","shell.execute_reply":"2022-08-12T10:18:15.753633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''for feature in new_df.columns:\n    sns.histplot(data =new_df[feature],bins =30,kde =True)\n    plt.show()'''","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:15.756309Z","iopub.execute_input":"2022-08-12T10:18:15.756653Z","iopub.status.idle":"2022-08-12T10:18:15.762709Z","shell.execute_reply.started":"2022-08-12T10:18:15.756621Z","shell.execute_reply":"2022-08-12T10:18:15.761721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.mixture import BayesianGaussianMixture\nbgm = BayesianGaussianMixture(n_components=7, random_state=1).fit(new_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:18:15.764099Z","iopub.execute_input":"2022-08-12T10:18:15.764463Z","iopub.status.idle":"2022-08-12T10:19:28.625007Z","shell.execute_reply.started":"2022-08-12T10:18:15.764432Z","shell.execute_reply":"2022-08-12T10:19:28.623103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgm.means_","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:28.626795Z","iopub.execute_input":"2022-08-12T10:19:28.627237Z","iopub.status.idle":"2022-08-12T10:19:28.647139Z","shell.execute_reply.started":"2022-08-12T10:19:28.627195Z","shell.execute_reply":"2022-08-12T10:19:28.644925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.arange(new_df.shape[1])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:28.648487Z","iopub.execute_input":"2022-08-12T10:19:28.649266Z","iopub.status.idle":"2022-08-12T10:19:28.658884Z","shell.execute_reply.started":"2022-08-12T10:19:28.649222Z","shell.execute_reply":"2022-08-12T10:19:28.658018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize =(15,5))\nfor i in range(len(bgm.means_)):\n    plt.scatter(np.arange(new_df.shape[1]),bgm.means_[i])\nplt.xticks(ticks =np.arange(new_df.shape[1]),labels =new_df.columns)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:28.660264Z","iopub.execute_input":"2022-08-12T10:19:28.660643Z","iopub.status.idle":"2022-08-12T10:19:29.122705Z","shell.execute_reply.started":"2022-08-12T10:19:28.660612Z","shell.execute_reply":"2022-08-12T10:19:29.121325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.124524Z","iopub.execute_input":"2022-08-12T10:19:29.125611Z","iopub.status.idle":"2022-08-12T10:19:29.133675Z","shell.execute_reply.started":"2022-08-12T10:19:29.125563Z","shell.execute_reply":"2022-08-12T10:19:29.132425Z"},"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-08-12T10:19:29.135175Z","iopub.execute_input":"2022-08-12T10:19:29.135617Z","iopub.status.idle":"2022-08-12T10:19:29.143183Z","shell.execute_reply.started":"2022-08-12T10:19:29.135575Z","shell.execute_reply":"2022-08-12T10:19:29.142095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(best_features)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.144642Z","iopub.execute_input":"2022-08-12T10:19:29.145401Z","iopub.status.idle":"2022-08-12T10:19:29.155839Z","shell.execute_reply.started":"2022-08-12T10:19:29.145343Z","shell.execute_reply":"2022-08-12T10:19:29.154891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_list=set(new_df.columns)-set(best_features)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.156914Z","iopub.execute_input":"2022-08-12T10:19:29.157846Z","iopub.status.idle":"2022-08-12T10:19:29.166340Z","shell.execute_reply.started":"2022-08-12T10:19:29.157809Z","shell.execute_reply":"2022-08-12T10:19:29.165285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop_list","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.167833Z","iopub.execute_input":"2022-08-12T10:19:29.168640Z","iopub.status.idle":"2022-08-12T10:19:29.178999Z","shell.execute_reply.started":"2022-08-12T10:19:29.168601Z","shell.execute_reply":"2022-08-12T10:19:29.177953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df =new_df.drop(drop_list,axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.180201Z","iopub.execute_input":"2022-08-12T10:19:29.180825Z","iopub.status.idle":"2022-08-12T10:19:29.192750Z","shell.execute_reply.started":"2022-08-12T10:19:29.180661Z","shell.execute_reply":"2022-08-12T10:19:29.191321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.194470Z","iopub.execute_input":"2022-08-12T10:19:29.194917Z","iopub.status.idle":"2022-08-12T10:19:29.227166Z","shell.execute_reply.started":"2022-08-12T10:19:29.194880Z","shell.execute_reply":"2022-08-12T10:19:29.225802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#wcss :within cluster sum of square\n# Silhouette score :to validate clusting model/ to verify number of clusters or K value","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.228953Z","iopub.execute_input":"2022-08-12T10:19:29.229689Z","iopub.status.idle":"2022-08-12T10:19:29.235180Z","shell.execute_reply.started":"2022-08-12T10:19:29.229643Z","shell.execute_reply":"2022-08-12T10:19:29.233818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.cluster import KMeans\nK_elbow =KElbowVisualizer(KMeans(),k=(2,10))\nK_elbow.fit(new_df)\nK_elbow.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:19:29.242053Z","iopub.execute_input":"2022-08-12T10:19:29.242528Z","iopub.status.idle":"2022-08-12T10:20:12.171745Z","shell.execute_reply.started":"2022-08-12T10:19:29.242474Z","shell.execute_reply":"2022-08-12T10:20:12.170642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# silhouette_score","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import silhouette_score","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:20:12.173552Z","iopub.execute_input":"2022-08-12T10:20:12.174335Z","iopub.status.idle":"2022-08-12T10:20:12.180415Z","shell.execute_reply.started":"2022-08-12T10:20:12.174296Z","shell.execute_reply":"2022-08-12T10:20:12.178883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# finding silhouette_score for range 2 to 16\nfor i in range(2,16):\n    cluster= KMeans(n_clusters =i).fit(new_df)\n    cluster_pred=cluster.predict(new_df)\n    slht_s=round(silhouette_score(new_df,cluster_pred),3)\n    print(i, '--',slht_s)'''","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:41.143555Z","iopub.execute_input":"2022-08-12T10:21:41.143952Z","iopub.status.idle":"2022-08-12T10:21:41.151455Z","shell.execute_reply.started":"2022-08-12T10:21:41.143922Z","shell.execute_reply":"2022-08-12T10:21:41.150302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# K_MEANS CLUSTERING","metadata":{}},{"cell_type":"code","source":"K_means =KMeans(n_clusters=2).fit(new_df)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:41.159255Z","iopub.execute_input":"2022-08-12T10:21:41.159655Z","iopub.status.idle":"2022-08-12T10:21:43.222182Z","shell.execute_reply.started":"2022-08-12T10:21:41.159625Z","shell.execute_reply":"2022-08-12T10:21:43.221016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_Kmeans =K_means.predict(new_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:43.227436Z","iopub.execute_input":"2022-08-12T10:21:43.228156Z","iopub.status.idle":"2022-08-12T10:21:43.248492Z","shell.execute_reply.started":"2022-08-12T10:21:43.228109Z","shell.execute_reply":"2022-08-12T10:21:43.247442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred_Kmeans)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:43.253133Z","iopub.execute_input":"2022-08-12T10:21:43.253592Z","iopub.status.idle":"2022-08-12T10:21:43.265770Z","shell.execute_reply.started":"2022-08-12T10:21:43.253559Z","shell.execute_reply":"2022-08-12T10:21:43.264462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BAYESIAN GAUSSIAN MIXTURE","metadata":{}},{"cell_type":"code","source":"BGM = BayesianGaussianMixture(n_components=2, random_state=1).fit(new_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:43.269552Z","iopub.execute_input":"2022-08-12T10:21:43.270145Z","iopub.status.idle":"2022-08-12T10:21:51.855131Z","shell.execute_reply.started":"2022-08-12T10:21:43.270098Z","shell.execute_reply":"2022-08-12T10:21:51.853501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_BGM =BGM.predict(new_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:51.863899Z","iopub.execute_input":"2022-08-12T10:21:51.868238Z","iopub.status.idle":"2022-08-12T10:21:51.938793Z","shell.execute_reply.started":"2022-08-12T10:21:51.868161Z","shell.execute_reply":"2022-08-12T10:21:51.937173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred_BGM)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:51.946674Z","iopub.execute_input":"2022-08-12T10:21:51.950773Z","iopub.status.idle":"2022-08-12T10:21:51.967387Z","shell.execute_reply.started":"2022-08-12T10:21:51.950679Z","shell.execute_reply":"2022-08-12T10:21:51.965653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_BGM","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:51.975400Z","iopub.execute_input":"2022-08-12T10:21:51.979836Z","iopub.status.idle":"2022-08-12T10:21:51.997745Z","shell.execute_reply.started":"2022-08-12T10:21:51.979741Z","shell.execute_reply":"2022-08-12T10:21:51.996139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DBSCAN","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import DBSCAN","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:52.005275Z","iopub.execute_input":"2022-08-12T10:21:52.006661Z","iopub.status.idle":"2022-08-12T10:21:52.014004Z","shell.execute_reply.started":"2022-08-12T10:21:52.006587Z","shell.execute_reply":"2022-08-12T10:21:52.012794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dbscan =DBSCAN().fit(new_df)\ndbscan.labels_","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:21:52.016315Z","iopub.execute_input":"2022-08-12T10:21:52.016906Z","iopub.status.idle":"2022-08-12T10:22:04.960191Z","shell.execute_reply.started":"2022-08-12T10:21:52.016859Z","shell.execute_reply":"2022-08-12T10:22:04.959045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dbscan.labels_)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:22:04.963050Z","iopub.execute_input":"2022-08-12T10:22:04.963406Z","iopub.status.idle":"2022-08-12T10:22:04.970358Z","shell.execute_reply.started":"2022-08-12T10:22:04.963375Z","shell.execute_reply":"2022-08-12T10:22:04.969320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}