{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn import linear_model\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error,mean_absolute_error\nimport matplotlib.pyplot as plt\nimport datetime\nfrom sklearn import metrics\nimport seaborn as sns\nimport sklearn.ensemble as ske\nimport gc\nfrom sklearn.linear_model import LinearRegression,SGDRegressor,ElasticNet,Ridge# 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)\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\nDATA_PATH = \"../input/ashrae-energy-prediction/\"\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-12T18:04:44.041245Z","iopub.execute_input":"2022-01-12T18:04:44.041996Z","iopub.status.idle":"2022-01-12T18:04:44.053366Z","shell.execute_reply.started":"2022-01-12T18:04:44.041955Z","shell.execute_reply":"2022-01-12T18:04:44.052504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Importing data**","metadata":{}},{"cell_type":"code","source":"#Loading data\nWeather_Train = pd.read_csv(DATA_PATH + 'weather_train.csv')\nTrain= pd.read_csv(DATA_PATH +'train.csv')\nBuilding= pd.read_csv(DATA_PATH +'building_metadata.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:05:11.237249Z","iopub.execute_input":"2022-01-12T18:05:11.238650Z","iopub.status.idle":"2022-01-12T18:05:20.934222Z","shell.execute_reply.started":"2022-01-12T18:05:11.238584Z","shell.execute_reply":"2022-01-12T18:05:20.933269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Memory usage reduction**","metadata":{}},{"cell_type":"code","source":"#Function That reduces the used memory\ndef reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().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:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n        else:\n            df[col] = df[col].astype('category')\n\n    end_mem = df.memory_usage().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    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:17.039695Z","iopub.execute_input":"2022-01-12T18:06:17.039982Z","iopub.status.idle":"2022-01-12T18:06:17.052455Z","shell.execute_reply.started":"2022-01-12T18:06:17.039952Z","shell.execute_reply":"2022-01-12T18:06:17.051313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reducing the needed data's memory usage\nTrain = reduce_mem_usage(Train)\nWeather_Train = reduce_mem_usage(Weather_Train)\nBuilding = reduce_mem_usage(Building)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:21.953310Z","iopub.execute_input":"2022-01-12T18:06:21.955026Z","iopub.status.idle":"2022-01-12T18:06:24.289832Z","shell.execute_reply.started":"2022-01-12T18:06:21.954974Z","shell.execute_reply":"2022-01-12T18:06:24.288985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merging tables\nresults = Building.merge(Train,left_on='building_id',right_on='building_id',how='left')\ndata = results.merge(Weather_Train,left_on=['site_id','timestamp'],right_on=['site_id','timestamp'],how='left')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:28.541223Z","iopub.execute_input":"2022-01-12T18:06:28.541850Z","iopub.status.idle":"2022-01-12T18:06:35.860944Z","shell.execute_reply.started":"2022-01-12T18:06:28.541812Z","shell.execute_reply":"2022-01-12T18:06:35.859430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add and Drop Features\ndata = data.drop(columns=['year_built', 'floor_count', 'wind_direction', 'dew_temperature'])\n\n#Function that is used to clean memory from deleted data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:38.139187Z","iopub.execute_input":"2022-01-12T18:06:38.139447Z","iopub.status.idle":"2022-01-12T18:06:39.197160Z","shell.execute_reply.started":"2022-01-12T18:06:38.139420Z","shell.execute_reply":"2022-01-12T18:06:39.196252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fixing timestamp and taking only the day and the month and then dropping timestamp column\ndata[\"timestamp\"] = pd.to_datetime(data[\"timestamp\"])\ndata[\"day\"]= data[\"timestamp\"].dt.day\ndata[\"month\"]= data[\"timestamp\"].dt.month\ndata= data.drop(\"timestamp\", axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:43.177085Z","iopub.execute_input":"2022-01-12T18:06:43.177340Z","iopub.status.idle":"2022-01-12T18:06:52.105528Z","shell.execute_reply.started":"2022-01-12T18:06:43.177313Z","shell.execute_reply":"2022-01-12T18:06:52.104713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Change data type to float 32 for filling NA value before transforming them into int for smooth modeling processing\ndata['wind_speed'] = data['wind_speed'].astype('float32')\ndata['air_temperature'] = data['air_temperature'].astype('float32')\ndata['precip_depth_1_hr'] = data['precip_depth_1_hr'].astype('float32')\ndata['cloud_coverage'] = data['cloud_coverage'].astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:54.600453Z","iopub.execute_input":"2022-01-12T18:06:54.601137Z","iopub.status.idle":"2022-01-12T18:06:55.178681Z","shell.execute_reply.started":"2022-01-12T18:06:54.601090Z","shell.execute_reply":"2022-01-12T18:06:55.178011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filling Null Values\ndata['precip_depth_1_hr'].fillna(data['precip_depth_1_hr'].mean(), inplace = True)\ndata['cloud_coverage'].fillna(data['cloud_coverage'].mean(), inplace = True)\ndata['wind_speed'].fillna(data['wind_speed'].mean(), inplace=True)\ndata['air_temperature'].fillna(data['air_temperature'].mean(), inplace=True)\n\n# Printing the sum of nulls inside the columns\ndata.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:06:56.938855Z","iopub.execute_input":"2022-01-12T18:06:56.939436Z","iopub.status.idle":"2022-01-12T18:06:57.637695Z","shell.execute_reply.started":"2022-01-12T18:06:56.939389Z","shell.execute_reply":"2022-01-12T18:06:57.636773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here column 'primaty_use' was treated by get_dummies function and get_dummies is used for data manipulation\ndata_linearR = pd.get_dummies(data,columns = ['primary_use'])","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:07:02.273430Z","iopub.execute_input":"2022-01-12T18:07:02.273694Z","iopub.status.idle":"2022-01-12T18:07:04.463435Z","shell.execute_reply.started":"2022-01-12T18:07:02.273666Z","shell.execute_reply":"2022-01-12T18:07:04.462541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#printing the columns of \"data_linearR\"\ndata_linearR.columns","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:07:10.754059Z","iopub.execute_input":"2022-01-12T18:07:10.754353Z","iopub.status.idle":"2022-01-12T18:07:10.759961Z","shell.execute_reply.started":"2022-01-12T18:07:10.754318Z","shell.execute_reply":"2022-01-12T18:07:10.759168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using the important features\nXD =data_linearR[['building_id', 'meter', 'air_temperature', 'wind_speed', 'precip_depth_1_hr', 'cloud_coverage',\n       'square_feet', 'primary_use_Education', 'primary_use_Entertainment/public assembly',\n       'primary_use_Food sales and service', 'primary_use_Healthcare',\n       'primary_use_Lodging/residential',\n       'primary_use_Manufacturing/industrial', 'primary_use_Office',\n       'primary_use_Other', 'primary_use_Parking',\n       'primary_use_Public services', 'primary_use_Religious worship',\n       'primary_use_Retail', 'primary_use_Services',\n       'primary_use_Technology/science', 'primary_use_Utility',\n       'primary_use_Warehouse/storage', 'month', 'day']]\n\n# Create target variable\nYD = data_linearR['meter_reading']\n\n# Train, test, split\nXD_train,XD_test, YD_train, YD_test = train_test_split(XD,YD, test_size = .20, random_state= 0)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:07:12.723390Z","iopub.execute_input":"2022-01-12T18:07:12.723670Z","iopub.status.idle":"2022-01-12T18:07:23.574744Z","shell.execute_reply.started":"2022-01-12T18:07:12.723637Z","shell.execute_reply":"2022-01-12T18:07:23.573768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#applying RandomForestRegressor and fitting the model\nRidge = Ridge(alpha=1.0)\nRidge.fit(XD,YD)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:07:29.644408Z","iopub.execute_input":"2022-01-12T18:07:29.646006Z","iopub.status.idle":"2022-01-12T18:07:38.023456Z","shell.execute_reply.started":"2022-01-12T18:07:29.645943Z","shell.execute_reply":"2022-01-12T18:07:38.022627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculating accuracy of Randomforest model\nRidge.score(XD,YD)","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:07:45.447136Z","iopub.execute_input":"2022-01-12T18:07:45.447746Z","iopub.status.idle":"2022-01-12T18:07:47.369698Z","shell.execute_reply.started":"2022-01-12T18:07:45.447702Z","shell.execute_reply":"2022-01-12T18:07:47.368800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#getting predictions for the test\nYR_pred = Ridge.predict(XD_test)\nYR_pred","metadata":{"execution":{"iopub.status.busy":"2022-01-12T18:08:10.814477Z","iopub.execute_input":"2022-01-12T18:08:10.814827Z","iopub.status.idle":"2022-01-12T18:08:11.169739Z","shell.execute_reply.started":"2022-01-12T18:08:10.814777Z","shell.execute_reply":"2022-01-12T18:08:11.168869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}