{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from zipfile import ZipFile \n!unzip ../input/expedia-personalized-sort/data\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('train.csv',delimiter=',')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.info","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.describe","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['visitor_hist_adr_usd'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['visitor_hist_starrating'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drop_list = ['srch_id', 'date_time', 'site_id', 'visitor_location_country_id',\n       'visitor_hist_starrating', 'visitor_hist_adr_usd', 'prop_country_id',\n       'prop_id','random_bool', 'comp1_rate', 'comp1_inv',\n       'comp1_rate_percent_diff', 'comp2_rate', 'comp2_inv',\n       'comp2_rate_percent_diff', 'comp3_rate', 'comp3_inv',\n       'comp3_rate_percent_diff', 'comp4_rate', 'comp4_inv',\n       'comp4_rate_percent_diff', 'comp5_rate', 'comp5_inv',\n       'comp5_rate_percent_diff', 'comp6_rate', 'comp6_inv',\n       'comp6_rate_percent_diff', 'comp7_rate', 'comp7_inv',\n       'comp7_rate_percent_diff', 'comp8_rate', 'comp8_inv',\n       'comp8_rate_percent_diff', 'click_bool', 'gross_bookings_usd',\n       'booking_bool']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = df_train.drop(drop_list,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cols = df_train.loc[: , \"prop_location_score1\":\"prop_location_score2\"]\ndf_train['prop_location_score'] = cols.mean(axis=1)\ndf_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train['promotion_flag'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.cluster import KMeans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kmeans = KMeans(n_clusters=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drop_list_again = ['srch_query_affinity_score','orig_destination_distance']\ndf_analyze = df_train.drop(drop_list_again,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze['prop_review_score'].fillna((df_analyze['prop_review_score'].mean()),inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drop_list_new = ['prop_location_score1','prop_location_score2']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze = df_analyze.drop((df_analyze[drop_list_new]),axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_analyze.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kmeans.fit(df_analyze)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kmeans.cluster_centers_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(kmeans.labels_)\nprint(len(kmeans.labels_))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(type(kmeans.labels_))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"unique,counts = np.unique(kmeans.labels_,return_counts=True)\nprint(dict(zip(unique,counts)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\ndf_analyze['cluster'] = kmeans.labels_\nsns.set_style('whitegrid')\nsns.lmplot('prop_review_score','price_usd',data = df_analyze,hue = 'cluster',palette='coolwarm',size=6,aspect=1,fit_reg=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kmean_4 = KMeans(n_clusters=4)\nkmean_4.fit(df_analyze.drop('position',axis=1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(kmean_4.cluster_centers_)\nunique,counts = np.unique(kmean_4.labels_,return_counts=True)\nkmean_4.cluster_centers_\nprint(dict(zip(unique,counts)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\ndf_analyze['cluster'] = kmean_4.labels_\nsns.set_style('whitegrid')\nsns.lmplot('prop_review_score','price_usd',data = df_analyze,hue = 'cluster',palette='coolwarm',size=6,aspect=1,fit_reg=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kmean_8 = KMeans(n_clusters=16)\nkmean_8.fit(df_analyze)\nprint(kmean_8.cluster_centers_)\nunique,counts = np.unique(kmean_8.labels_,return_counts=True)\nkmean_8.cluster_centers_\nprint(dict(zip(unique,counts)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}