{"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.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import PCA","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-07T11:12:18.663802Z","iopub.execute_input":"2022-07-07T11:12:18.664268Z","iopub.status.idle":"2022-07-07T11:12:18.670674Z","shell.execute_reply.started":"2022-07-07T11:12:18.664211Z","shell.execute_reply":"2022-07-07T11:12:18.669468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Import Data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/tabular-playground-series-jul-2022/data.csv')\nsample_submission = pd.read_csv('../input/tabular-playground-series-jul-2022/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:18.679384Z","iopub.execute_input":"2022-07-07T11:12:18.680494Z","iopub.status.idle":"2022-07-07T11:12:19.555081Z","shell.execute_reply.started":"2022-07-07T11:12:18.680445Z","shell.execute_reply":"2022-07-07T11:12:19.553802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:19.558855Z","iopub.execute_input":"2022-07-07T11:12:19.560174Z","iopub.status.idle":"2022-07-07T11:12:19.570846Z","shell.execute_reply.started":"2022-07-07T11:12:19.560129Z","shell.execute_reply":"2022-07-07T11:12:19.569619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Information about given Data","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:19.572498Z","iopub.execute_input":"2022-07-07T11:12:19.572944Z","iopub.status.idle":"2022-07-07T11:12:19.603521Z","shell.execute_reply.started":"2022-07-07T11:12:19.572898Z","shell.execute_reply":"2022-07-07T11:12:19.601814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:19.607010Z","iopub.execute_input":"2022-07-07T11:12:19.607610Z","iopub.status.idle":"2022-07-07T11:12:19.629959Z","shell.execute_reply.started":"2022-07-07T11:12:19.607557Z","shell.execute_reply":"2022-07-07T11:12:19.628829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:19.631783Z","iopub.execute_input":"2022-07-07T11:12:19.632246Z","iopub.status.idle":"2022-07-07T11:12:19.648451Z","shell.execute_reply.started":"2022-07-07T11:12:19.632179Z","shell.execute_reply":"2022-07-07T11:12:19.647571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is no any null value in given data, Hence data preprocessing is not required","metadata":{"execution":{"iopub.status.busy":"2022-07-06T05:23:39.362798Z","iopub.execute_input":"2022-07-06T05:23:39.363223Z","iopub.status.idle":"2022-07-06T05:23:39.371436Z","shell.execute_reply.started":"2022-07-06T05:23:39.363187Z","shell.execute_reply":"2022-07-06T05:23:39.369551Z"}}},{"cell_type":"markdown","source":"## 3. EDA","metadata":{}},{"cell_type":"markdown","source":"Let's chek correlation heatmap","metadata":{"execution":{"iopub.status.busy":"2022-07-07T05:35:18.215656Z","iopub.execute_input":"2022-07-07T05:35:18.216146Z","iopub.status.idle":"2022-07-07T05:35:18.224849Z","shell.execute_reply.started":"2022-07-07T05:35:18.216104Z","shell.execute_reply":"2022-07-07T05:35:18.223221Z"}}},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nsns.heatmap(df.corr().round(1),annot=True,annot_kws={\"size\":10})","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:19.649809Z","iopub.execute_input":"2022-07-07T11:12:19.650097Z","iopub.status.idle":"2022-07-07T11:12:22.964319Z","shell.execute_reply.started":"2022-07-07T11:12:19.650070Z","shell.execute_reply":"2022-07-07T11:12:22.963278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To plot multiple pairwise bivariate distributions in a dataset, we can use the pairplot() function. This shows the relationship for (n, 2) combination of variable in a DataFrame as a matrix of plots and the diagonal plots are the univariate plots.","metadata":{}},{"cell_type":"code","source":"#sns.pairplot(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:22.965795Z","iopub.execute_input":"2022-07-07T11:12:22.966474Z","iopub.status.idle":"2022-07-07T11:12:22.970350Z","shell.execute_reply.started":"2022-07-07T11:12:22.966440Z","shell.execute_reply":"2022-07-07T11:12:22.969565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Standardize the data","metadata":{}},{"cell_type":"code","source":"scale = StandardScaler()\nscaled = scale.fit_transform(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:22.971722Z","iopub.execute_input":"2022-07-07T11:12:22.972265Z","iopub.status.idle":"2022-07-07T11:12:23.044422Z","shell.execute_reply.started":"2022-07-07T11:12:22.972215Z","shell.execute_reply":"2022-07-07T11:12:23.043571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scaled = pd.DataFrame(scaled,columns=df.columns)\ndf_scaled.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:23.045615Z","iopub.execute_input":"2022-07-07T11:12:23.046091Z","iopub.status.idle":"2022-07-07T11:12:23.071244Z","shell.execute_reply.started":"2022-07-07T11:12:23.046060Z","shell.execute_reply":"2022-07-07T11:12:23.070018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. K-Means Clustering","metadata":{}},{"cell_type":"markdown","source":"Elbow Method\nWCSS is the sum of squared distance between each point and the centroid in a cluster.\nWhen we plot the WCSS with the No. of clusters, the plot looks like an Elbow.\nAs the number of clusters increases, the WCSS value will start to decrease.\nWCSS value is largest when K = 1.","metadata":{}},{"cell_type":"code","source":"a = []\nx = range(1,10)\nfor i in x:\n    k = KMeans(n_clusters=i)\n    k.fit(df_scaled)\n    a.append(k.inertia_)\n    \nplt.plot(x,a,marker='*')\nplt.ylabel('WCSS')\nplt.xlabel('No. of clusters')\nplt.title('Elbow')","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:12:23.075415Z","iopub.execute_input":"2022-07-07T11:12:23.075744Z","iopub.status.idle":"2022-07-07T11:13:09.564493Z","shell.execute_reply.started":"2022-07-07T11:12:23.075715Z","shell.execute_reply":"2022-07-07T11:13:09.563397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The KElbowVisualizer implements the “elbow” method to help select the optimal number of clusters by fitting the model with a range of values.","metadata":{}},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\n\nmodel = KMeans(random_state=42)\nvisualizer = KElbowVisualizer(model, k=(4,12))\n\nvisualizer.fit(df_scaled)        # Fit the data to the visualizer\nvisualizer.show()   ","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:13:09.565681Z","iopub.execute_input":"2022-07-07T11:13:09.566009Z","iopub.status.idle":"2022-07-07T11:14:15.346390Z","shell.execute_reply.started":"2022-07-07T11:13:09.565978Z","shell.execute_reply":"2022-07-07T11:14:15.345124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As per elbow method value of k is 7","metadata":{}},{"cell_type":"code","source":"#Modelling\nmodel = KMeans(n_clusters=7)\nmodel.fit(df_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:14:15.348474Z","iopub.execute_input":"2022-07-07T11:14:15.349433Z","iopub.status.idle":"2022-07-07T11:14:22.880987Z","shell.execute_reply.started":"2022-07-07T11:14:15.349384Z","shell.execute_reply":"2022-07-07T11:14:22.879895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(model.labels_).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:14:22.882503Z","iopub.execute_input":"2022-07-07T11:14:22.882900Z","iopub.status.idle":"2022-07-07T11:14:22.892101Z","shell.execute_reply.started":"2022-07-07T11:14:22.882868Z","shell.execute_reply":"2022-07-07T11:14:22.891026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.fit_predict(df_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:14:22.893504Z","iopub.execute_input":"2022-07-07T11:14:22.893843Z","iopub.status.idle":"2022-07-07T11:14:29.673585Z","shell.execute_reply.started":"2022-07-07T11:14:22.893814Z","shell.execute_reply":"2022-07-07T11:14:29.672580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components = 2)\npca_data = pca.fit_transform(df_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:14:29.675006Z","iopub.execute_input":"2022-07-07T11:14:29.675367Z","iopub.status.idle":"2022-07-07T11:14:30.525728Z","shell.execute_reply.started":"2022-07-07T11:14:29.675334Z","shell.execute_reply":"2022-07-07T11:14:30.524191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df = pd.DataFrame({\n    \"x\": pca_data[:,0],\n    \"y\": pca_data[:,1],\n    \"clusters\" : pred\n})","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:14:30.527663Z","iopub.execute_input":"2022-07-07T11:14:30.528288Z","iopub.status.idle":"2022-07-07T11:14:30.543763Z","shell.execute_reply.started":"2022-07-07T11:14:30.528239Z","shell.execute_reply":"2022-07-07T11:14:30.541890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,9))\nsns.scatterplot(x=new_df['x'],y=new_df['y'],hue=new_df[\"clusters\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:15:08.528841Z","iopub.execute_input":"2022-07-07T11:15:08.529268Z","iopub.status.idle":"2022-07-07T11:15:11.838924Z","shell.execute_reply.started":"2022-07-07T11:15:08.529215Z","shell.execute_reply":"2022-07-07T11:15:11.837676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Predicted'] = pred","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:15:16.287092Z","iopub.execute_input":"2022-07-07T11:15:16.287500Z","iopub.status.idle":"2022-07-07T11:15:16.292896Z","shell.execute_reply.started":"2022-07-07T11:15:16.287463Z","shell.execute_reply":"2022-07-07T11:15:16.292094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[:,['id','Predicted']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:15:27.938409Z","iopub.execute_input":"2022-07-07T11:15:27.938935Z","iopub.status.idle":"2022-07-07T11:15:27.954607Z","shell.execute_reply.started":"2022-07-07T11:15:27.938889Z","shell.execute_reply":"2022-07-07T11:15:27.953424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6. Create Submission file","metadata":{}},{"cell_type":"code","source":"submission = df.loc[:,['id','Predicted']]","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:15:33.496118Z","iopub.execute_input":"2022-07-07T11:15:33.496908Z","iopub.status.idle":"2022-07-07T11:15:33.502658Z","shell.execute_reply.started":"2022-07-07T11:15:33.496871Z","shell.execute_reply":"2022-07-07T11:15:33.501678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('Submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T11:15:34.597788Z","iopub.execute_input":"2022-07-07T11:15:34.598570Z","iopub.status.idle":"2022-07-07T11:15:34.761073Z","shell.execute_reply.started":"2022-07-07T11:15:34.598532Z","shell.execute_reply":"2022-07-07T11:15:34.760246Z"},"trusted":true},"execution_count":null,"outputs":[]}]}