{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"building=pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv')\n#weather_train=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv')\ntrain=pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"sample=pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')"},{"metadata":{"trusted":true},"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')\n#weather_test=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"features=train[['square_feet','year_built','floor_count','meter']]\n"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"weather_train['timestamp']=pd.to_datetime(weather_train.timestamp)\nweather_test['timestamp']=pd.to_datetime(weather_test.timestamp)"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"weather_train=pd.merge(building,weather_train,on='site_id')\nweather_test=pd.merge(building,weather_test,on='site_id')"},{"metadata":{"trusted":true},"cell_type":"code","source":"train=pd.merge(building,train,on='building_id',how='right')\ntest=pd.merge(building,test,on='building_id',how='right')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#d1.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['timestamp']=pd.to_datetime(train.timestamp)\ntest['timestamp']=pd.to_datetime(test.timestamp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#d2.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"weather_train['timestamp']=weather_train.timestamp.dt.date\nweather_test['timestamp']=weather_test.timestamp.dt.date"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"weather_train=weather_train.groupby('timestamp').mean().reset_index()\nweather_test=weather_test.groupby('timestamp').mean().reset_index()"},{"metadata":{"trusted":true},"cell_type":"code","source":"#d1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#d2['timestamp']=d2.timestamp.dt.date\n#d2=d2.groupby('timestamp').mean().reset_index()\n#t2['timestamp']=t2.timestamp.dt.date\n#t2=t2.groupby('timestamp').mean().reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#d2.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"weather_train = weather_train.set_index(\"timestamp\")\ntrain = train.set_index(\"timestamp\")\nweather_test = weather_test.set_index(\"timestamp\")\ntest = test.set_index(\"timestamp\")"},{"metadata":{},"cell_type":"markdown","source":"d1=d1.reindex(d2.index, method='nearest')"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"train=pd.merge(train,weather_train,how='left')\ntest=pd.merge(test,weather_test,how='left')"},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train.isnull().sum())\nprint(test.isnull().sum())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"features=train[['square_feet','meter']]\ny=train.meter_reading","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_X=test[['square_feet','meter']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nx=StandardScaler().fit_transform(features)\ntest_x=StandardScaler().fit_transform(test_X)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\nrf=RandomForestRegressor()\nrf.fit(x_train,y_train)\ny_hat=rf.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import mean_squared_log_error as msle\n(msle(y_test,y_hat))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_y=rf.predict(test_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"2.6434394121439375**0.5\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample=pd.read_csv('/kaggle/input/ashrae-energy-prediction/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['meter_reading']=test_y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission=test[['row_id','meter_reading']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('energy.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub=pd.read_csv('energy.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head()","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":1}