{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sklearn","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip /kaggle/input/expedia-personalized-sort/data.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Finding important features and preparing the dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = train.loc[train['prop_id'] == 104517]\n\ndf = df.loc[df['visitor_location_country_id'] == 219]\n\ndf = df.loc[df['srch_room_count'] == 1]\n\ndf = df[['date_time', 'price_usd', 'srch_booking_window', 'srch_saturday_night_bool']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets check price column to draw some relations"},{"metadata":{"trusted":true},"cell_type":"code","source":"df['price_usd'].describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = df.loc[df['price_usd']<5584]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.plot(x='date_time',y='price_usd',figsize=(20,5))\nplt.xlabel('Date')\nplt.ylabel('Price')\nplt.title(\"Time Series Graph Of Price Of Room Based on Date-Time\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Number of users on Saturday Night and Non-Saturday Night might differ. Let's check that out"},{"metadata":{"trusted":true},"cell_type":"code","source":"a = df.loc[df['srch_saturday_night_bool']==0,'price_usd']\nb = df.loc[df['srch_saturday_night_bool']==1,'price_usd']\n\nplt.figure(figsize=(20,10))\nplt.hist(a,bins=50, alpha=0.5,label='Saturday Night')\nplt.hist(b,bins=50, alpha=0.5,label='Non-Saturday Night')\nplt.legend(loc='upper right')\nplt.xlabel('Price')\nplt.ylabel('Count')\nplt.title('Price Comparison between Non-Sat and Saturday')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Model Building\n1. Kmeans ALgorithm"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.cluster import KMeans","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = df[['price_usd','srch_booking_window','srch_saturday_night_bool']]\nn_cluster = range(5,25)\nkmeans = [KMeans(n_clusters=i).fit(train_data) for i in n_cluster]\nscores = [kmeans[i].score(train_data) for i in range(len(kmeans))]\n\nfig,ax = plt.subplots(figsize=(20,6))\nax.plot(n_cluster,scores)\nplt.xlabel('Number Of Clusters')\nplt.ylabel('Score')\nplt.title('Elbow Curve')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"Here we can see that the scores start to stabilize after 15\nSo, ideally we would take 15 score to try out at first"},{"metadata":{"trusted":true},"cell_type":"code","source":"X = train_data.reset_index(drop=True)\nkm = KMeans(n_clusters = 15)\nkm.fit(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"km.predict(X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from mpl_toolkits.mplot3d import Axes3D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(1, figsize=(12,12))\nax = Axes3D(fig, rect = [0,0,0.95,1],elev=45, azim=140)\nlabels = km.labels_\nax.scatter(X.iloc[:,0],X.iloc[:,1],X.iloc[:,2],c=labels.astype(np.float), edgecolor='r')\nax.set_xlabel('Price in USD')\nax.set_ylabel('Search booking window')\nax.set_zlabel('Search saturday night bool')\nplt.title('KMeans CLustering for Anamoly Detecttion')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nos.remove('train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.6"}},"nbformat":4,"nbformat_minor":4}