{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\nfrom sklearn.cluster import KMeans\nplt.rcParams[\"figure.figsize\"] = (20,10)\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\n\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-11-28T08:59:33.330716Z","iopub.execute_input":"2022-11-28T08:59:33.331165Z","iopub.status.idle":"2022-11-28T08:59:34.958803Z","shell.execute_reply.started":"2022-11-28T08:59:33.331130Z","shell.execute_reply":"2022-11-28T08:59:34.957129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Loading The Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('../input/google-smartphone-decimeter-challenge/baseline_locations_train.csv')\ntrain_data","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:34.962778Z","iopub.execute_input":"2022-11-28T08:59:34.963405Z","iopub.status.idle":"2022-11-28T08:59:35.360195Z","shell.execute_reply.started":"2022-11-28T08:59:34.963355Z","shell.execute_reply":"2022-11-28T08:59:35.358447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.phoneName.unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:35.361792Z","iopub.execute_input":"2022-11-28T08:59:35.362174Z","iopub.status.idle":"2022-11-28T08:59:35.387080Z","shell.execute_reply.started":"2022-11-28T08:59:35.362141Z","shell.execute_reply":"2022-11-28T08:59:35.385309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:35.391119Z","iopub.execute_input":"2022-11-28T08:59:35.392172Z","iopub.status.idle":"2022-11-28T08:59:35.435235Z","shell.execute_reply.started":"2022-11-28T08:59:35.392109Z","shell.execute_reply":"2022-11-28T08:59:35.433723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['latDeg'].max()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:35.436453Z","iopub.execute_input":"2022-11-28T08:59:35.436829Z","iopub.status.idle":"2022-11-28T08:59:35.448979Z","shell.execute_reply.started":"2022-11-28T08:59:35.436796Z","shell.execute_reply":"2022-11-28T08:59:35.447078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['lngDeg'].max()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:35.450538Z","iopub.execute_input":"2022-11-28T08:59:35.450936Z","iopub.status.idle":"2022-11-28T08:59:35.466300Z","shell.execute_reply.started":"2022-11-28T08:59:35.450887Z","shell.execute_reply":"2022-11-28T08:59:35.463245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. PLot the Data","metadata":{}},{"cell_type":"code","source":"# PLot the Data\nplt.scatter(train_data['latDeg'], train_data['lngDeg'])\nplt.ylim()\nplt.xlim()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:35.468332Z","iopub.execute_input":"2022-11-28T08:59:35.468808Z","iopub.status.idle":"2022-11-28T08:59:36.052320Z","shell.execute_reply.started":"2022-11-28T08:59:35.468759Z","shell.execute_reply":"2022-11-28T08:59:36.051024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Select features","metadata":{}},{"cell_type":"markdown","source":"## 3.1. Select 2 features","metadata":{}},{"cell_type":"code","source":"#Select the features\nx = train_data.iloc[:, 3:5]\nx","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:36.053716Z","iopub.execute_input":"2022-11-28T08:59:36.054083Z","iopub.status.idle":"2022-11-28T08:59:36.073782Z","shell.execute_reply.started":"2022-11-28T08:59:36.054051Z","shell.execute_reply":"2022-11-28T08:59:36.072152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clustering\nkmeans = KMeans(10)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:36.075584Z","iopub.execute_input":"2022-11-28T08:59:36.076757Z","iopub.status.idle":"2022-11-28T08:59:36.085243Z","shell.execute_reply.started":"2022-11-28T08:59:36.076714Z","shell.execute_reply":"2022-11-28T08:59:36.084051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans.fit(x)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:36.088548Z","iopub.execute_input":"2022-11-28T08:59:36.088944Z","iopub.status.idle":"2022-11-28T08:59:39.532359Z","shell.execute_reply.started":"2022-11-28T08:59:36.088912Z","shell.execute_reply":"2022-11-28T08:59:39.531344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Clustering Results\nidentified_clusters = kmeans.fit_predict(x)\nidentified_clusters","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:39.533553Z","iopub.execute_input":"2022-11-28T08:59:39.534128Z","iopub.status.idle":"2022-11-28T08:59:43.258370Z","shell.execute_reply.started":"2022-11-28T08:59:39.534083Z","shell.execute_reply":"2022-11-28T08:59:43.257057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_clusters = train_data.copy()\ndata_with_clusters['Cluster'] = identified_clusters\ndata_with_clusters","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:43.260744Z","iopub.execute_input":"2022-11-28T08:59:43.261275Z","iopub.status.idle":"2022-11-28T08:59:43.287415Z","shell.execute_reply.started":"2022-11-28T08:59:43.261211Z","shell.execute_reply":"2022-11-28T08:59:43.285736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PLot the Data\n\nplt.scatter(train_data['latDeg'], train_data['lngDeg'], c=data_with_clusters['Cluster'], cmap='rainbow')\nplt.ylim()\nplt.xlim()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:43.289396Z","iopub.execute_input":"2022-11-28T08:59:43.289877Z","iopub.status.idle":"2022-11-28T08:59:45.917069Z","shell.execute_reply.started":"2022-11-28T08:59:43.289830Z","shell.execute_reply":"2022-11-28T08:59:45.915658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3.2 Select more features","metadata":{}},{"cell_type":"code","source":"#Select the features\nx = train_data.iloc[:, 2:6]\nx","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:45.918912Z","iopub.execute_input":"2022-11-28T08:59:45.919329Z","iopub.status.idle":"2022-11-28T08:59:45.941444Z","shell.execute_reply.started":"2022-11-28T08:59:45.919291Z","shell.execute_reply":"2022-11-28T08:59:45.939988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clustering\nkmeans2 = KMeans(5)\nkmeans2.fit(x)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:45.943157Z","iopub.execute_input":"2022-11-28T08:59:45.944543Z","iopub.status.idle":"2022-11-28T08:59:47.106446Z","shell.execute_reply.started":"2022-11-28T08:59:45.944488Z","shell.execute_reply":"2022-11-28T08:59:47.105352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Clustering Results\nidentified_clusters = kmeans2.fit_predict(x)\nidentified_clusters","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:47.107969Z","iopub.execute_input":"2022-11-28T08:59:47.108509Z","iopub.status.idle":"2022-11-28T08:59:48.459292Z","shell.execute_reply.started":"2022-11-28T08:59:47.108459Z","shell.execute_reply":"2022-11-28T08:59:48.457999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_with_clusters = train_data.copy()\ndata_with_clusters['Cluster2'] = identified_clusters\ndata_with_clusters","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:48.460987Z","iopub.execute_input":"2022-11-28T08:59:48.462240Z","iopub.status.idle":"2022-11-28T08:59:48.495161Z","shell.execute_reply.started":"2022-11-28T08:59:48.462185Z","shell.execute_reply":"2022-11-28T08:59:48.493057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PLot the Data\nplt.scatter(train_data['latDeg'], train_data['lngDeg'], c=data_with_clusters['Cluster2'], cmap='rainbow')\nplt.ylim()\nplt.xlim()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:48.497621Z","iopub.execute_input":"2022-11-28T08:59:48.498076Z","iopub.status.idle":"2022-11-28T08:59:51.381537Z","shell.execute_reply.started":"2022-11-28T08:59:48.498038Z","shell.execute_reply":"2022-11-28T08:59:51.380066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.cluster import hierarchy\nfrom scipy.cluster.hierarchy import dendrogram\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-28T08:59:51.383403Z","iopub.execute_input":"2022-11-28T08:59:51.383769Z","iopub.status.idle":"2022-11-28T08:59:51.391324Z","shell.execute_reply.started":"2022-11-28T08:59:51.383736Z","shell.execute_reply":"2022-11-28T08:59:51.389589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_data.iloc[0:500, :]\ndata","metadata":{"execution":{"iopub.status.busy":"2022-11-28T09:06:37.222187Z","iopub.execute_input":"2022-11-28T09:06:37.222634Z","iopub.status.idle":"2022-11-28T09:06:37.245454Z","shell.execute_reply.started":"2022-11-28T09:06:37.222599Z","shell.execute_reply":"2022-11-28T09:06:37.244042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Z = hierarchy.linkage(data[['latDeg','lngDeg','heightAboveWgs84EllipsoidM']], method='average')\n  \nplt.figure()\nplt.title(\"Dendrograms\")\n  \n# Dendrogram plotting using linkage matrix\ndendrogram = hierarchy.dendrogram(Z)","metadata":{"execution":{"iopub.status.busy":"2022-11-28T09:06:43.874298Z","iopub.execute_input":"2022-11-28T09:06:43.875007Z","iopub.status.idle":"2022-11-28T09:06:56.197469Z","shell.execute_reply.started":"2022-11-28T09:06:43.874966Z","shell.execute_reply":"2022-11-28T09:06:56.196204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating Dendrogram for our data\n# max_d = cut-off/ Threshold value\nmax_d = 4\n  \nZ = hierarchy.linkage(data[['latDeg','lngDeg']], method='average')\nplt.figure()\nplt.title(\"Dendrograms\")\ndendrogram = hierarchy.dendrogram(Z)\n  \n# Cutting the dendrogram at max_d\nplt.axhline(y=max_d, c='k')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T09:07:24.767883Z","iopub.execute_input":"2022-11-28T09:07:24.771993Z","iopub.status.idle":"2022-11-28T09:07:37.541003Z","shell.execute_reply.started":"2022-11-28T09:07:24.771866Z","shell.execute_reply":"2022-11-28T09:07:37.539484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating Dendrogram for our data\n# max_d = cut-off/ Threshold value\nmax_d = 4\n  \nZ = hierarchy.linkage(data[['latDeg','lngDeg','heightAboveWgs84EllipsoidM']], method='average')\nplt.figure()\nplt.title(\"Dendrograms\")\ndendrogram = hierarchy.dendrogram(Z)\n  \n# Cutting the dendrogram at max_d\nplt.axhline(y=max_d, c='k')","metadata":{"execution":{"iopub.status.busy":"2022-11-28T09:08:09.432807Z","iopub.execute_input":"2022-11-28T09:08:09.433344Z","iopub.status.idle":"2022-11-28T09:08:22.055134Z","shell.execute_reply.started":"2022-11-28T09:08:09.433289Z","shell.execute_reply":"2022-11-28T09:08:22.053778Z"},"trusted":true},"execution_count":null,"outputs":[]}]}