{"cells":[{"metadata":{},"cell_type":"markdown","source":"Started on `17 October 2019`"},{"metadata":{},"cell_type":"markdown","source":"# Introduction"},{"metadata":{},"cell_type":"markdown","source":"#### This notebook uses averages of meter readings to predict the energy consumptions. No ML involved."},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom itertools import product\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport datetime as dt\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":{},"cell_type":"markdown","source":"### Helper function to reduce memory usage\n* This function originates from https://www.kaggle.com/gemartin/load-data-reduce-memory-usage"},{"metadata":{"trusted":true},"cell_type":"code","source":"def reduce_mem_usage(df, force_obj_in_category=True, debug=True):\n    \"\"\" \n    Iterate through all the columns of a dataframe and modify the data type to reduce memory usage. \n    This function originates from https://www.kaggle.com/gemartin/load-data-reduce-memory-usage\n    \"\"\"\n    if debug:\n        start_mem = df.memory_usage(deep=True).sum() / 1024**2\n        print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        \n        if col_type != object and df[col].dtype.name != 'category' and 'datetime' not in col_type.name:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                for i_type in [np.int8, np.int16, np.int32, np.int64]:\n                    if c_min > np.iinfo(i_type).min and c_max < np.iinfo(i_type).max:\n                        df[col] = df[col].astype(i_type)\n                        break\n            elif str(col_type)[:4] == 'uint':\n                for i_type in [np.uint8, np.uint16, np.uint32, np.uint64]:\n                    if c_max < np.iinfo(i_type).max:\n                        df[col] = df[col].astype(i_type)\n                        break\n            elif col_type == bool:\n                df[col] = df[col].astype(np.uint8)\n            else:\n                df[col] = df[col].astype(np.float32)\n        elif force_obj_in_category and 'datetime' not in col_type.name:\n            df[col] = df[col].astype('category')\n\n    if debug:\n        end_mem = df.memory_usage(deep=True).sum() / 1024**2\n        print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n        print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load training data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# load training data from csv files\ntrain = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\ntest = pd.read_csv('../input/ashrae-energy-prediction/test.csv')\nbuilding = pd.read_csv('../input/ashrae-energy-prediction/building_metadata.csv')\nweather_train = pd.read_csv('../input/ashrae-energy-prediction/weather_train.csv')\nweather_test = pd.read_csv('../input/ashrae-energy-prediction/weather_test.csv')\nsubmission = pd.read_csv('../input/ashrae-energy-prediction/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Shape of the data:','\\n','  train_csv: ',train.shape,'\\n','  test_csv: ',test.shape,'\\n',\n      '  building_metadata.csv: ',building.shape,'\\n','  weather_train.csv: ',weather_train.shape,'\\n',\n      '  weather_test.csv: ',weather_test.shape,'\\n','  sample_submission.csv: ',submission.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = reduce_mem_usage(train)\nbuilding = reduce_mem_usage(building)\nweather_train = reduce_mem_usage(weather_train)\ntest = reduce_mem_usage(test)\nweather_test = reduce_mem_usage(weather_test)\nsubmission = reduce_mem_usage(submission)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Get the mean meter readings by meter and building"},{"metadata":{"trusted":true},"cell_type":"code","source":"train['meter_reading'] = np.log1p(train['meter_reading'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meter = train.groupby(['meter','building_id']).mean()\nmeter","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Write mean meter readings into the test data"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = test.drop('timestamp', axis=1)\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create an empty dataframe with same rows as test_df\nresult = pd.DataFrame(columns=['row_id', 'building_id', 'meter', 'meter_reading'])\nresult.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for j in meter.index:\n    temp = test_df[test_df['meter']==j[0]]\n    temp = temp[temp['building_id']==j[1]]\n    reading = meter.loc[(j[0], j[1])].values\n    temp['meter_reading'] = reading.item(0)\n    result = result.append(temp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result.sort_values('row_id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission['meter_reading'] = np.expm1(result['meter_reading'])\nsubmission.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.sample(100)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.3"}},"nbformat":4,"nbformat_minor":1}