{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/train.csv\")\nbuilding_info=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/building_metadata.csv\")\nweather_info_test=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_test.csv\")\ntest=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/test.csv\")\nweather_info_train=pd.read_csv(\"/kaggle/input/ashrae-energy-prediction/weather_train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_all=train.merge(building_info,on='building_id',how='left')\ntrain_all=train_all.merge(weather_info_train,on=['site_id', 'timestamp'],how='left')\ntrain_all.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"# check the correlation between the numerical independent variables and the meter_reading\ntrain_all.corr()\n# based on the correlation matrix, the following columns won't be included in the model:\n# precip_depth_1_hr, sea_level_pressure, wind_direction, dew_temperature.\n# Before dropping any columns, there are 16 columns in train_all.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# delete the columns I don't want\ntrain_all=train_all.drop(['precip_depth_1_hr','sea_level_pressure','wind_direction','dew_temperature'],axis=1)\n# There  are 12 columns after dropping 4 columns related to weather. There are \n# 20216100 rows in the train_all dataframe before dropping any rows.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# This is time series data, and we are trying to predict the meter_reading for each \n# meter type of each building. Thus, we're predicting the time series trend for each meter type in each building.\n# Based on our goal, the weird history of a meter's reading which is not representing the meter's trend and will not be repeated\n# in the future should be deleted.\n# From the EDA, we know All electricity meter is 0 until May 20 for site_id == 0. Thus,\n# data meets these conditions are deleted.\ntrain_all=train_all.drop(train_all[(train_all['building_id']<= 104) & (train_all['meter']==0) & (train_all['timestamp']<= \"2016-05-21\")].index)\n#train_df = train_df.query('not (building_id <= 104 & meter == 0 & timestamp <= \"2016-05-20\")')\n# train_all.shape\n# There are 19867540 rows after dropping those rows.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add time ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# turn each column in the dataframe to be a numpy array, because the tensorflow model doesn't deal \n# with dataframe.\ndef convert_to_tensor(s):\n    dt = s.dtype\n    if dt == \"float64\" or dt == \"int64\":\n        a = np.asarray(s).astype(\"float32\")\n        a = np.nan_to_num(a, nan=a[~np.isnan(a)].mean())\n        #return (a - a.mean()) / a.std()\n    elif dt == \"object\":\n        return s\n    return None\n# seperate the train data to training features and label\ndef df_to_dataset(dataframe, target_name):\n    data_dict = {}\n    for col in dataframe.columns:\n        t = convert_to_tensor(dataframe[col])\n        if col == target_name:\n            labels = t\n        elif t is not None:\n            data_dict[col] = t\n    ds = tf.data.Dataset.from_tensor_slices((data_dict, labels))\n    return ds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_to_dataset(train_all,'meter_reading')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test[test['building_id']==1099]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}