{"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-07T03:44:50.427670Z","iopub.execute_input":"2022-07-07T03:44:50.428434Z","iopub.status.idle":"2022-07-07T03:44:50.458687Z","shell.execute_reply.started":"2022-07-07T03:44:50.428303Z","shell.execute_reply":"2022-07-07T03:44:50.457439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndf=df.drop(\"id\",axis=1)\nss=pd.read_csv(\"../input/tabular-playground-series-jul-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:44:50.460846Z","iopub.execute_input":"2022-07-07T03:44:50.461587Z","iopub.status.idle":"2022-07-07T03:44:51.703560Z","shell.execute_reply.started":"2022-07-07T03:44:50.461546Z","shell.execute_reply":"2022-07-07T03:44:51.702500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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 LabelEncoder\nfrom sklearn.preprocessing import RobustScaler,PowerTransformer\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\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nfrom matplotlib.colors import ListedColormap\nfrom sklearn import metrics\nimport warnings\nimport sys\nif not sys.warnoptions:\n    warnings.simplefilter(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:45:10.004209Z","iopub.execute_input":"2022-07-07T03:45:10.004600Z","iopub.status.idle":"2022-07-07T03:45:11.151058Z","shell.execute_reply.started":"2022-07-07T03:45:10.004567Z","shell.execute_reply":"2022-07-07T03:45:11.150015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.corr()\nsns.heatmap(df.corr(), vmax=.3, center=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:48:20.374970Z","iopub.execute_input":"2022-07-07T03:48:20.375360Z","iopub.status.idle":"2022-07-07T03:48:21.438750Z","shell.execute_reply.started":"2022-07-07T03:48:20.375327Z","shell.execute_reply":"2022-07-07T03:48:21.437627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rb_scaler=RobustScaler()\nX=rb_scaler.fit_transform(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:49:02.841577Z","iopub.execute_input":"2022-07-07T03:49:02.841960Z","iopub.status.idle":"2022-07-07T03:49:02.992003Z","shell.execute_reply.started":"2022-07-07T03:49:02.841929Z","shell.execute_reply":"2022-07-07T03:49:02.990847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=3,random_state=1)\npca.fit(df)\nPCA_ds = pd.DataFrame(pca.transform(df), columns=([\"col1\",\"col2\",\"col3\"]))\nPCA_ds.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:55:01.544116Z","iopub.execute_input":"2022-07-07T03:55:01.544545Z","iopub.status.idle":"2022-07-07T03:55:02.272171Z","shell.execute_reply.started":"2022-07-07T03:55:01.544509Z","shell.execute_reply":"2022-07-07T03:55:02.271016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PCA_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:55:05.931937Z","iopub.execute_input":"2022-07-07T03:55:05.932320Z","iopub.status.idle":"2022-07-07T03:55:05.947301Z","shell.execute_reply.started":"2022-07-07T03:55:05.932288Z","shell.execute_reply":"2022-07-07T03:55:05.946094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x =PCA_ds[\"col1\"]\ny =PCA_ds[\"col2\"]\nz =PCA_ds[\"col3\"]\n#To plot\nfig = plt.figure(figsize=(10,8))\nax = fig.add_subplot(111, projection=\"3d\")\nax.scatter(x,y,z, c=\"maroon\", marker=\"o\" )\nax.set_title(\"A 3D Projection Of Data In The Reduced Dimension\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:58:48.873211Z","iopub.execute_input":"2022-07-07T03:58:48.874220Z","iopub.status.idle":"2022-07-07T03:58:50.825364Z","shell.execute_reply.started":"2022-07-07T03:58:48.874175Z","shell.execute_reply":"2022-07-07T03:58:50.824262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Elbow Method to determine the number of clusters to be formed:')\nElbow_M = KElbowVisualizer(KMeans(random_state=23), k=(4,12))\nElbow_M.fit(X)\nElbow_M.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:57:27.152992Z","iopub.execute_input":"2022-07-07T03:57:27.153407Z","iopub.status.idle":"2022-07-07T03:58:28.295902Z","shell.execute_reply.started":"2022-07-07T03:57:27.153353Z","shell.execute_reply":"2022-07-07T03:58:28.294565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntransformer = PowerTransformer()\nX=transformer.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T03:56:05.313069Z","iopub.execute_input":"2022-07-07T03:56:05.314147Z","iopub.status.idle":"2022-07-07T03:56:09.259590Z","shell.execute_reply.started":"2022-07-07T03:56:05.314088Z","shell.execute_reply":"2022-07-07T03:56:09.258315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BGM = BayesianGaussianMixture(n_components=6,covariance_type='full',random_state=1)\n# fit model and predict clusters\npreds = BGM.fit_predict(X)\nPCA_ds[\"Clusters\"] = preds\n#Adding the Clusters feature to the orignal dataframe.\ndf[\"Clusters\"]= preds","metadata":{"execution":{"iopub.status.busy":"2022-07-07T04:04:31.785791Z","iopub.execute_input":"2022-07-07T04:04:31.786165Z","iopub.status.idle":"2022-07-07T04:05:28.869499Z","shell.execute_reply.started":"2022-07-07T04:04:31.786134Z","shell.execute_reply":"2022-07-07T04:05:28.868116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(10,8))\nax = plt.subplot(111, projection='3d', label=\"bla\")\nax.scatter(x, y, z, s=40, c=PCA_ds[\"Clusters\"], marker='o' )\nax.set_title(\"The Plot Of The Clusters\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T04:05:28.876588Z","iopub.execute_input":"2022-07-07T04:05:28.880255Z","iopub.status.idle":"2022-07-07T04:05:31.314741Z","shell.execute_reply.started":"2022-07-07T04:05:28.880189Z","shell.execute_reply":"2022-07-07T04:05:31.313642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.Predicted=pd.DataFrame(preds)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T04:05:31.315945Z","iopub.execute_input":"2022-07-07T04:05:31.316263Z","iopub.status.idle":"2022-07-07T04:05:31.322172Z","shell.execute_reply.started":"2022-07-07T04:05:31.316232Z","shell.execute_reply":"2022-07-07T04:05:31.321453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pl = sns.countplot(x=df[\"Clusters\"])\npl.set_title(\"Distribution Of The Clusters\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T04:05:31.324900Z","iopub.execute_input":"2022-07-07T04:05:31.325473Z","iopub.status.idle":"2022-07-07T04:05:31.676316Z","shell.execute_reply.started":"2022-07-07T04:05:31.325429Z","shell.execute_reply":"2022-07-07T04:05:31.675244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T04:05:31.678028Z","iopub.execute_input":"2022-07-07T04:05:31.678544Z","iopub.status.idle":"2022-07-07T04:05:31.840817Z","shell.execute_reply.started":"2022-07-07T04:05:31.678493Z","shell.execute_reply":"2022-07-07T04:05:31.839513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}