{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **PCA+Bayesian_gaussian_mixture**","metadata":{}},{"cell_type":"markdown","source":"# **Detailed EDA+PCA+Kmeans** - [here](https://www.kaggle.com/code/arunpurakkatt/eda-pca-kmeans-clustering/notebook)","metadata":{}},{"cell_type":"code","source":"#Importing the Libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom matplotlib import colors\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nfrom sklearn.mixture import BayesianGaussianMixture,GaussianMixture\nfrom yellowbrick.cluster import KElbowVisualizer\nfrom sklearn.metrics import silhouette_score\nfrom sklearn.cluster import KMeans\nfrom sklearn.preprocessing import RobustScaler,PowerTransformer","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:06:17.273756Z","iopub.execute_input":"2022-07-07T07:06:17.274128Z","iopub.status.idle":"2022-07-07T07:06:17.280206Z","shell.execute_reply.started":"2022-07-07T07:06:17.274083Z","shell.execute_reply":"2022-07-07T07:06:17.279328Z"},"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-07T06:25:45.893383Z","iopub.execute_input":"2022-07-07T06:25:45.893728Z","iopub.status.idle":"2022-07-07T06:25:46.945718Z","shell.execute_reply.started":"2022-07-07T06:25:45.893699Z","shell.execute_reply":"2022-07-07T06:25:46.944649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:25:48.384149Z","iopub.execute_input":"2022-07-07T06:25:48.385063Z","iopub.status.idle":"2022-07-07T06:25:48.587356Z","shell.execute_reply.started":"2022-07-07T06:25:48.385023Z","shell.execute_reply":"2022-07-07T06:25:48.586371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:25:50.869975Z","iopub.execute_input":"2022-07-07T06:25:50.87137Z","iopub.status.idle":"2022-07-07T06:25:50.881023Z","shell.execute_reply.started":"2022-07-07T06:25:50.871282Z","shell.execute_reply":"2022-07-07T06:25:50.879913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Scaling of data**","metadata":{}},{"cell_type":"code","source":"scalar = RobustScaler()\nscaled_data = pd.DataFrame(scalar.fit_transform(df)) #scaling the data\nscaled_data","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:50:29.380199Z","iopub.execute_input":"2022-07-07T06:50:29.380592Z","iopub.status.idle":"2022-07-07T06:50:29.575678Z","shell.execute_reply.started":"2022-07-07T06:50:29.380559Z","shell.execute_reply":"2022-07-07T06:50:29.574377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **PCA**","metadata":{}},{"cell_type":"code","source":"X=PCA()\npca_values=X.fit_transform(scaled_data)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:51:58.620333Z","iopub.execute_input":"2022-07-07T06:51:58.62162Z","iopub.status.idle":"2022-07-07T06:51:58.863594Z","shell.execute_reply.started":"2022-07-07T06:51:58.621568Z","shell.execute_reply":"2022-07-07T06:51:58.862174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert into data frame\npcs=pd.DataFrame(pca_values)\npcs.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:52:02.58699Z","iopub.execute_input":"2022-07-07T06:52:02.587492Z","iopub.status.idle":"2022-07-07T06:52:02.612381Z","shell.execute_reply.started":"2022-07-07T06:52:02.58745Z","shell.execute_reply":"2022-07-07T06:52:02.61108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The amount of variance of each PCA\nvar=X.explained_variance_ratio_\nvar","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:52:11.019629Z","iopub.execute_input":"2022-07-07T06:52:11.020137Z","iopub.status.idle":"2022-07-07T06:52:11.029718Z","shell.execute_reply.started":"2022-07-07T06:52:11.020075Z","shell.execute_reply":"2022-07-07T06:52:11.028591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#componenets of PCA ie weights convert as data frame\nwts = pd.DataFrame(X.components_)\nwts.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:52:19.811834Z","iopub.execute_input":"2022-07-07T06:52:19.812259Z","iopub.status.idle":"2022-07-07T06:52:19.842221Z","shell.execute_reply.started":"2022-07-07T06:52:19.812215Z","shell.execute_reply":"2022-07-07T06:52:19.841009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#find cumulative variance\nvar1=np.cumsum(np.round(var,decimals=4)*100)\nvar1","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:52:30.011452Z","iopub.execute_input":"2022-07-07T06:52:30.01181Z","iopub.status.idle":"2022-07-07T06:52:30.01905Z","shell.execute_reply.started":"2022-07-07T06:52:30.011778Z","shell.execute_reply":"2022-07-07T06:52:30.018291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(var,color='red')\nplt.plot(var1,color='blue')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:52:53.364225Z","iopub.execute_input":"2022-07-07T06:52:53.364602Z","iopub.status.idle":"2022-07-07T06:52:53.59054Z","shell.execute_reply.started":"2022-07-07T06:52:53.364573Z","shell.execute_reply":"2022-07-07T06:52:53.589404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert PCA values to Data frame\nnew_df=pd.DataFrame(pca_values[:,:])\nnew_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:53:15.45068Z","iopub.execute_input":"2022-07-07T06:53:15.45119Z","iopub.status.idle":"2022-07-07T06:53:15.487281Z","shell.execute_reply.started":"2022-07-07T06:53:15.451154Z","shell.execute_reply":"2022-07-07T06:53:15.486132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Co-relation between features after PCA\nsns.heatmap(new_df.corr())","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:53:28.932384Z","iopub.execute_input":"2022-07-07T06:53:28.933254Z","iopub.status.idle":"2022-07-07T06:53:29.644076Z","shell.execute_reply.started":"2022-07-07T06:53:28.933202Z","shell.execute_reply":"2022-07-07T06:53:29.643042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Optimal K value - Elbow method**","metadata":{}},{"cell_type":"code","source":"### Elbow Method for K means\n# Import ElbowVisualizer\nfrom yellowbrick.cluster import KElbowVisualizer\nmodel = KMeans(random_state=42)\n# k is range of number of clusters.\nvisualizer = KElbowVisualizer(model, k=(2,15), timings= True)\nvisualizer.fit(new_df)        # Fit data to visualizer\nvisualizer.show()        # Finalize and render figure","metadata":{"execution":{"iopub.status.busy":"2022-07-07T06:54:52.356272Z","iopub.execute_input":"2022-07-07T06:54:52.356816Z","iopub.status.idle":"2022-07-07T06:56:34.758305Z","shell.execute_reply.started":"2022-07-07T06:54:52.356767Z","shell.execute_reply":"2022-07-07T06:56:34.757001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **BayesianGaussianMixture**","metadata":{}},{"cell_type":"code","source":"BGM = BayesianGaussianMixture(n_components=6,covariance_type='full',random_state=42)\n# fit model and predict clusters\npred = BGM.fit_predict(new_df)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:09:54.02518Z","iopub.execute_input":"2022-07-07T07:09:54.025581Z","iopub.status.idle":"2022-07-07T07:10:51.374854Z","shell.execute_reply.started":"2022-07-07T07:09:54.025548Z","shell.execute_reply":"2022-07-07T07:10:51.373525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:11:03.030283Z","iopub.execute_input":"2022-07-07T07:11:03.030638Z","iopub.status.idle":"2022-07-07T07:11:03.038412Z","shell.execute_reply.started":"2022-07-07T07:11:03.030608Z","shell.execute_reply":"2022-07-07T07:11:03.037568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Predicted']=pred\ndf.loc[:,['id','Predicted']].head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:12:25.30294Z","iopub.execute_input":"2022-07-07T07:12:25.303351Z","iopub.status.idle":"2022-07-07T07:12:25.327777Z","shell.execute_reply.started":"2022-07-07T07:12:25.303315Z","shell.execute_reply":"2022-07-07T07:12:25.326502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#distribution of clusters\npl = sns.countplot(x=df[\"Predicted\"])\npl.set_title(\"Distribution Of The Clusters\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:12:45.797603Z","iopub.execute_input":"2022-07-07T07:12:45.798003Z","iopub.status.idle":"2022-07-07T07:12:46.018835Z","shell.execute_reply.started":"2022-07-07T07:12:45.797969Z","shell.execute_reply":"2022-07-07T07:12:46.018005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=df.loc[:,['id','Predicted']]\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:13:02.340296Z","iopub.execute_input":"2022-07-07T07:13:02.34084Z","iopub.status.idle":"2022-07-07T07:13:02.35271Z","shell.execute_reply.started":"2022-07-07T07:13:02.340792Z","shell.execute_reply":"2022-07-07T07:13:02.35142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T07:13:59.240867Z","iopub.execute_input":"2022-07-07T07:13:59.241449Z","iopub.status.idle":"2022-07-07T07:13:59.343256Z","shell.execute_reply.started":"2022-07-07T07:13:59.241395Z","shell.execute_reply":"2022-07-07T07:13:59.342213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}