{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# imports:\nimport pandas as pd\nimport numpy as np\nimport datetime\nimport seaborn as sns\n\n%matplotlib inline\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport gc\n\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn import metrics\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\n\nfrom sklearn.preprocessing import LabelEncoder\n\n\n\nimport plotly.express as px\nimport lightgbm as lgb\nfrom sklearn.model_selection import KFold\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.ensemble import RandomForestRegressor\nfrom xgboost import XGBRegressor\nfrom lightgbm import LGBMRegressor\nfrom sklearn.metrics import mean_squared_error","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-01T10:42:11.58739Z","iopub.execute_input":"2023-06-01T10:42:11.58802Z","iopub.status.idle":"2023-06-01T10:42:16.363307Z","shell.execute_reply.started":"2023-06-01T10:42:11.587988Z","shell.execute_reply":"2023-06-01T10:42:16.362411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here, we start loading the data files and the analysis of each dataset.\nOnly the 1st 50K rows are considered for memory limitations","metadata":{}},{"cell_type":"code","source":"# loading data files:\n#train:\ntrain_dtype={'building_id':'int16','meter':'int8','meter_reading':'float32'}\ntrain_m =pd.read_csv('/kaggle/input/ashrae-energy-prediction/train.csv',dtype=train_dtype,parse_dates=['timestamp'],nrows=50000)\n#We need to drop the 0 values in the meter reading because they do not make sense\ntrain_m = train_m[train_m.meter_reading != 0]\ntrain_m['timestamp'] = pd.to_datetime(train_m['timestamp'])\ntrain_m.sample(3)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:16.365136Z","iopub.execute_input":"2023-06-01T10:42:16.365653Z","iopub.status.idle":"2023-06-01T10:42:16.501555Z","shell.execute_reply.started":"2023-06-01T10:42:16.365624Z","shell.execute_reply":"2023-06-01T10:42:16.500512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:16.503173Z","iopub.execute_input":"2023-06-01T10:42:16.503683Z","iopub.status.idle":"2023-06-01T10:42:16.541479Z","shell.execute_reply.started":"2023-06-01T10:42:16.503653Z","shell.execute_reply":"2023-06-01T10:42:16.540211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.isna().sum()/len(train_m)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:16.544192Z","iopub.execute_input":"2023-06-01T10:42:16.545211Z","iopub.status.idle":"2023-06-01T10:42:16.556832Z","shell.execute_reply.started":"2023-06-01T10:42:16.545177Z","shell.execute_reply":"2023-06-01T10:42:16.555471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-Note that the 0 value in the meter_reading doesn't make sense because power consumption will never be 0.\n-Also, there are no missing data","metadata":{}},{"cell_type":"code","source":"plt.hist(train_m['meter_reading']);\nplt.title('Target distribution plot');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:16.559316Z","iopub.execute_input":"2023-06-01T10:42:16.560874Z","iopub.status.idle":"2023-06-01T10:42:16.842352Z","shell.execute_reply.started":"2023-06-01T10:42:16.560839Z","shell.execute_reply":"2023-06-01T10:42:16.840314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"this skewness and the 0 values suggests the log transformation of log(x+1)","metadata":{}},{"cell_type":"code","source":"plt.hist(train_m['timestamp']);\nplt.title('Train data time range');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:16.844473Z","iopub.execute_input":"2023-06-01T10:42:16.846551Z","iopub.status.idle":"2023-06-01T10:42:17.142831Z","shell.execute_reply.started":"2023-06-01T10:42:16.846493Z","shell.execute_reply":"2023-06-01T10:42:17.14171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test:\ntest_dtype={'row_id':'int8','building_id':'int16','meter':'int8'}\ntest=pd.read_csv('/kaggle/input/ashrae-energy-prediction/test.csv',dtype=test_dtype,parse_dates=['timestamp'],nrows=50000)\ntest['timestamp'] = pd.to_datetime(test['timestamp'])\ntest.sample(3)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.14413Z","iopub.execute_input":"2023-06-01T10:42:17.144444Z","iopub.status.idle":"2023-06-01T10:42:17.216379Z","shell.execute_reply.started":"2023-06-01T10:42:17.144421Z","shell.execute_reply":"2023-06-01T10:42:17.214598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.217732Z","iopub.execute_input":"2023-06-01T10:42:17.218094Z","iopub.status.idle":"2023-06-01T10:42:17.239998Z","shell.execute_reply.started":"2023-06-01T10:42:17.218063Z","shell.execute_reply":"2023-06-01T10:42:17.238867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isna().sum()/len(test)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.241119Z","iopub.execute_input":"2023-06-01T10:42:17.241951Z","iopub.status.idle":"2023-06-01T10:42:17.25263Z","shell.execute_reply.started":"2023-06-01T10:42:17.24192Z","shell.execute_reply":"2023-06-01T10:42:17.251473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"No missing data","metadata":{}},{"cell_type":"code","source":"plt.hist(test['timestamp']);\nplt.title('Test data time range');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.257432Z","iopub.execute_input":"2023-06-01T10:42:17.258061Z","iopub.status.idle":"2023-06-01T10:42:17.516314Z","shell.execute_reply.started":"2023-06-01T10:42:17.258015Z","shell.execute_reply":"2023-06-01T10:42:17.51544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#building\nbuilding_dtype={'site_id':'int8','building_id':'int16','square_feet':'int32','year_built':'float16',\n            'floor_count':'float16'}\nbuilding=pd.read_csv('/kaggle/input/ashrae-energy-prediction/building_metadata.csv',dtype=building_dtype,nrows=50000)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.517716Z","iopub.execute_input":"2023-06-01T10:42:17.518236Z","iopub.status.idle":"2023-06-01T10:42:17.533731Z","shell.execute_reply.started":"2023-06-01T10:42:17.518207Z","shell.execute_reply":"2023-06-01T10:42:17.532602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note that train and test data have different time ranges.","metadata":{}},{"cell_type":"code","source":"plt.xticks(rotation=75)\nsns.histplot(building.primary_use);\nplt.title('primay_use histogram');\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.535245Z","iopub.execute_input":"2023-06-01T10:42:17.535755Z","iopub.status.idle":"2023-06-01T10:42:17.853946Z","shell.execute_reply.started":"2023-06-01T10:42:17.535725Z","shell.execute_reply":"2023-06-01T10:42:17.853114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.855251Z","iopub.execute_input":"2023-06-01T10:42:17.85571Z","iopub.status.idle":"2023-06-01T10:42:17.883419Z","shell.execute_reply.started":"2023-06-01T10:42:17.855683Z","shell.execute_reply":"2023-06-01T10:42:17.882505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"building.isna().sum()/len(building)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.884748Z","iopub.execute_input":"2023-06-01T10:42:17.885263Z","iopub.status.idle":"2023-06-01T10:42:17.895342Z","shell.execute_reply.started":"2023-06-01T10:42:17.885235Z","shell.execute_reply":"2023-06-01T10:42:17.893862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is missing data in floor_count and year_built. It's too much that it should be discarded, not filled.","metadata":{}},{"cell_type":"code","source":"plt.xticks(rotation=75)\nsns.histplot(building.primary_use);\nplt.title('Histogram of primay_use feature');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:17.896684Z","iopub.execute_input":"2023-06-01T10:42:17.897003Z","iopub.status.idle":"2023-06-01T10:42:18.20948Z","shell.execute_reply.started":"2023-06-01T10:42:17.896979Z","shell.execute_reply":"2023-06-01T10:42:18.208803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weather_test\nwtest_dtype={'site_id':'int8','air_temperature':'float16','cloud_coverage':'float16','dew_temperature':'float16','precip_depth_1_hr':'float16','sea_level_pressure':'float16','wind_direction':'float16',\n    'wind_speed':'float16'}\nweather_test=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_test.csv',dtype=wtest_dtype,nrows=50000)\nweather_test['timestamp'] = pd.to_datetime(weather_test['timestamp'])\n#weather_train.drop(['cloud_coverage','precip_depth_1_hr'], axis = 1,inplace=True)\nweather_test.sample(3)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.210636Z","iopub.execute_input":"2023-06-01T10:42:18.211086Z","iopub.status.idle":"2023-06-01T10:42:18.30646Z","shell.execute_reply.started":"2023-06-01T10:42:18.211064Z","shell.execute_reply":"2023-06-01T10:42:18.304746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#weather_train:\nwtrain_dtype={'site_id':'int8','air_temperature':'float16','cloud_coverage':'float16','dew_temperature':'float16','precip_depth_1_hr':'float16','sea_level_pressure':'float16','wind_direction':'float16',\n    'wind_speed':'float16'}\nweather_train=pd.read_csv('/kaggle/input/ashrae-energy-prediction/weather_train.csv',dtype=wtrain_dtype,nrows=50000)\nweather_train['timestamp'] = pd.to_datetime(weather_train['timestamp'])\nweather_train.sample(3)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.30769Z","iopub.execute_input":"2023-06-01T10:42:18.307994Z","iopub.status.idle":"2023-06-01T10:42:18.400303Z","shell.execute_reply.started":"2023-06-01T10:42:18.30797Z","shell.execute_reply":"2023-06-01T10:42:18.399184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.401346Z","iopub.execute_input":"2023-06-01T10:42:18.402519Z","iopub.status.idle":"2023-06-01T10:42:18.467189Z","shell.execute_reply.started":"2023-06-01T10:42:18.402477Z","shell.execute_reply":"2023-06-01T10:42:18.466184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weather_train.isna().sum()/len(weather_train)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.468365Z","iopub.execute_input":"2023-06-01T10:42:18.468965Z","iopub.status.idle":"2023-06-01T10:42:18.479197Z","shell.execute_reply.started":"2023-06-01T10:42:18.468936Z","shell.execute_reply":"2023-06-01T10:42:18.478117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note that there is missing data in all features except site_id and time stamp. The ratio is fillable.","metadata":{}},{"cell_type":"code","source":"plt.hist(weather_train.air_temperature);\nplt.title('Air temperature histogram');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.480654Z","iopub.execute_input":"2023-06-01T10:42:18.480935Z","iopub.status.idle":"2023-06-01T10:42:18.712472Z","shell.execute_reply.started":"2023-06-01T10:42:18.48091Z","shell.execute_reply":"2023-06-01T10:42:18.711264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(weather_train.dew_temperature);\nplt.title('Dew temperature histogram');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.714557Z","iopub.execute_input":"2023-06-01T10:42:18.714966Z","iopub.status.idle":"2023-06-01T10:42:18.918218Z","shell.execute_reply.started":"2023-06-01T10:42:18.714936Z","shell.execute_reply":"2023-06-01T10:42:18.916927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note that it is a bit skewed","metadata":{}},{"cell_type":"code","source":"plt.hist(weather_train.sea_level_pressure);\nplt.title('sea_level_pressure histogram');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:18.91965Z","iopub.execute_input":"2023-06-01T10:42:18.920076Z","iopub.status.idle":"2023-06-01T10:42:19.164785Z","shell.execute_reply.started":"2023-06-01T10:42:18.920022Z","shell.execute_reply":"2023-06-01T10:42:19.163793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(weather_train.wind_direction);\nplt.title('wind_direction histogram');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.165902Z","iopub.execute_input":"2023-06-01T10:42:19.166237Z","iopub.status.idle":"2023-06-01T10:42:19.397303Z","shell.execute_reply.started":"2023-06-01T10:42:19.166209Z","shell.execute_reply":"2023-06-01T10:42:19.396012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"After loading the data and having the insight, we start cleaning the dataset for the models later.","metadata":{}},{"cell_type":"code","source":"#defining a function to handle missing values as found in weather_train\ndef Missing_data_fill(data):\n  for i in data.columns:\n    if data[i].isna().sum()!=0:\n      if data[i].dtype == 'float16':\n        data[i]=data[i].astype('float32')\n      if data[i][0]==np.nan:\n        data[i][0]=data[i].mean()\n      data[i].fillna(method='ffill',inplace=True)\n  return data","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.400977Z","iopub.execute_input":"2023-06-01T10:42:19.40154Z","iopub.status.idle":"2023-06-01T10:42:19.4093Z","shell.execute_reply.started":"2023-06-01T10:42:19.401458Z","shell.execute_reply":"2023-06-01T10:42:19.40758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calling the missing data fill function to handle missing data found in weather and building datasets\nweather_train=Missing_data_fill(weather_train)\n#Dropping the missed column values from building:\nbuilding.drop(columns=['year_built','floor_count'],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.411018Z","iopub.execute_input":"2023-06-01T10:42:19.411401Z","iopub.status.idle":"2023-06-01T10:42:19.445964Z","shell.execute_reply.started":"2023-06-01T10:42:19.411378Z","shell.execute_reply":"2023-06-01T10:42:19.444617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Encoding string data into numerical values:\nencoder = LabelEncoder()\nbuilding[\"primary_use\"] = encoder.fit_transform(building[\"primary_use\"])\nbuilding.sample(3)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.447524Z","iopub.execute_input":"2023-06-01T10:42:19.447838Z","iopub.status.idle":"2023-06-01T10:42:19.461467Z","shell.execute_reply.started":"2023-06-01T10:42:19.447817Z","shell.execute_reply":"2023-06-01T10:42:19.460219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mergingsets with train and test datasets using foreign keys:\ntrain_m = pd.merge(train_m,building, on=\"building_id\", how=\"left\")\ntrain_m = train_m.merge(weather_train, on=[\"site_id\", \"timestamp\"], how=\"left\")","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.463389Z","iopub.execute_input":"2023-06-01T10:42:19.46384Z","iopub.status.idle":"2023-06-01T10:42:19.501495Z","shell.execute_reply.started":"2023-06-01T10:42:19.463801Z","shell.execute_reply":"2023-06-01T10:42:19.500805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.head()\n#del train_m,building,weather_train\n#gc.collect()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-06-01T10:42:19.506114Z","iopub.execute_input":"2023-06-01T10:42:19.506398Z","iopub.status.idle":"2023-06-01T10:42:19.522884Z","shell.execute_reply.started":"2023-06-01T10:42:19.506375Z","shell.execute_reply":"2023-06-01T10:42:19.521585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting from KBTU to kWh\ntrain_m.loc[train_m[train_m['site_id']==0].index,'meter_reading']=train_m[train_m['site_id']==0]['meter_reading']*0.2931","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.523999Z","iopub.execute_input":"2023-06-01T10:42:19.525417Z","iopub.status.idle":"2023-06-01T10:42:19.543528Z","shell.execute_reply.started":"2023-06-01T10:42:19.525363Z","shell.execute_reply":"2023-06-01T10:42:19.542018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.545694Z","iopub.execute_input":"2023-06-01T10:42:19.546424Z","iopub.status.idle":"2023-06-01T10:42:19.561692Z","shell.execute_reply.started":"2023-06-01T10:42:19.546388Z","shell.execute_reply":"2023-06-01T10:42:19.55968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Null values, so we drop rows with null values","metadata":{}},{"cell_type":"code","source":"train_m = train_m.dropna(how='any',axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.563173Z","iopub.execute_input":"2023-06-01T10:42:19.563519Z","iopub.status.idle":"2023-06-01T10:42:19.5736Z","shell.execute_reply.started":"2023-06-01T10:42:19.563488Z","shell.execute_reply":"2023-06-01T10:42:19.572187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.575094Z","iopub.execute_input":"2023-06-01T10:42:19.575467Z","iopub.status.idle":"2023-06-01T10:42:19.588432Z","shell.execute_reply.started":"2023-06-01T10:42:19.57544Z","shell.execute_reply":"2023-06-01T10:42:19.587338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Draw the scatter to detect and remove outliers","metadata":{}},{"cell_type":"code","source":"train_sample=train_m.sample(10000)\nsns.scatterplot(x=train_sample.building_id,y=train_sample.meter_reading,hue=train_sample.site_id);\ndel train_sample","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:19.589597Z","iopub.execute_input":"2023-06-01T10:42:19.589938Z","iopub.status.idle":"2023-06-01T10:42:20.491788Z","shell.execute_reply.started":"2023-06-01T10:42:19.589904Z","shell.execute_reply":"2023-06-01T10:42:20.490525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m = train_m [ train_m['meter_reading']<= 1500 ]","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:20.493083Z","iopub.execute_input":"2023-06-01T10:42:20.493356Z","iopub.status.idle":"2023-06-01T10:42:20.501291Z","shell.execute_reply.started":"2023-06-01T10:42:20.493334Z","shell.execute_reply":"2023-06-01T10:42:20.499895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sample=train_m.sample(10000)\nsns.scatterplot(x=train_sample.building_id,y=train_sample.meter_reading,hue=train_sample.site_id);\ndel train_sample","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:20.502912Z","iopub.execute_input":"2023-06-01T10:42:20.503261Z","iopub.status.idle":"2023-06-01T10:42:21.428798Z","shell.execute_reply.started":"2023-06-01T10:42:20.503233Z","shell.execute_reply":"2023-06-01T10:42:21.428024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now,feature engineering","metadata":{}},{"cell_type":"code","source":"# This function firstly converts timestamp to date and then breaks down date into 6 new columns: hour, day, dayOfWeek, dayOfYear, month and year\n\ndef breakdown_timestamp(dataframe):\n    \n    dataframe['hour']= np.uint8(dataframe['timestamp'].dt.hour)\n    \n    dataframe['day']= np.uint16(dataframe['timestamp'].dt.day)\n    dataframe['dayofweek']= np.uint8(dataframe['timestamp'].dt.dayofweek)\n    dataframe['dayofyear']= np.uint16(dataframe['timestamp'].dt.dayofyear)\n\n    dataframe['month']= np.uint8(dataframe['timestamp'].dt.month)\n\n    dataframe['year']= np.uint16(dataframe['timestamp'].dt.year)\n    \n    return dataframe","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.43016Z","iopub.execute_input":"2023-06-01T10:42:21.430665Z","iopub.status.idle":"2023-06-01T10:42:21.436754Z","shell.execute_reply.started":"2023-06-01T10:42:21.430637Z","shell.execute_reply":"2023-06-01T10:42:21.43529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m = breakdown_timestamp(train_m)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.439317Z","iopub.execute_input":"2023-06-01T10:42:21.439631Z","iopub.status.idle":"2023-06-01T10:42:21.46497Z","shell.execute_reply.started":"2023-06-01T10:42:21.439609Z","shell.execute_reply":"2023-06-01T10:42:21.463387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_m.head()\ndata = train_m.loc[:, train_m.columns != 'timestamp'] # data without timestamp\nx = data.iloc[:, data.columns != 'meter_reading'].values # train data\ny = train_m.iloc[:, 3:4].values # test data\ny=y.astype('int')\n# x\n# y\n# data\n","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.467141Z","iopub.execute_input":"2023-06-01T10:42:21.467904Z","iopub.status.idle":"2023-06-01T10:42:21.479371Z","shell.execute_reply.started":"2023-06-01T10:42:21.46787Z","shell.execute_reply":"2023-06-01T10:42:21.477947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Defining input and output target","metadata":{}},{"cell_type":"code","source":"feat_in=['building_id', 'meter','site_id','primary_use', 'square_feet', 'air_temperature','dew_temperature',\n        'sea_level_pressure', 'wind_direction','wind_speed', 'hour', 'day','weekday', 'month']\nfeat_out='meter_reading'","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.480779Z","iopub.execute_input":"2023-06-01T10:42:21.481716Z","iopub.status.idle":"2023-06-01T10:42:21.491717Z","shell.execute_reply.started":"2023-06-01T10:42:21.481681Z","shell.execute_reply":"2023-06-01T10:42:21.490374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train and test data split:","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.493168Z","iopub.execute_input":"2023-06-01T10:42:21.49364Z","iopub.status.idle":"2023-06-01T10:42:21.510738Z","shell.execute_reply.started":"2023-06-01T10:42:21.493607Z","shell.execute_reply":"2023-06-01T10:42:21.509449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nx_train = sc.fit_transform(x_train)\nx_test = sc.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.512271Z","iopub.execute_input":"2023-06-01T10:42:21.512849Z","iopub.status.idle":"2023-06-01T10:42:21.527279Z","shell.execute_reply.started":"2023-06-01T10:42:21.512809Z","shell.execute_reply":"2023-06-01T10:42:21.526435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nclassifier = GaussianNB()\nclassifier.fit(x_train, y_train.ravel())","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.528442Z","iopub.execute_input":"2023-06-01T10:42:21.529542Z","iopub.status.idle":"2023-06-01T10:42:21.604551Z","shell.execute_reply.started":"2023-06-01T10:42:21.529492Z","shell.execute_reply":"2023-06-01T10:42:21.603386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = classifier.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.60567Z","iopub.execute_input":"2023-06-01T10:42:21.606126Z","iopub.status.idle":"2023-06-01T10:42:21.963866Z","shell.execute_reply.started":"2023-06-01T10:42:21.606101Z","shell.execute_reply":"2023-06-01T10:42:21.962514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn import metrics\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y_test, y_pred)\nprint (cm)\n\nplt.figure(figsize=(12,12));\nsns.heatmap(cm, annot=True, \n            linewidths=.5, square = True, cmap = 'Blues_r', fmt='0.4g');\n\nplt.xlabel('Actual label');\nplt.ylabel('Predicted label');","metadata":{"execution":{"iopub.status.busy":"2023-06-01T10:42:21.964976Z","iopub.execute_input":"2023-06-01T10:42:21.965305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\nprint('Accuracy score: ', format(accuracy_score(y_test, y_pred)))\nprint('Precision score: ', format(precision_score(y_test, y_pred)))\nprint('Recall score: ', format(recall_score(y_test, y_pred)))\nprint('F1 score: ', format(f1_score(y_test, y_pred)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}