image_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

INPUT_DIR = '../input/'
TRAIN_FILE = 'train.json'
TEST_FILE = 'test.json'
train_df = pd.read_json(INPUT_DIR + TRAIN_FILE)
test_df = pd.read_json(INPUT_DIR + TEST_FILE)
all_df = pd.concat((train_df, test_df), axis=0)
all_df['train'] = all_df['interest_level'].notnull()
all_df['test'] = all_df['interest_level'].isnull()
n_train = train_df.shape[0]
n_test = test_df.shape[0]
n_total = n_train + n_test
n_train_pct = n_train / n_total * 100.0
n_test_pct = n_test / n_total * 100.0

g = sns.FacetGrid(all_df, col='test', sharex=True, sharey=False, size=5)
g = g.map(sns.distplot, 'bathrooms')

g = sns.FacetGrid(all_df[all_df['bathrooms'] < 10], col='test', sharex=True, sharey=False, size=5)
g = g.map(sns.countplot, 'bathrooms')

g = sns.FacetGrid(all_df, col='test', sharex=True, sharey=False, size=5)
g = g.map(sns.countplot, 'bedrooms')

g = sns.FacetGrid(all_df, col='train', sharex=True, sharey=True, size=5)
g = g.map(sns.distplot, 'bathrooms')

# How does the train / test split depend on the creation date?
all_agg_df = all_df.copy()
all_agg_df = all_agg_df.set_index('created', drop=True)
all_agg_df = all_agg_df.groupby('train').resample('1D').size().transpose()

fig, ax = plt.subplots(1,1, figsize=(18,10))
ax = all_agg_df.plot.bar(ax=ax, stacked=True)
ax.set_xticklabels(all_agg_df.index.strftime('%a %b %d'))
ax.set_title('All listings creation date and train/test split');

all_agg_df.head()

all_agg_df = all_df.copy()
all_agg_df['dayofweek'] = all_df['created'].dt.dayofweek
all_agg_df = all_agg_df.groupby('dayofweek').size()

all_agg_df = all_df.copy()
all_agg_df['hour'] = all_df['created'].dt.hour
all_agg_df = all_agg_df.groupby('hour').size()

# Separate line plot for day vs hour of day creation
all_agg_df = all_df.copy()

all_agg_df = all_agg_df.reset_index()
all_agg_df['dayofweek'] = all_agg_df['created'].dt.weekday_name
all_agg_df['hour'] = all_agg_df['created'].dt.hour
all_agg_df = all_agg_df.groupby(['dayofweek', 'hour']).size().reset_index(name="count")
# all_agg_df = all_agg_df[['dayofweek', 'hour', 'checkouts']]
all_agg_df = all_agg_df.pivot_table(values='count', index='hour', columns='dayofweek')

all_agg_df = all_agg_df[['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']]
day_palette = sns.color_palette("hls", 7) # Need to have 7 distinct colours

fig, ax = plt.subplots(1,1, figsize=(16,10))
all_agg_df.plot.line(ax=ax, linewidth=3, color=day_palette, title="Created by hour and day");

plot_df = train_df.copy()
plot_df['day'] = plot_df['created'].dt.dayofweek
plot_df['hour'] = plot_df['created'].dt.hour

sns.boxplot(data=plot_df, x='interest_level', y='day')