{"cells":[{"metadata":{},"cell_type":"markdown","source":"# ASHRAE GREAT ENERGY PREDICTOR III"},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport plotly\n%matplotlib inline\nfrom pandas import concat, DataFrame\nfrom math import sqrt\nfrom numpy import concatenate\nfrom matplotlib import pyplot\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fix random seed for reproducibility\nnp.random.seed(6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.rcParams[\"figure.figsize\"]=(10,5)\n# To avoid conflict between pandas and prophet\npd.plotting.register_matplotlib_converters()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# IMPORT DATA"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train data \ntrain1=pd.read_csv(\"../input/ashrae-energy-prediction/train.csv\",index_col=\"timestamp\",parse_dates=True,squeeze=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#test data\ntest1=pd.read_csv(\"../input/ashrae-energy-prediction/test.csv\",index_col=\"timestamp\",parse_dates=True,squeeze=True)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# Top 2 rows of data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train1.head(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test1.head(2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# FEATURES"},{"metadata":{"trusted":true},"cell_type":"code","source":"train1.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test1.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# Check Null Values"},{"metadata":{"trusted":true},"cell_type":"code","source":"train1.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test1.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# Descriptive Statistics"},{"metadata":{"trusted":true},"cell_type":"code","source":"train1.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Correlation of train attributes\ncorr_train=train1.corr(method=\"spearman\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#correlation heatmap plot between features in train data\ncorr_train.style.background_gradient(cmap='viridis').set_precision(2)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# PLOTS BY METER TYPE"},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"# Count per meter type in train data\nsns.countplot(\"meter\",data=train1)\nplt.title(\"NUMBER OF METERS PER METER TYPE N TRAIN DATA\")\nplt.xlabel(\"METER TYPE\")\nplt.ylabel(\"COUNT PER METER TYPE\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"# Count per meter type in test data\nsns.countplot(\"meter\",data=test1)\nplt.title(\"NUMBER OF METERS PER METER TYPE N TEST DATA\")\nplt.xlabel(\"METER TYPE\")\nplt.ylabel(\"COUNT PER METER TYPE\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"# Time series data with plots"},{"metadata":{"trusted":true},"cell_type":"code","source":"meter_t0,meter_t1,meter_t2,meter_t3=train1[train1[\"meter\"]==0],train1[train1[\"meter\"]==1],train1[train1[\"meter\"]==2],train1[train1[\"meter\"]==3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meter_e0,meter_e1,meter_e2,meter_e3=test1[test1[\"meter\"]==0],test1[test1[\"meter\"]==1],test1[test1[\"meter\"]==2],test1[test1[\"meter\"]==3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Meter type to meter reading plots\nmeter_types=(meter_t0,meter_t1,meter_t2,meter_t3)\nfor meter_type in meter_types:\n    plt.plot(meter_type.index,meter_type.meter_reading)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plotting time series data after grouping\ntypes=(0,1,2,3)\nmeters=train1.groupby(\"meter\")\nmeters=meters[\"meter_reading\"].resample(\"MS\").mean()\nfor meter in types:\n    plt.title(meter)\n    meters[meter].plot()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train=pd.DataFrame(train1[[\"meter\",\"building_id\"]])\nx_test=pd.DataFrame(test1[[\"meter\",\"building_id\"]])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train=pd.DataFrame(train1[\"meter_reading\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Import and use random forest\nfrom sklearn.ensemble import RandomForestRegressor as RF","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model Fit and Predict\nregressor = RF(n_estimators=10,\n             criterion='mse',\n             max_features= None, \n             max_depth = 14,bootstrap=True)\nregressor=regressor.fit(x_train, y_train.values.ravel())\nregressor.fit(x_train, y_train.values.ravel())\ny_pred = regressor.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred1=pd.DataFrame(y_pred)\ny_pred1.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred1=y_pred1.reset_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Top rows after resetting index\ny_pred1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred1.rename(columns={\"index\":\"row_id\",\n                       0:\"meter_reading\"},inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Final Submmission\ny_pred1.to_csv(\"SubmissionA.csv\",index=False)","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.7.4"}},"nbformat":4,"nbformat_minor":1}