{"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-07T13:00:49.885359Z","iopub.execute_input":"2022-07-07T13:00:49.885859Z","iopub.status.idle":"2022-07-07T13:00:49.902084Z","shell.execute_reply.started":"2022-07-07T13:00:49.885759Z","shell.execute_reply":"2022-07-07T13:00:49.900789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.cluster import KMeans\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:00:54.453512Z","iopub.execute_input":"2022-07-07T13:00:54.454423Z","iopub.status.idle":"2022-07-07T13:00:55.104194Z","shell.execute_reply.started":"2022-07-07T13:00:54.454372Z","shell.execute_reply":"2022-07-07T13:00:55.103058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_data = pd.read_csv(\"../input/tabular-playground-series-jul-2022/data.csv\")\ndraft = draft_data.copy()\n\ndraft.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:01:22.589187Z","iopub.execute_input":"2022-07-07T13:01:22.589590Z","iopub.status.idle":"2022-07-07T13:01:23.414199Z","shell.execute_reply.started":"2022-07-07T13:01:22.589554Z","shell.execute_reply":"2022-07-07T13:01:23.411992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding null data**","metadata":{}},{"cell_type":"code","source":"draft.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:02:41.529901Z","iopub.execute_input":"2022-07-07T13:02:41.530429Z","iopub.status.idle":"2022-07-07T13:02:41.548803Z","shell.execute_reply.started":"2022-07-07T13:02:41.530387Z","shell.execute_reply":"2022-07-07T13:02:41.547540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(draft.corr());","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:03:05.049996Z","iopub.execute_input":"2022-07-07T13:03:05.050385Z","iopub.status.idle":"2022-07-07T13:03:05.714607Z","shell.execute_reply.started":"2022-07-07T13:03:05.050353Z","shell.execute_reply":"2022-07-07T13:03:05.713497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\ndraft_scaled = scaler.fit_transform(draft)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:03:32.211197Z","iopub.execute_input":"2022-07-07T13:03:32.211589Z","iopub.status.idle":"2022-07-07T13:03:32.278067Z","shell.execute_reply.started":"2022-07-07T13:03:32.211554Z","shell.execute_reply":"2022-07-07T13:03:32.276752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(0.9)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:04:59.113536Z","iopub.execute_input":"2022-07-07T13:04:59.113971Z","iopub.status.idle":"2022-07-07T13:04:59.119524Z","shell.execute_reply.started":"2022-07-07T13:04:59.113935Z","shell.execute_reply":"2022-07-07T13:04:59.118186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_pca = pca.fit_transform(draft_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:05:14.109670Z","iopub.execute_input":"2022-07-07T13:05:14.110748Z","iopub.status.idle":"2022-07-07T13:05:14.240204Z","shell.execute_reply.started":"2022-07-07T13:05:14.110710Z","shell.execute_reply":"2022-07-07T13:05:14.238879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_pca.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:05:21.713239Z","iopub.execute_input":"2022-07-07T13:05:21.713946Z","iopub.status.idle":"2022-07-07T13:05:21.722277Z","shell.execute_reply.started":"2022-07-07T13:05:21.713906Z","shell.execute_reply":"2022-07-07T13:05:21.720772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_pca","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:05:29.745817Z","iopub.execute_input":"2022-07-07T13:05:29.746202Z","iopub.status.idle":"2022-07-07T13:05:29.752722Z","shell.execute_reply.started":"2022-07-07T13:05:29.746168Z","shell.execute_reply":"2022-07-07T13:05:29.752013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_pca = pd.DataFrame(draft_pca)\ndraft_pca.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:05:56.817722Z","iopub.execute_input":"2022-07-07T13:05:56.818986Z","iopub.status.idle":"2022-07-07T13:05:56.849052Z","shell.execute_reply.started":"2022-07-07T13:05:56.818930Z","shell.execute_reply":"2022-07-07T13:05:56.848011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Kmeans**","metadata":{}},{"cell_type":"code","source":"li=[]\nk_range = range(1,10)\nfor k in k_range:\n    km = KMeans(n_clusters = k)\n    km.fit(draft_scaled)\n    li.append(km.inertia_)\n\nplt.xlabel('K')\nplt.ylabel('Sum of squared error')\nplt.plot(k_range,li)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:07:12.006238Z","iopub.execute_input":"2022-07-07T13:07:12.006632Z","iopub.status.idle":"2022-07-07T13:07:56.354486Z","shell.execute_reply.started":"2022-07-07T13:07:12.006595Z","shell.execute_reply":"2022-07-07T13:07:56.353085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"km = KMeans(n_clusters= 7)\ny = km.fit_predict(draft_pca)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:11.758052Z","iopub.execute_input":"2022-07-07T13:08:11.758494Z","iopub.status.idle":"2022-07-07T13:08:17.932451Z","shell.execute_reply.started":"2022-07-07T13:08:11.758456Z","shell.execute_reply":"2022-07-07T13:08:17.931280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:20.113414Z","iopub.execute_input":"2022-07-07T13:08:20.113841Z","iopub.status.idle":"2022-07-07T13:08:20.122568Z","shell.execute_reply.started":"2022-07-07T13:08:20.113804Z","shell.execute_reply":"2022-07-07T13:08:20.121270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draft_data['Predicted']=y","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:30.233577Z","iopub.execute_input":"2022-07-07T13:08:30.234210Z","iopub.status.idle":"2022-07-07T13:08:30.240303Z","shell.execute_reply.started":"2022-07-07T13:08:30.234170Z","shell.execute_reply":"2022-07-07T13:08:30.239109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=draft_data.loc[:,['id','Predicted']]\nsubmission.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:32.826308Z","iopub.execute_input":"2022-07-07T13:08:32.827190Z","iopub.status.idle":"2022-07-07T13:08:32.838582Z","shell.execute_reply.started":"2022-07-07T13:08:32.827128Z","shell.execute_reply":"2022-07-07T13:08:32.837732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:39.187879Z","iopub.execute_input":"2022-07-07T13:08:39.188267Z","iopub.status.idle":"2022-07-07T13:08:39.198589Z","shell.execute_reply.started":"2022-07-07T13:08:39.188233Z","shell.execute_reply":"2022-07-07T13:08:39.197577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission4.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-07T13:08:48.938135Z","iopub.execute_input":"2022-07-07T13:08:48.938532Z","iopub.status.idle":"2022-07-07T13:08:49.100809Z","shell.execute_reply.started":"2022-07-07T13:08:48.938502Z","shell.execute_reply":"2022-07-07T13:08:49.099644Z"},"trusted":true},"execution_count":null,"outputs":[]}]}