text_plain_output_1.png
import numpy as np

import pandas as pd

data_path = '../input/'
train_file = data_path + 'train.json'
test_file = data_path + 'test.json'
train_df = pd.read_json(train_file)
test_df = pd.read_json(test_file)

features_to_use = ['bathrooms', 'bedrooms', 'latitude', 'longitude', 'price']

train_df['num_photos'] = train_df['photos'].apply(len)
test_df['num_photos'] = test_df['photos'].apply(len)
train_df['num_features'] = train_df['features'].apply(len)
test_df['num_features'] = test_df['features'].apply(len)
train_df['num_description_words'] = train_df['description'].apply(lambda x: len(x.split(' ')))
test_df['num_description_words'] = test_df['description'].apply(lambda x: len(x.split(' ')))
train_df['created'] = pd.to_datetime(train_df['created'])
test_df['created'] = pd.to_datetime(test_df['created'])
train_df['created_year'] = train_df['created'].dt.year
test_df['created_year'] = test_df['created'].dt.year
train_df['created_month'] = train_df['created'].dt.month
test_df['created_month'] = test_df['created'].dt.month
train_df['created_day'] = train_df['created'].dt.day
test_df['created_day'] = test_df['created'].dt.day
train_df['created_hour'] = train_df['created'].dt.hour
test_df['created_hour'] = test_df['created'].dt.hour
train_df['price_per_bed'] = train_df['price'] / train_df['bedrooms'].clip(lower=1)
test_df['price_per_bed'] = test_df['price'] / test_df['bedrooms'].clip(lower=1)
train_df['created_date'] = np.array(train_df.created.values, dtype='datetime64[D]').astype(np.float32)
test_df['created_date'] = np.array(test_df.created.values, dtype='datetime64[D]').astype(np.float32)
features_to_use.extend(['num_photos', 'num_features', 'num_description_words', 'created_year', 'created_month', 'created_day', 'listing_id', 'created_hour', 'price_per_bed', 'created_date'])

train_df.head()