{"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":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom scipy import stats\nfrom sklearn import metrics as mtr\nfrom sklearn.cluster import KMeans\nfrom sklearn.preprocessing import RobustScaler,PowerTransformer","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-31T15:14:45.808971Z","iopub.execute_input":"2022-07-31T15:14:45.809494Z","iopub.status.idle":"2022-07-31T15:14:47.259781Z","shell.execute_reply.started":"2022-07-31T15:14:45.809383Z","shell.execute_reply":"2022-07-31T15:14:47.258507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing Data","metadata":{}},{"cell_type":"code","source":"data= pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:14:51.573056Z","iopub.execute_input":"2022-07-31T15:14:51.573465Z","iopub.status.idle":"2022-07-31T15:14:52.674632Z","shell.execute_reply.started":"2022-07-31T15:14:51.573429Z","shell.execute_reply":"2022-07-31T15:14:52.673320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Scaling Data","metadata":{}},{"cell_type":"code","source":"X = data.copy()\nX.drop(['id'],axis=1,inplace=True)\n\nr=RobustScaler(with_centering=True, \nwith_scaling=True, \nquantile_range=(25.0, 75.0), \ncopy=True,).fit_transform(X)\nX=pd.DataFrame(r,columns=X.columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:14:58.321082Z","iopub.execute_input":"2022-07-31T15:14:58.321697Z","iopub.status.idle":"2022-07-31T15:14:58.500866Z","shell.execute_reply.started":"2022-07-31T15:14:58.321643Z","shell.execute_reply":"2022-07-31T15:14:58.500033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finding the optimal number of clusters","metadata":{}},{"cell_type":"code","source":"from yellowbrick.cluster import KElbowVisualizer\nvisualizer = KElbowVisualizer(KMeans(), k=(2,12))\nvisualizer.fit(X)\nvisualizer.show()  ","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:15:08.626607Z","iopub.execute_input":"2022-07-31T15:15:08.627021Z","iopub.status.idle":"2022-07-31T15:16:27.809349Z","shell.execute_reply.started":"2022-07-31T15:15:08.626985Z","shell.execute_reply":"2022-07-31T15:16:27.808211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using KMeans","metadata":{}},{"cell_type":"code","source":"model = KMeans(n_clusters=5, random_state=0).fit(X)\npred = model.predict(X)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:17:01.809061Z","iopub.execute_input":"2022-07-31T15:17:01.809629Z","iopub.status.idle":"2022-07-31T15:17:06.575758Z","shell.execute_reply.started":"2022-07-31T15:17:01.809588Z","shell.execute_reply":"2022-07-31T15:17:06.572296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':data['id'],'Predicted':pred})\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:17:09.925299Z","iopub.execute_input":"2022-07-31T15:17:09.925735Z","iopub.status.idle":"2022-07-31T15:17:09.943069Z","shell.execute_reply.started":"2022-07-31T15:17:09.925696Z","shell.execute_reply":"2022-07-31T15:17:09.941915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = 'submission.csv'\nsubmission.to_csv(filename,index=False)\nprint('Saved file: ' + filename)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T15:17:13.864983Z","iopub.execute_input":"2022-07-31T15:17:13.865884Z","iopub.status.idle":"2022-07-31T15:17:13.971713Z","shell.execute_reply.started":"2022-07-31T15:17:13.865830Z","shell.execute_reply":"2022-07-31T15:17:13.970466Z"},"trusted":true},"execution_count":null,"outputs":[]}]}