{"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":"# 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\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-21T15:29:36.095429Z","iopub.execute_input":"2022-01-21T15:29:36.095957Z","iopub.status.idle":"2022-01-21T15:29:36.112746Z","shell.execute_reply.started":"2022-01-21T15:29:36.095908Z","shell.execute_reply":"2022-01-21T15:29:36.111963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install hvplot","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:29:36.115065Z","iopub.execute_input":"2022-01-21T15:29:36.115598Z","iopub.status.idle":"2022-01-21T15:29:47.035531Z","shell.execute_reply.started":"2022-01-21T15:29:36.115564Z","shell.execute_reply":"2022-01-21T15:29:47.034669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## IMPORTING LIBRARIES ","metadata":{}},{"cell_type":"code","source":"print(\"import libraries!\")\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport hvplot.pandas\n%matplotlib inline\nprint(\"importing finished\")","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:29:47.037471Z","iopub.execute_input":"2022-01-21T15:29:47.037801Z","iopub.status.idle":"2022-01-21T15:29:47.054016Z","shell.execute_reply.started":"2022-01-21T15:29:47.037768Z","shell.execute_reply":"2022-01-21T15:29:47.052847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read Datasets","metadata":{}},{"cell_type":"code","source":"building_meta = 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')\ntrain = pd.read_csv('../input/ashrae-energy-prediction/train.csv')\ntest = pd.read_csv('../input/ashrae-energy-prediction/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:29:47.055781Z","iopub.execute_input":"2022-01-21T15:29:47.056265Z","iopub.status.idle":"2022-01-21T15:30:33.665656Z","shell.execute_reply.started":"2022-01-21T15:29:47.056215Z","shell.execute_reply":"2022-01-21T15:30:33.664739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Data Description","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:33.668329Z","iopub.execute_input":"2022-01-21T15:30:33.668586Z","iopub.status.idle":"2022-01-21T15:30:33.68543Z","shell.execute_reply.started":"2022-01-21T15:30:33.668554Z","shell.execute_reply":"2022-01-21T15:30:33.684414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Function to reduce the DF size\ndef reduce_mem_usage(df, verbose=True):\n    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']\n    start_mem = df.memory_usage().sum() / 1024**2    \n    for col in df.columns:\n        col_type = df[col].dtypes\n        if col_type in numerics:\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    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose: print('Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction)'.format(end_mem, 100 * (start_mem - end_mem) / start_mem))\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:33.687029Z","iopub.execute_input":"2022-01-21T15:30:33.687285Z","iopub.status.idle":"2022-01-21T15:30:33.708601Z","shell.execute_reply.started":"2022-01-21T15:30:33.687253Z","shell.execute_reply":"2022-01-21T15:30:33.707339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()\nprint('#'*25)\ntest.info()\nprint('#'*25)\nweather_train.info()\nprint('#'*25)\nweather_test.info()\nprint('#'*25)\nbuilding_meta.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:33.710701Z","iopub.execute_input":"2022-01-21T15:30:33.71098Z","iopub.status.idle":"2022-01-21T15:30:33.817782Z","shell.execute_reply.started":"2022-01-21T15:30:33.710946Z","shell.execute_reply":"2022-01-21T15:30:33.81681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Note: memory usage before reduction:\n> 1. Train data memory usage is: 616.9+ MB\n> 2. Test data memory usage is: 1.2+ GB\n> 3. Weather_train memory usage is: 9.6+ MB\n> 4. Weather_test memory usage is: 19.0+ MB\n> 5. Building_meta memory usage is: 68.0+ KB ","metadata":{}},{"cell_type":"code","source":"## REducing memory\ntrain = reduce_mem_usage(train)\ntest = reduce_mem_usage(test)\n\nweather_train = reduce_mem_usage(weather_train)\nweather_test = reduce_mem_usage(weather_test)\nbuilding_meta = reduce_mem_usage(building_meta)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:33.818979Z","iopub.execute_input":"2022-01-21T15:30:33.81923Z","iopub.status.idle":"2022-01-21T15:30:35.267917Z","shell.execute_reply.started":"2022-01-21T15:30:33.819197Z","shell.execute_reply":"2022-01-21T15:30:35.266942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()\nprint('#'*25)\ntest.info()\nprint('#'*25)\nweather_train.info()\nprint('#'*25)\nweather_test.info()\nprint('#'*25)\nbuilding_meta.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:35.269194Z","iopub.execute_input":"2022-01-21T15:30:35.269463Z","iopub.status.idle":"2022-01-21T15:30:35.37268Z","shell.execute_reply.started":"2022-01-21T15:30:35.269432Z","shell.execute_reply":"2022-01-21T15:30:35.371656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Note: memory usage after reduction :\n> 1. Train data memory usage is: 289.2+ MB (53.1% reduction)\n> 2. Test data memory usage is: 596.5+ MB (53.1% reduction)\n> 3. Weather_train memory usage is: 3.1+ MB (68.1% reduction)\n> 4. Weather_test memory usage is: 6.1+ MB (68.1% reduction)\n> 5. Building_meta memory usage is: 27.0+ KB (60.3% reduction)","metadata":{}},{"cell_type":"code","source":"train.columns.values","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:35.374248Z","iopub.execute_input":"2022-01-21T15:30:35.374486Z","iopub.status.idle":"2022-01-21T15:30:35.382042Z","shell.execute_reply.started":"2022-01-21T15:30:35.374455Z","shell.execute_reply":"2022-01-21T15:30:35.380845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:35.383729Z","iopub.execute_input":"2022-01-21T15:30:35.384112Z","iopub.status.idle":"2022-01-21T15:30:36.842311Z","shell.execute_reply.started":"2022-01-21T15:30:35.384066Z","shell.execute_reply":"2022-01-21T15:30:36.840609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:36.84441Z","iopub.execute_input":"2022-01-21T15:30:36.845406Z","iopub.status.idle":"2022-01-21T15:30:38.793601Z","shell.execute_reply.started":"2022-01-21T15:30:36.845334Z","shell.execute_reply":"2022-01-21T15:30:38.792614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns.values","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:38.79505Z","iopub.execute_input":"2022-01-21T15:30:38.796036Z","iopub.status.idle":"2022-01-21T15:30:38.80374Z","shell.execute_reply.started":"2022-01-21T15:30:38.795986Z","shell.execute_reply":"2022-01-21T15:30:38.802961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## # FULL EDA","metadata":{}},{"cell_type":"code","source":"# Examine the Distribution of the Target Column (meter_reading)\nfor key, d in train.groupby('meter_reading'):\n    break\n    d.head()\nplt.figure(figsize = (20,5))\nd['meter'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:38.807644Z","iopub.execute_input":"2022-01-21T15:30:38.807926Z","iopub.status.idle":"2022-01-21T15:30:52.099019Z","shell.execute_reply.started":"2022-01-21T15:30:38.807861Z","shell.execute_reply":"2022-01-21T15:30:52.097957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,5))\ntrain['meter_reading'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:52.100589Z","iopub.execute_input":"2022-01-21T15:30:52.101757Z","iopub.status.idle":"2022-01-21T15:30:56.348096Z","shell.execute_reply.started":"2022-01-21T15:30:52.101716Z","shell.execute_reply":"2022-01-21T15:30:56.347103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['meter_reading'].plot(kind='hist',\n                            bins=25,\n                            figsize=(15, 5),\n                           title='Distribution of Target Variable (meter_reading)')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:56.349615Z","iopub.execute_input":"2022-01-21T15:30:56.350042Z","iopub.status.idle":"2022-01-21T15:30:59.318297Z","shell.execute_reply.started":"2022-01-21T15:30:56.350006Z","shell.execute_reply":"2022-01-21T15:30:59.317274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check missing data","metadata":{}},{"cell_type":"code","source":"total = train.isnull().sum().sort_values(ascending = False)\npercent = (train.isnull().sum()/train.isnull().count()*100).sort_values(ascending = False)\nmissing__train_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing__train_data.head(4)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:30:59.319602Z","iopub.execute_input":"2022-01-21T15:30:59.319898Z","iopub.status.idle":"2022-01-21T15:31:05.087322Z","shell.execute_reply.started":"2022-01-21T15:30:59.319842Z","shell.execute_reply":"2022-01-21T15:31:05.086314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking missing data for weather_train dataset\ntotal = weather_train.isnull().sum().sort_values(ascending = False)\npercent = (weather_train.isnull().sum()/weather_train.isnull().count()*100).sort_values(ascending = False)\nmissing_weather_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing_weather_data.head(9)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.089062Z","iopub.execute_input":"2022-01-21T15:31:05.089392Z","iopub.status.idle":"2022-01-21T15:31:05.16982Z","shell.execute_reply.started":"2022-01-21T15:31:05.089345Z","shell.execute_reply":"2022-01-21T15:31:05.168933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Now we can drop columns of 'cloud_coverage', 'precip_depth_1_hr' and 'sea_level_pressure' because of the large amount of missing data on them. over 49%, 36% and 7.5% respectively of their data is missing.","metadata":{}},{"cell_type":"code","source":"weather_train.drop(['cloud_coverage','precip_depth_1_hr', 'sea_level_pressure'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.171586Z","iopub.execute_input":"2022-01-21T15:31:05.172163Z","iopub.status.idle":"2022-01-21T15:31:05.197236Z","shell.execute_reply.started":"2022-01-21T15:31:05.172107Z","shell.execute_reply":"2022-01-21T15:31:05.19614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking missing data for weather_test dataset\n\ntotal = weather_test.isnull().sum().sort_values(ascending = False)\npercent = (weather_test.isnull().sum()/weather_test.isnull().count()*100).sort_values(ascending = False)\nmissing_weather_test_data  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing_weather_test_data.head(9)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.198751Z","iopub.execute_input":"2022-01-21T15:31:05.199118Z","iopub.status.idle":"2022-01-21T15:31:05.32893Z","shell.execute_reply.started":"2022-01-21T15:31:05.199066Z","shell.execute_reply":"2022-01-21T15:31:05.327711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Also, the same problem of missing data exists in weather_test dataset, so we can drop them.","metadata":{}},{"cell_type":"code","source":"weather_test.drop(['cloud_coverage','precip_depth_1_hr', 'sea_level_pressure'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.33079Z","iopub.execute_input":"2022-01-21T15:31:05.331186Z","iopub.status.idle":"2022-01-21T15:31:05.360574Z","shell.execute_reply.started":"2022-01-21T15:31:05.331138Z","shell.execute_reply":"2022-01-21T15:31:05.359485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total = building_meta.isnull().sum().sort_values(ascending = False)\npercent = (building_meta.isnull().sum()/building_meta.isnull().count()*100).sort_values(ascending = False)\nmissing_building_meta  = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])\nmissing_building_meta.head(6)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.362506Z","iopub.execute_input":"2022-01-21T15:31:05.362911Z","iopub.status.idle":"2022-01-21T15:31:05.38619Z","shell.execute_reply.started":"2022-01-21T15:31:05.362823Z","shell.execute_reply":"2022-01-21T15:31:05.385499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Columns types\n    **Let's look at the number of columns of each data type. int64 and float64 are numeric variables (which can be either discrete or continuous). object columns contain strings and are categorical features.**","metadata":{}},{"cell_type":"code","source":"train.dtypes.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.387471Z","iopub.execute_input":"2022-01-21T15:31:05.387964Z","iopub.status.idle":"2022-01-21T15:31:05.396664Z","shell.execute_reply.started":"2022-01-21T15:31:05.387931Z","shell.execute_reply":"2022-01-21T15:31:05.395596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.select_dtypes('object').apply(pd.Series.nunique, axis = 0)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:05.398404Z","iopub.execute_input":"2022-01-21T15:31:05.398784Z","iopub.status.idle":"2022-01-21T15:31:07.386738Z","shell.execute_reply.started":"2022-01-21T15:31:05.398742Z","shell.execute_reply":"2022-01-21T15:31:07.385924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlations\nNow that we have dealt with the categorical variables and the outliers, let's continue with the EDA. One way to try and understand the data is by looking for correlations between the features and the target. We can calculate the Pearson correlation coefficient between every variable and the target using the .corr dataframe method.\n\nThe correlation coefficient is not the greatest method to represent \"relevance\" of a feature, but it does give us an idea of possible relationships within the data. Some general interpretations of the absolute value of the correlation coefficent are:\n*     00-.19 “very weak”\n*     20-.39 “weak”\n*     40-.59 “moderate”\n*     60-.79 “strong”\n*     80-1.0 “very strong”","metadata":{}},{"cell_type":"code","source":"correlations = train.corr()['meter_reading'].sort_values()\n\n# Display correlations\nprint('Most Positive Correlations:\\n', correlations.tail(15))\nprint('\\nMost Negative Correlations:\\n', correlations.head(15))","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:07.387938Z","iopub.execute_input":"2022-01-21T15:31:07.38824Z","iopub.status.idle":"2022-01-21T15:31:08.223891Z","shell.execute_reply.started":"2022-01-21T15:31:07.388208Z","shell.execute_reply":"2022-01-21T15:31:08.223038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# weather correlation\nw_correlations = weather_train.corr()['wind_speed'].sort_values()\n\n# Display correlations\nprint('Most Positive Correlations:\\n', w_correlations.tail(15))\nprint('\\nMost Negative Correlations:\\n',w_correlations.head(15))","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:08.225263Z","iopub.execute_input":"2022-01-21T15:31:08.225594Z","iopub.status.idle":"2022-01-21T15:31:08.262415Z","shell.execute_reply.started":"2022-01-21T15:31:08.225547Z","shell.execute_reply":"2022-01-21T15:31:08.261416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrs = train.corr()\ncorrs","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:08.263866Z","iopub.execute_input":"2022-01-21T15:31:08.264469Z","iopub.status.idle":"2022-01-21T15:31:09.099782Z","shell.execute_reply.started":"2022-01-21T15:31:08.264418Z","shell.execute_reply":"2022-01-21T15:31:09.098764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(true, predicted):  \n    mae = metrics.mean_absolute_error(true, predicted)\n    mse = metrics.mean_squared_error(true, predicted)\n    rmse = np.sqrt(metrics.mean_squared_error(true, predicted))\n    r2_square = metrics.r2_score(true, predicted)\n    print('MAE:', mae)\n    print('MSE:', mse)\n    print('RMSE:', rmse)\n    print('R2 Square', r2_square)\n    print('__________________________________')","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:09.101405Z","iopub.execute_input":"2022-01-21T15:31:09.101759Z","iopub.status.idle":"2022-01-21T15:31:09.108842Z","shell.execute_reply.started":"2022-01-21T15:31:09.101711Z","shell.execute_reply":"2022-01-21T15:31:09.108149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Support Vector Machine ","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVR\n\nsvm_reg = SVR()\nsvm_reg.fit(X_train, y_train)\n\ntest_pred = svm_reg.predict(X_test)\ntrain_pred = svm_reg.predict(X_train)\n\nprint('Test set evaluation:\\n_____________________________________')\nevaluate(y_test, test_pred)\n\nprint('Train set evaluation:\\n_____________________________________')\nevaluate(y_train, train_pred)","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:09.109796Z","iopub.execute_input":"2022-01-21T15:31:09.110088Z","iopub.status.idle":"2022-01-21T15:31:09.178403Z","shell.execute_reply.started":"2022-01-21T15:31:09.110055Z","shell.execute_reply":"2022-01-21T15:31:09.177472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Stochastic Gradient Descent","metadata":{}},{"cell_type":"code","source":"sgdreg = SGDRegressor()\nsgdreg.fit(X_train, y_train)\ny_pred = sgdreg.predict(X_test)\nprint(\"RMSLE for Stochastic Gradient Descent: \", math.sqrt(mean_squared_error(y_test, y_pred)))","metadata":{"execution":{"iopub.status.busy":"2022-01-21T15:31:09.179288Z","iopub.status.idle":"2022-01-21T15:31:09.179683Z","shell.execute_reply.started":"2022-01-21T15:31:09.179469Z","shell.execute_reply":"2022-01-21T15:31:09.179489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train/Test Split","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}