{"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\n#import numpy as np # linear algebra\n#import 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\n# import os\n# for 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-09T13:17:58.766736Z","iopub.execute_input":"2022-01-09T13:17:58.767748Z","iopub.status.idle":"2022-01-09T13:17:58.789594Z","shell.execute_reply.started":"2022-01-09T13:17:58.767629Z","shell.execute_reply":"2022-01-09T13:17:58.788889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Read the data ","metadata":{}},{"cell_type":"code","source":"import os\nimport gc\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O\nimport datetime","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:17:58.791397Z","iopub.execute_input":"2022-01-09T13:17:58.79232Z","iopub.status.idle":"2022-01-09T13:17:58.802331Z","shell.execute_reply.started":"2022-01-09T13:17:58.792273Z","shell.execute_reply":"2022-01-09T13:17:58.801679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/ashrae-energy-prediction'\n\nfor dirname, _, filenames in os.walk(path):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:17:58.803488Z","iopub.execute_input":"2022-01-09T13:17:58.804238Z","iopub.status.idle":"2022-01-09T13:17:58.822354Z","shell.execute_reply.started":"2022-01-09T13:17:58.804185Z","shell.execute_reply":"2022-01-09T13:17:58.821467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Prepare Data","metadata":{}},{"cell_type":"code","source":"def reduce_mem(df):\n    result = df.copy()\n    for col in result.columns:\n        col_data = result[col]\n        dn = col_data.dtype.name\n        if not dn.startswith(\"datetime\"):\n            if dn == \"object\":  # only object feature has low cardinality\n                result[col] = pd.to_numeric(col_data.astype(\"category\").cat.codes, downcast=\"unsigned\")\n            elif dn.startswith(\"int\") | dn.startswith(\"uint\"):\n                if col_data.min() >= 0:\n                    result[col] = pd.to_numeric(col_data, downcast=\"unsigned\")\n                else:\n                    result[col] = pd.to_numeric(col_data, downcast='integer')\n            else:\n                result[col] = pd.to_numeric(col_data, downcast='float')\n    return result\n\ndef _delete_bad_sitezero(X, y):\n    cond = (X.timestamp > '2016-05-20') | (X.site_id != 0) | (X.meter != 0)\n    X = X[cond]\n    y = y.reindex_like(X)\n    return X.reset_index(drop=True), y.reset_index(drop=True)\n\ndef _extract_temporal(X, train=True):\n    X['hour'] = X.timestamp.dt.hour\n    X['weekday'] = X.timestamp.dt.weekday\n    if train:\n        # include month to create validation set, to be deleted before training\n        X['month'] = X.timestamp.dt.month \n    # month and year cause overfit, could try other (holiday, business, etc.)\n    return reduce_mem(X)\ndef load_data(source='train'):\n    assert source in ['train','test']\n    df = pd.read_csv(f'{path}/{source}.csv', parse_dates=['timestamp'])\n    return reduce_mem(df)\n\ndef load_building():\n    df = pd.read_csv(f'{path}/building_metadata.csv').fillna(-1)\n    return reduce_mem(df)\n\ndef load_weather(source='train', fix_timezone=True, impute=True, add_lag=True):\n    assert source in ['train','test']\n    df = pd.read_csv(f'{path}/weather_{source}.csv', parse_dates=['timestamp'])\n    if fix_timezone:\n        offsets = [5,0,9,6,8,0,6,6,5,7,8,6,0,7,6,6]\n        offset_map = {site: offset for site, offset in enumerate(offsets)}\n        df.timestamp = df.timestamp - pd.to_timedelta(df.site_id.map(offset_map), unit='h')\n    if impute:\n        site_dfs = []\n        for site in df.site_id.unique():\n            if source == 'train':\n                new_idx = pd.date_range(start='2016-1-1', end='2016-12-31-23', freq='H')\n            else:\n                new_idx = pd.date_range(start='2017-1-1', end='2018-12-31-23', freq='H')\n            site_df = df[df.site_id == site].set_index('timestamp').reindex(new_idx)\n            site_df.site_id = site\n            for col in [c for c in site_df.columns if c != 'site_id']:\n                site_df[col] = site_df[col].interpolate(limit_direction='both', method='linear')\n                site_df[col] = site_df[col].fillna(df[col].median())\n            site_dfs.append(site_df)\n        df = pd.concat(site_dfs)\n        df['timestamp'] = df.index\n        df = df.reset_index(drop=True)\n        \n    if add_lag:\n        df = add_lag_features(df, window=3)\n    \n    return reduce_mem(df)\n\ndef merged_dfs(source='train', fix_timezone=True, impute=True, add_lag=False):\n    df = load_data(source=source).merge(load_building(), on='building_id', how='left')\n    df = df.merge(load_weather(source=source, fix_timezone=fix_timezone, impute=impute, add_lag=add_lag),\n                 on=['site_id','timestamp'], how='left')\n    if source == 'train':\n        X = df.drop('meter_reading', axis=1)  \n        y = np.log1p(df.meter_reading)  # log-transform of target\n        return X, y\n    elif source == 'test':\n        return df","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:17:58.824124Z","iopub.execute_input":"2022-01-09T13:17:58.824715Z","iopub.status.idle":"2022-01-09T13:17:58.844623Z","shell.execute_reply.started":"2022-01-09T13:17:58.824673Z","shell.execute_reply":"2022-01-09T13:17:58.843692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = merged_dfs(add_lag=False)\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:17:58.846885Z","iopub.execute_input":"2022-01-09T13:17:58.848072Z","iopub.status.idle":"2022-01-09T13:18:25.595919Z","shell.execute_reply.started":"2022-01-09T13:17:58.848028Z","shell.execute_reply":"2022-01-09T13:18:25.594979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preprocessing\nX_train, y_train = _delete_bad_sitezero(X_train, y_train)\nX_train = _extract_temporal(X_train)\n\n# remove timestamp and other unimportant features\nto_drop = ['timestamp','sea_level_pressure','wind_direction','wind_speed', 'precip_depth_1_hr', 'year_built', 'square_feet']\nX_train.drop(to_drop, axis=1, inplace=True)\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:25.597029Z","iopub.execute_input":"2022-01-09T13:18:25.597275Z","iopub.status.idle":"2022-01-09T13:18:40.457904Z","shell.execute_reply.started":"2022-01-09T13:18:25.597247Z","shell.execute_reply":"2022-01-09T13:18:40.457089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:40.459265Z","iopub.execute_input":"2022-01-09T13:18:40.459562Z","iopub.status.idle":"2022-01-09T13:18:40.480144Z","shell.execute_reply.started":"2022-01-09T13:18:40.459521Z","shell.execute_reply":"2022-01-09T13:18:40.479086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(X_train.columns)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:40.481551Z","iopub.execute_input":"2022-01-09T13:18:40.481764Z","iopub.status.idle":"2022-01-09T13:18:40.487567Z","shell.execute_reply.started":"2022-01-09T13:18:40.481738Z","shell.execute_reply":"2022-01-09T13:18:40.486731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.concat([X_train, y_train], axis=1)\n\ndel X_train, y_train\ngc.collect()\n\ndf_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:40.488671Z","iopub.execute_input":"2022-01-09T13:18:40.488894Z","iopub.status.idle":"2022-01-09T13:18:41.009652Z","shell.execute_reply.started":"2022-01-09T13:18:40.488869Z","shell.execute_reply":"2022-01-09T13:18:41.008722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:41.010837Z","iopub.execute_input":"2022-01-09T13:18:41.011059Z","iopub.status.idle":"2022-01-09T13:18:41.030658Z","shell.execute_reply.started":"2022-01-09T13:18:41.011032Z","shell.execute_reply":"2022-01-09T13:18:41.029898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_targets = df_train['meter_reading']","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:41.031854Z","iopub.execute_input":"2022-01-09T13:18:41.032052Z","iopub.status.idle":"2022-01-09T13:18:41.03597Z","shell.execute_reply.started":"2022-01-09T13:18:41.032027Z","shell.execute_reply":"2022-01-09T13:18:41.035122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_targets.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:41.036922Z","iopub.execute_input":"2022-01-09T13:18:41.0373Z","iopub.status.idle":"2022-01-09T13:18:41.061058Z","shell.execute_reply.started":"2022-01-09T13:18:41.03727Z","shell.execute_reply":"2022-01-09T13:18:41.060227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Spliting Data","metadata":{}},{"cell_type":"markdown","source":"splite the dataset for training and testing set (20% for testing and 80% for training )","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_data,test_data,train_target,test_target = train_test_split(df_train,df_targets, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:41.06255Z","iopub.execute_input":"2022-01-09T13:18:41.062834Z","iopub.status.idle":"2022-01-09T13:18:47.674649Z","shell.execute_reply.started":"2022-01-09T13:18:41.062796Z","shell.execute_reply":"2022-01-09T13:18:47.673642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.678831Z","iopub.execute_input":"2022-01-09T13:18:47.679065Z","iopub.status.idle":"2022-01-09T13:18:47.694364Z","shell.execute_reply.started":"2022-01-09T13:18:47.679037Z","shell.execute_reply":"2022-01-09T13:18:47.693512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.695464Z","iopub.execute_input":"2022-01-09T13:18:47.695685Z","iopub.status.idle":"2022-01-09T13:18:47.708311Z","shell.execute_reply.started":"2022-01-09T13:18:47.695655Z","shell.execute_reply":"2022-01-09T13:18:47.707501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.70991Z","iopub.execute_input":"2022-01-09T13:18:47.710502Z","iopub.status.idle":"2022-01-09T13:18:47.720827Z","shell.execute_reply.started":"2022-01-09T13:18:47.710459Z","shell.execute_reply":"2022-01-09T13:18:47.720232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_target.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.722101Z","iopub.execute_input":"2022-01-09T13:18:47.722876Z","iopub.status.idle":"2022-01-09T13:18:47.733611Z","shell.execute_reply.started":"2022-01-09T13:18:47.722832Z","shell.execute_reply":"2022-01-09T13:18:47.73271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_target.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.735104Z","iopub.execute_input":"2022-01-09T13:18:47.73556Z","iopub.status.idle":"2022-01-09T13:18:47.746802Z","shell.execute_reply.started":"2022-01-09T13:18:47.735519Z","shell.execute_reply":"2022-01-09T13:18:47.746228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Machine Models","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression,SGDRegressor,ElasticNet,Ridge\nfrom sklearn.svm import SVC\nfrom sklearn import linear_model\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import mean_squared_error,mean_absolute_error\nfrom sklearn.svm import SVR\nfrom sklearn.tree import DecisionTreeRegressor","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.748281Z","iopub.execute_input":"2022-01-09T13:18:47.748729Z","iopub.status.idle":"2022-01-09T13:18:47.962684Z","shell.execute_reply.started":"2022-01-09T13:18:47.748686Z","shell.execute_reply":"2022-01-09T13:18:47.961909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def checkModelPerformane(model):\n    X=train_data\n    y=train_target\n    model.fit(X,y)\n    pred=model.predict(test_data)\n    print(\"mean_squared_error: \",np.sqrt(mean_squared_error(test_target, pred))) \n    print(\"mean_absolute_error: \", np.sqrt(mean_absolute_error(test_target, pred)))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.963723Z","iopub.execute_input":"2022-01-09T13:18:47.964123Z","iopub.status.idle":"2022-01-09T13:18:47.968648Z","shell.execute_reply.started":"2022-01-09T13:18:47.96409Z","shell.execute_reply":"2022-01-09T13:18:47.967719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 1.4. Linear Regression","metadata":{}},{"cell_type":"code","source":"print(\"LinearRegression\")\ncheckModelPerformane(LinearRegression())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:47.970026Z","iopub.execute_input":"2022-01-09T13:18:47.970251Z","iopub.status.idle":"2022-01-09T13:18:52.103747Z","shell.execute_reply.started":"2022-01-09T13:18:47.970223Z","shell.execute_reply":"2022-01-09T13:18:52.100732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"LinearRegression with lasso regualrizion\")\ncheckModelPerformane(linear_model.Lasso(alpha=0.1))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:52.105344Z","iopub.execute_input":"2022-01-09T13:18:52.105627Z","iopub.status.idle":"2022-01-09T13:18:54.788665Z","shell.execute_reply.started":"2022-01-09T13:18:52.105586Z","shell.execute_reply":"2022-01-09T13:18:54.787703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 4.2. Lasso, Elastic and ridge regression","metadata":{}},{"cell_type":"code","source":"print(\"lasso regression \")\ncheckModelPerformane(linear_model.Lasso(alpha=0.1))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:54.793595Z","iopub.execute_input":"2022-01-09T13:18:54.79639Z","iopub.status.idle":"2022-01-09T13:18:57.513371Z","shell.execute_reply.started":"2022-01-09T13:18:54.796329Z","shell.execute_reply":"2022-01-09T13:18:57.51239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"ElasticNet regression \")\ncheckModelPerformane(ElasticNet())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:57.51487Z","iopub.execute_input":"2022-01-09T13:18:57.51545Z","iopub.status.idle":"2022-01-09T13:18:59.936444Z","shell.execute_reply.started":"2022-01-09T13:18:57.515395Z","shell.execute_reply":"2022-01-09T13:18:59.935593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Ridge regression \")\ncheckModelPerformane(Ridge(alpha=1.0))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:18:59.937896Z","iopub.execute_input":"2022-01-09T13:18:59.938461Z","iopub.status.idle":"2022-01-09T13:19:01.537728Z","shell.execute_reply.started":"2022-01-09T13:18:59.938415Z","shell.execute_reply":"2022-01-09T13:19:01.536693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 3.4 Decision tree","metadata":{}},{"cell_type":"code","source":"print(\"Decision tree regression\")\ncheckModelPerformane(DecisionTreeRegressor(random_state=42))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:19:01.539911Z","iopub.execute_input":"2022-01-09T13:19:01.54068Z","iopub.status.idle":"2022-01-09T13:22:35.363015Z","shell.execute_reply.started":"2022-01-09T13:19:01.540627Z","shell.execute_reply":"2022-01-09T13:22:35.361983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 4.4 layers","metadata":{}},{"cell_type":"code","source":"from keras import models\nfrom keras import layers\n\ndef build_model():\n    # Because we will need to instantiate\n    # the same model multiple times,\n    # we use a function to construct it.\n    model=models.Sequential()\n    model.add(layers.Flatten())\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(128, activation='relu'))# Add the proper output and activation\n    #model.add(layers.Dense(1, activation='relu'))# Add the proper output and activation\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:35.364968Z","iopub.execute_input":"2022-01-09T13:22:35.365336Z","iopub.status.idle":"2022-01-09T13:22:41.481981Z","shell.execute_reply.started":"2022-01-09T13:22:35.365289Z","shell.execute_reply":"2022-01-09T13:22:41.481098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = build_model()\n# model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])# Add the proper loss\n\n# Desc = model.fit(df_train,df_targets,\n#                     epochs=20, batch_size=3, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:41.485783Z","iopub.execute_input":"2022-01-09T13:22:41.486038Z","iopub.status.idle":"2022-01-09T13:22:41.489424Z","shell.execute_reply.started":"2022-01-09T13:22:41.486005Z","shell.execute_reply":"2022-01-09T13:22:41.488887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 5.4 SGDRegressor","metadata":{}},{"cell_type":"markdown","source":"we didn't run it, because it took alot of time to run","metadata":{}},{"cell_type":"code","source":"# print(\"SGDRegressor\")\n# checkModelPerformane(SGDRegressor())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:41.490396Z","iopub.execute_input":"2022-01-09T13:22:41.490944Z","iopub.status.idle":"2022-01-09T13:22:41.505047Z","shell.execute_reply.started":"2022-01-09T13:22:41.49091Z","shell.execute_reply":"2022-01-09T13:22:41.504131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 6.4 Random Forest","metadata":{}},{"cell_type":"markdown","source":"we didn't run it, because it took alot of time to run","metadata":{}},{"cell_type":"code","source":"# print(\"Random Forest\")\n# checkModelPerformane(RandomForestRegressor())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:41.506234Z","iopub.execute_input":"2022-01-09T13:22:41.506462Z","iopub.status.idle":"2022-01-09T13:22:41.516931Z","shell.execute_reply.started":"2022-01-09T13:22:41.506434Z","shell.execute_reply":"2022-01-09T13:22:41.515741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # 7.4 SVM","metadata":{}},{"cell_type":"markdown","source":"Took a lot of time","metadata":{}},{"cell_type":"code","source":"# print(\"SVM\")\n# checkModelPerformane(SVR())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:41.518249Z","iopub.execute_input":"2022-01-09T13:22:41.520963Z","iopub.status.idle":"2022-01-09T13:22:41.52791Z","shell.execute_reply.started":"2022-01-09T13:22:41.520921Z","shell.execute_reply":"2022-01-09T13:22:41.527176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(df_train)\nprint(X_scaled)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:41.529401Z","iopub.execute_input":"2022-01-09T13:22:41.529849Z","iopub.status.idle":"2022-01-09T13:22:46.405738Z","shell.execute_reply.started":"2022-01-09T13:22:41.529805Z","shell.execute_reply":"2022-01-09T13:22:46.404734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Fine tune model","metadata":{}},{"cell_type":"markdown","source":"we didn't run it, because it took alot of time to run","metadata":{}},{"cell_type":"code","source":"# from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.406829Z","iopub.execute_input":"2022-01-09T13:22:46.407036Z","iopub.status.idle":"2022-01-09T13:22:46.41056Z","shell.execute_reply.started":"2022-01-09T13:22:46.407012Z","shell.execute_reply":"2022-01-09T13:22:46.409915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# param_grid = [\n#     # try 12 (3×4) combinations of hyperparameters\n#     {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},\n#     # then try 6 (2×3) combinations with bootstrap set as False\n#     {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},\n#   ]\n\n# forest_reg = RandomForestRegressor(random_state=42)\n# # train across 5 folds, that's a total of (12+6)*5=90 rounds of training \n# grid_search = GridSearchCV(forest_reg, param_grid, cv=5,\n#                            scoring='neg_mean_squared_error', return_train_score=True)\n# grid_search.fit(housing_prepared, housing_labels)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.411553Z","iopub.execute_input":"2022-01-09T13:22:46.411872Z","iopub.status.idle":"2022-01-09T13:22:46.424249Z","shell.execute_reply.started":"2022-01-09T13:22:46.411835Z","shell.execute_reply":"2022-01-09T13:22:46.423252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid_search.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.425704Z","iopub.execute_input":"2022-01-09T13:22:46.426071Z","iopub.status.idle":"2022-01-09T13:22:46.437586Z","shell.execute_reply.started":"2022-01-09T13:22:46.426036Z","shell.execute_reply":"2022-01-09T13:22:46.436704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.439835Z","iopub.execute_input":"2022-01-09T13:22:46.440226Z","iopub.status.idle":"2022-01-09T13:22:46.449959Z","shell.execute_reply.started":"2022-01-09T13:22:46.440184Z","shell.execute_reply":"2022-01-09T13:22:46.449319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cvres = grid_search.cv_results_\n# for mean_score, params in zip(cvres[\"mean_test_score\"], cvres[\"params\"]):\n#     print(np.sqrt(-mean_score), params)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.451319Z","iopub.execute_input":"2022-01-09T13:22:46.452239Z","iopub.status.idle":"2022-01-09T13:22:46.461889Z","shell.execute_reply.started":"2022-01-09T13:22:46.452147Z","shell.execute_reply":"2022-01-09T13:22:46.460946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.DataFrame(grid_search.cv_results_)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.462954Z","iopub.execute_input":"2022-01-09T13:22:46.463199Z","iopub.status.idle":"2022-01-09T13:22:46.472624Z","shell.execute_reply.started":"2022-01-09T13:22:46.463145Z","shell.execute_reply":"2022-01-09T13:22:46.471977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.model_selection import RandomizedSearchCV\n# from scipy.stats import randint\n\n# param_distribs = {\n#         'n_estimators': randint(low=1, high=200),\n#         'max_features': randint(low=1, high=8),\n#     }\n\n# forest_reg = LinearRegression()\n# rnd_search = RandomizedSearchCV(forest_reg, param_distributions=param_distribs,\n#                                 n_iter=10, cv=5, scoring='neg_mean_squared_error', random_state=42)\n# rnd_search.fit(train_data, train_target)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.473826Z","iopub.execute_input":"2022-01-09T13:22:46.474431Z","iopub.status.idle":"2022-01-09T13:22:46.488976Z","shell.execute_reply.started":"2022-01-09T13:22:46.474376Z","shell.execute_reply":"2022-01-09T13:22:46.488036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cvres = rnd_search.cv_results_\n# for mean_score, params in zip(cvres[\"mean_test_score\"], cvres[\"params\"]):\n#     print(np.sqrt(-mean_score), params)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T13:22:46.490523Z","iopub.execute_input":"2022-01-09T13:22:46.491073Z","iopub.status.idle":"2022-01-09T13:22:46.500454Z","shell.execute_reply.started":"2022-01-09T13:22:46.49104Z","shell.execute_reply":"2022-01-09T13:22:46.499593Z"},"trusted":true},"execution_count":null,"outputs":[]}]}