{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\n%matplotlib inline\nwarnings.filterwarnings('ignore')\n\nimport gc\nimport matplotlib.patches as patches\nfrom scipy import stats\nfrom scipy.stats import skew\n\nfrom plotly import tools, subplots\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\nimport plotly.express as px\npd.set_option('max_columns', 100)\n\npy.init_notebook_mode(connected=True)\nfrom plotly.offline import init_notebook_mode, iplot\ninit_notebook_mode(connected=True)\nimport plotly.graph_objs as go\n\nimport os,random, math, psutil, pickle\nfrom sklearn.preprocessing import LabelEncoder\nimport missingno as msno\n\nfrom sklearn.metrics import mean_squared_error\nimport lightgbm as lgb\nfrom sklearn.model_selection import KFold, StratifiedKFold\nfrom tqdm import tqdm\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# DATA PROCESSING\n\n# 1. Load Data\n# 2. Reduce Memory\n# 3. Average Imputation\n# 4. Conversions\n    # Convert Timestamps\n    # Convert Strings to Categorical\n    # Convert Primary Use through Label Encoder\n    # Convert Wind Direction Degrees to 16-Wind Compass\n# 5. Merge Metadata, Weather, and Train/Test DF\n# 6. Data Minification\n# 7. Create Features\n    # Create Beaufort Wind Scale\n    # Create Age Feature\n    # Create Datetime Features\n# 8. Assign DTypes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load Data\nroot = '../input/ashrae-energy-prediction/'\ntrain_df = pd.read_csv(root + 'train.csv')\nweather_train_df = pd.read_csv(root + 'weather_train.csv')\ntest_df = pd.read_csv(root + 'test.csv')\nweather_test_df = pd.read_csv(root + 'weather_test.csv')\nbuilding_meta_df = pd.read_csv(root + 'building_metadata.csv')\nsample_submission = pd.read_csv(root + 'sample_submission.csv')\n\nprint('Size of train_df data', train_df.shape)\nprint('Size of weather_train_df data', weather_train_df.shape)\nprint('Size of weather_test_df data', weather_test_df.shape)\nprint('Size of building_meta_df data', building_meta_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reduce Memory\ndef reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2\n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df\n\ntrain_df = reduce_mem_usage(train_df)\ntest_df = reduce_mem_usage(test_df)\nweather_train_df = reduce_mem_usage(weather_train_df)\nweather_test_df = reduce_mem_usage(weather_test_df)\nbuilding_meta_df = reduce_mem_usage(building_meta_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Merge Metadata, Weather, and Train/Test DF\ntemp_df = train_df[['building_id']]\ntemp_df = temp_df.merge(building_meta_df, on=['building_id'], how='left')\ndel temp_df['building_id']\ntrain_df = pd.concat([train_df, temp_df], axis=1)\n\ntemp_df = test_df[['building_id']]\ntemp_df = temp_df.merge(building_meta_df, on=['building_id'], how='left')\ndel temp_df['building_id']\ntest_df = pd.concat([test_df, temp_df], axis=1)\ndel temp_df, building_meta_df\n\ntemp_df = train_df[['site_id','timestamp']]\ntemp_df = temp_df.merge(weather_train_df, on=['site_id','timestamp'], how='left')\ndel temp_df['site_id'], temp_df['timestamp']\ntrain_df = pd.concat([train_df, temp_df], axis=1)\n\ntemp_df = test_df[['site_id','timestamp']]\ntemp_df = temp_df.merge(weather_test_df, on=['site_id','timestamp'], how='left')\ndel temp_df['site_id'], temp_df['timestamp']\ntest_df = pd.concat([test_df, temp_df], axis=1)\ndel temp_df, weather_train_df, weather_test_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Average Imputation\ndef average_imputation(df, column_name):\n    imputation = df.groupby(['timestamp'])[column_name].mean()\n    df.loc[df[column_name].isnull(), column_name] = df[df[column_name].isnull()][[column_name]].apply(lambda x: imputation[df['timestamp'][x.index]].values)\n    del imputation\n    return df\n\ntrain_df = average_imputation(train_df, 'wind_speed')\ntrain_df = average_imputation(train_df, 'wind_direction')\ntest_df = average_imputation(test_df, 'wind_speed')\ntest_df = average_imputation(test_df, 'wind_direction')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Conversions\n\n# Convert Timestamps\ntrain_df['timestamp'] = pd.to_datetime(train_df[\"timestamp\"], format='%Y-%m-%d %H:%M:%S')\ntest_df['timestamp'] = pd.to_datetime(test_df['timestamp'])\n\n# Convert Strings to Categorical\ntrain_df['primary_use'] = train_df['primary_use'].astype('category')\ntest_df['primary_use'] = test_df['primary_use'].astype('category')\n\n# Convert Primary Use through Label Encoder\nle = LabelEncoder()\ntrain_df['primary_use'] = le.fit_transform(train_df['primary_use']).astype(np.int8)\ntest_df['primary_use'] = le.fit_transform(test_df['primary_use']).astype(np.int8)\n\n# Convert Wind Direction Degrees to 16-Wind Compass\ndef degToCompass(num):\n    val = int((num / 22.5))\n    arr = [i for i in range(0, 16)]\n    return arr[(val % 16)]\n\ntrain_df['wind_direction'] = train_df['wind_direction'].apply(degToCompass)\ntest_df['wind_direction'] = test_df['wind_direction'].apply(degToCompass)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Data Minification\ntrain_df.to_pickle('train_df.pkl')\ntest_df.to_pickle('test_df.pkl')\ndel train_df, test_df\ngc.collect() # release unreferenced memory\ntrain_df = pd.read_pickle('train_df.pkl')\ntest_df = pd.read_pickle('test_df.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create Features\n\n# Create Beaufort Wind Scale Variable\nbeaufort = [(0, 0, 0.3), (1, 0.3, 1.6), (2, 1.6, 3.4), (3, 3.4, 5.5), (4, 5.5, 8), (5, 8, 10.8), (6, 10.8, 13.9), \n            (7, 13.9, 17.2), (8, 17.2, 20.8), (9, 20.8, 24.5), (10, 24.5, 28.5), (11, 28.5, 33), (12, 33, 200)]\n\nfor item in beaufort:\n    train_df.loc[(train_df['wind_speed'] >= item[1]) & (train_df['wind_speed'] < item[2]), 'beaufort_scale'] = item[0]\n    test_df.loc[(test_df['wind_speed'] >= item[1]) & (test_df['wind_speed'] < item[2]), 'beaufort_scale'] = item[0]\n    \n# Create Age Feature\ntrain_df['age'] = train_df['year_built'].max() - train_df['year_built'] + 1\ntest_df['age'] = test_df['year_built'].max() - test_df['year_built'] + 1\n\n# Create Datetime Features\ntrain_df['month_datetime'] = train_df['timestamp'].dt.month.astype(np.int8)\ntrain_df['weekofyear_datetime'] = train_df['timestamp'].dt.weekofyear.astype(np.int8)\ntrain_df['dayofyear_datetime'] = train_df['timestamp'].dt.dayofyear.astype(np.int16)\ntrain_df['hour_datetime'] = train_df['timestamp'].dt.hour.astype(np.int8)\ntrain_df['day_week'] = train_df['timestamp'].dt.dayofweek.astype(np.int8)\ntrain_df['day_month_datetime'] = train_df['timestamp'].dt.day.astype(np.int8)\ntrain_df['week_month_datetime'] = train_df['timestamp'].dt.day/7\ntrain_df['week_month_datetime'] = train_df['week_month_datetime'].apply(lambda x: math.ceil(x)).astype(np.int8)\n\ntest_df['month_datetime'] = test_df['timestamp'].dt.month.astype(np.int8)\ntest_df['weekofyear_datetime'] = test_df['timestamp'].dt.weekofyear.astype(np.int8)\ntest_df['dayofyear_datetime'] = test_df['timestamp'].dt.dayofyear.astype(np.int16)    \ntest_df['hour_datetime'] = test_df['timestamp'].dt.hour.astype(np.int8)\ntest_df['day_week'] = test_df['timestamp'].dt.dayofweek.astype(np.int8)\ntest_df['day_month_datetime'] = test_df['timestamp'].dt.day.astype(np.int8)\ntest_df['week_month_datetime'] = test_df['timestamp'].dt.day/7\ntest_df['week_month_datetime'] = test_df['week_month_datetime'].apply(lambda x: math.ceil(x)).astype(np.int8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Assign DTypes\ntrain_df['beaufort_scale'] = train_df['beaufort_scale'].astype(np.uint8)\ntrain_df[\"wind_direction\"] = train_df['wind_direction'].astype(np.uint8)\ntrain_df[\"meter\"] = train_df['meter'].astype(np.uint8)\ntrain_df[\"site_id\"] = train_df['site_id'].astype(np.uint8)\n\ntest_df['beaufort_scale'] = test_df['beaufort_scale'].astype(np.uint8)\ntest_df[\"wind_direction\"] = test_df['wind_direction'].astype(np.uint8)\ntest_df[\"meter\"] = test_df['meter'].astype(np.uint8)\ntest_df[\"site_id\"] = test_df['site_id'].astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# EXPLORATORY DATA ANALYSIS\n\n# 1. Target Distribution\n# 2. Handle Missing Values\n    # Visualize Missing Values\n    # Fill In Missing Values with -999\n# 3. Outlier Analysis\n# 4. Drop Columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Target Distribution\nplt.figure(figsize = (15,5))\ntrain_df['meter_reading'].plot()\ntrain_df.meter_reading.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Handle Missing Values\n\n# Visualize Missing Values\ntotal = train_df.isnull().sum().sort_values(ascending = False)\npercent = (train_df.isnull().sum()/train_df.isnull().count()*100).sort_values(ascending = False)\nmissing_train_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmsno.matrix(train_df.head(20000), figsize=(15, 5))\nmissing_train_data.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fill In Missing Values with -999\ntrain_df['floor_count'] = train_df['floor_count'].fillna(-999).astype(np.int16)\ntest_df['floor_count'] = test_df['floor_count'].fillna(-999).astype(np.int16)\ntrain_df['year_built'] = train_df['year_built'].fillna(-999).astype(np.int16)\ntest_df['year_built'] = test_df['year_built'].fillna(-999).astype(np.int16)\ntrain_df['age'] = train_df['age'].fillna(-999).astype(np.int16)\ntest_df['age'] = test_df['age'].fillna(-999).astype(np.int16)\ntrain_df['cloud_coverage'] = train_df['cloud_coverage'].fillna(-999).astype(np.int16)\ntest_df['cloud_coverage'] = test_df['cloud_coverage'].fillna(-999).astype(np.int16)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Outlier Analysis\nenergy_types_dict = {0: \"electricity\", 1: \"chilledwater\", 2: \"steam\", 3: \"hotwater\"}\nenergy_types = ['electricity', 'chilledwater', 'steam', 'hotwater']\n\nplt.figure(figsize=(13, 3))\ntmp_df = train_df.meter.value_counts()\ntmp_df.index = energy_types\ntmp_df.sort_values().plot(kind=\"barh\")\nplt.title(\"Most readings measure electricity\")\nplt.xlabel(\"Number of measurements\")\nplt.ylabel(\"Meter Type\")\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(13, 3))\ntmp_df = train_df.groupby(\"meter\").meter_reading.sum()\ntmp_df.index = energy_types\ntmp_df.sort_values().plot(kind=\"barh\")\nplt.title(\"Steam generation consumes the most energy\")\nplt.xlabel(\"Amount of consumed energy\")\nplt.ylabel(\"Meter Type\")\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(13, 3))\nsns.distplot(train_df.meter_reading, hist=False)\nplt.title(\"Target variable (meter_reading) is highly skewed\")\nplt.ylabel(\"Number of measurements\")\nplt.xlabel(\"Amount of consumed energy\")\nplt.xlim(0, train_df.meter_reading.max() + 100_000)\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(13, 3))\nsns.distplot(np.log1p(train_df.meter_reading))\nplt.title(\"Log Transform: Better but still skewed\")\nplt.ylabel(\"Number of measurements\")\nplt.xlabel(\"Amount of consumed energy\")\nplt.xlim(0, 12)\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(13, 3))\nfor idx in range(0,4):\n    sns.distplot(np.log1p(train_df[train_df.meter == idx].meter_reading), hist=False, label=energy_types[idx])\nplt.title(\"Log Transform: energy type distributions look relatively skewed\")\nplt.ylabel(\"Number of measurements\")\nplt.xlabel(\"Amount of consumed energy\")\nplt.legend()\nplt.xlim(0, 12)\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp_df = train_df.groupby(\"primary_use\").meter_reading.sum().sort_values()\n\nplt.figure(figsize=(13,4))\ntemp_df.plot(kind=\"barh\")\nplt.title(\"Education buildings consume by far most of energy\")\nplt.xlabel(\"Number of measurements\")\nplt.ylabel(\"Primary use\")\nplt.tight_layout()\nplt.show()\n\nplt.figure(figsize=(13,4))\ntemp_df[:-1].plot(kind=\"barh\")\nplt.title(\"Among other types, office buildings consume most energy\")\nplt.xlabel(\"Number of measurements\")\nplt.ylabel(\"Primary use w/o «Education»\")\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Drop Columns\ndel train_df[\"timestamp\"], test_df[\"timestamp\"]\n\ncategoricals = [\"site_id\", \"building_id\", \"primary_use\",  \"meter\",  \"wind_direction\"]\nnumericals = [\"square_feet\", \"year_built\", \"air_temperature\", \"cloud_coverage\", \n              \"dew_temperature\", 'precip_depth_1_hr', 'floor_count', 'beaufort_scale']\ndrop_cols = [\"sea_level_pressure\", \"wind_speed\"]\nfeat_cols = categoricals + numericals\n\ntarget = np.log1p(train_df[\"meter_reading\"])\ndel train_df[\"meter_reading\"] \ntrain_df = train_df.drop(drop_cols, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# MODEL\n\n# 1. Model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model\nparams = {'boosting_type': 'gbdt', 'objective': 'regression',\n          'metric': {'rmse'}, 'subsample_freq': 1, 'learning_rate': 0.3,\n          'bagging_freq': 5, 'num_leaves': 330, 'feature_fraction': 0.9, \n          'lambda_l1': 1, 'lambda_l2': 1}\n\nfolds = 5\nseed = 666\nshuffle = False\nkf = KFold(n_splits=folds, shuffle=shuffle, random_state=seed)\n\nmodels = []\nfor train_index, val_index in kf.split(train_df[feat_cols], train_df['building_id']):\n    train_X = train_df[feat_cols].iloc[train_index]\n    val_X = train_df[feat_cols].iloc[val_index]\n    train_y = target.iloc[train_index]\n    val_y = target.iloc[val_index]\n    lgb_train = lgb.Dataset(train_X, train_y, categorical_feature=categoricals)\n    lgb_eval = lgb.Dataset(val_X, val_y, categorical_feature=categoricals)\n    gbm = lgb.train(params, lgb_train, num_boost_round=500,\n                    valid_sets=(lgb_train, lgb_eval),\n                    early_stopping_rounds=50, verbose_eval = 50)\n    models.append(gbm)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del train_df\ngc.collect()\n\ntest_df = test_df[feat_cols]\ni = 0\nres = []\nstep_size = 50000\nfor j in tqdm(range(int(np.ceil(test_df.shape[0]/50000)))):\n    res.append(np.expm1(sum([model.predict(test_df.iloc[i:i + step_size]) for model in models]) / folds))\n    i += step_size\nres = np.concatenate(res)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission['meter_reading'] = res\nsample_submission.loc[sample_submission['meter_reading'] < 0, 'meter_reading'] = 0\nsample_submission.to_csv('submission.csv', index=False)\nsample_submission.head()","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":1}