# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# #### 1. Importing libraries and data

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import os
import sklearn
from sklearn.cluster import KMeans # Here is where you import the k-means algorithm from scikit-learn.
import pylab as pl # PyLab is a convenience module that bulk imports matplotlib.
import openpyxl

# +
# This option ensures the graphs you create are displayed in your notebook without the need to "call" them specifically.

# %matplotlib inline
# -

path = r'C:\Users\

df_airbnb = pd.read_csv(os.path.join(path, '2 - Airbnb - Data', 'Prepared Data', 'df_airbnb_v2_rev.csv'))

# df_airbnb.dtypes

df_airbnb.columns

# Dropping unnamed column
df_airbnb1 = df_airbnb.drop(columns = ['Unnamed: 0','Unnamed: 0.1'])

# #### 2. Elbow technique

num_cl = range(1, 10) # Defines the range of potential clusters in the data.
kmeans = [KMeans(n_clusters=i) for i in num_cl] # Defines k-means clusters in the range assigned above.

# +
score = [kmeans[i].fit(df_airbnb2).score(df_airbnb2) for i in range(len(kmeans))] # Creates a score that represents 
# a rate of variation for the given cluster option.

score

# +
# Plot the elbow curve using PyLab.

pl.plot(num_cl,score)
pl.xlabel('Number of Clusters')
pl.ylabel('Score')
pl.title('Elbow Curve')
pl.show()
# -

# #####  In the Airbnb dataset, there's a large jump from two to three on the x-axis, but after that, the curve straightens out. This means that the optimal count for your clusters is three.

# +
# Create the k-means object.

kmeans = KMeans(n_clusters = 3, n_jobs = -1) 

# +
# Fit the k-means object to the data.

kmeans.fit(df_airbnb2)
# -

df_airbnb2['clusters'] = kmeans.fit_predict(df_airbnb2)

df_airbnb2.head()

df_airbnb2['clusters'].value_counts()

# +
# Plot the clusters for the "price" and "min nights" variables.

plt.figure(figsize=(12,8))
ax = sns.scatterplot(x=df_airbnb2['min_nights'], y=df_airbnb2['price'], hue=kmeans.labels_, s=100) 
# Here, you're subsetting `X` for the x and y arguments to avoid using their labels. 
# `hue` takes the value of the attribute `kmeans.labels_`, which is the result of running the k-means algorithm.
# `s` represents the size of the points you want to see in the plot.

ax.grid(False) # This removes the grid from the background.
plt.xlabel('minimum nights') # Label x-axis.
plt.ylabel('price per night') # Label y-axis.
plt.show()
# -

# #### There is no visible linear connection between the variables. Most of the dots are concentrated between the values 0 and 150, meaning that most of the bookings happens for shorter period of time (confirming that airbnb is mostly used for shor stay, even if there is a pattern of stays lasting more than 1 month)

# +
# Plot the clusters for the "nr reviews" and "price" variables.

plt.figure(figsize=(12,8))
ax = sns.scatterplot(x=df_airbnb2['nr_reviews'], y=df_airbnb2['price'], hue=kmeans.labels_, s=100)

ax.grid(False) 
plt.xlabel('number of reviews') 
plt.ylabel('price') 
plt.show()
# -

df_airbnb2.loc[df_airbnb2['clusters'] == 2, 'cluster'] = 'dark purple'
df_airbnb2.loc[df_airbnb2['clusters'] == 1, 'cluster'] = 'purple'
df_airbnb2.loc[df_airbnb2['clusters'] == 0, 'cluster'] = 'pink'

df_airbnb2.groupby('cluster').agg({'price':['mean', 'median'], 
                         'min_nights':['mean', 'median'], 
                         'nr_reviews':['mean', 'median'],
                          'availability_365':['mean', 'median']})

# ##### all the cluster do have a minimum stay of 6 nights and an average price above 65 euros. the mean availability_365 between the cluster is between 86 and 132, but if we look at the median of the dark and pink cluster the median is "0", meaning that there are a lot of accomodations published online, but not available.

# ##### Since the listings and data have been collected in March 2021, it might be that due to the pandemic the accomodation description was still online, but the calender not available due to the local lockdown restrictions.

df_airbnb2.to_excel(os.path.join(path,'2 - Airbnb - Data','Prepared Data','berlin_cane_cluster.xlsx'))



StarcoderdataJupyterScriptsDedupFiltered