{"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-09T11:25:34.982414Z","iopub.execute_input":"2022-01-09T11:25:34.983304Z","iopub.status.idle":"2022-01-09T11:25:34.989253Z","shell.execute_reply.started":"2022-01-09T11:25:34.983231Z","shell.execute_reply":"2022-01-09T11:25:34.988254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read 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-09T11:25:34.990803Z","iopub.execute_input":"2022-01-09T11:25:34.991116Z","iopub.status.idle":"2022-01-09T11:25:35.003706Z","shell.execute_reply.started":"2022-01-09T11:25:34.991084Z","shell.execute_reply":"2022-01-09T11:25:35.003025Z"},"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-09T11:25:35.004693Z","iopub.execute_input":"2022-01-09T11:25:35.005249Z","iopub.status.idle":"2022-01-09T11:25:35.022065Z","shell.execute_reply.started":"2022-01-09T11:25:35.005217Z","shell.execute_reply":"2022-01-09T11:25:35.021367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 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-09T11:25:35.023491Z","iopub.execute_input":"2022-01-09T11:25:35.023728Z","iopub.status.idle":"2022-01-09T11:25:35.046555Z","shell.execute_reply.started":"2022-01-09T11:25:35.023699Z","shell.execute_reply":"2022-01-09T11:25:35.045416Z"},"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-09T11:25:35.047915Z","iopub.execute_input":"2022-01-09T11:25:35.048788Z","iopub.status.idle":"2022-01-09T11:25:58.377918Z","shell.execute_reply.started":"2022-01-09T11:25:35.048741Z","shell.execute_reply":"2022-01-09T11:25:58.377027Z"},"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-09T11:25:58.379157Z","iopub.execute_input":"2022-01-09T11:25:58.380086Z","iopub.status.idle":"2022-01-09T11:26:13.636202Z","shell.execute_reply.started":"2022-01-09T11:25:58.380046Z","shell.execute_reply":"2022-01-09T11:26:13.635077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:13.638243Z","iopub.execute_input":"2022-01-09T11:26:13.638577Z","iopub.status.idle":"2022-01-09T11:26:13.659176Z","shell.execute_reply.started":"2022-01-09T11:26:13.638543Z","shell.execute_reply":"2022-01-09T11:26:13.658506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(X_train.columns)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:13.660088Z","iopub.execute_input":"2022-01-09T11:26:13.660778Z","iopub.status.idle":"2022-01-09T11:26:13.666441Z","shell.execute_reply.started":"2022-01-09T11:26:13.660741Z","shell.execute_reply":"2022-01-09T11:26:13.665664Z"},"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-09T11:26:13.667791Z","iopub.execute_input":"2022-01-09T11:26:13.668592Z","iopub.status.idle":"2022-01-09T11:26:14.274186Z","shell.execute_reply.started":"2022-01-09T11:26:13.668545Z","shell.execute_reply":"2022-01-09T11:26:14.273283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:14.275498Z","iopub.execute_input":"2022-01-09T11:26:14.275747Z","iopub.status.idle":"2022-01-09T11:26:14.298148Z","shell.execute_reply.started":"2022-01-09T11:26:14.275717Z","shell.execute_reply":"2022-01-09T11:26:14.297586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_targets = df_train['meter_reading']","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:14.299000Z","iopub.execute_input":"2022-01-09T11:26:14.299732Z","iopub.status.idle":"2022-01-09T11:26:14.303900Z","shell.execute_reply.started":"2022-01-09T11:26:14.299696Z","shell.execute_reply":"2022-01-09T11:26:14.302943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_targets.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:14.305465Z","iopub.execute_input":"2022-01-09T11:26:14.305784Z","iopub.status.idle":"2022-01-09T11:26:14.323186Z","shell.execute_reply.started":"2022-01-09T11:26:14.305734Z","shell.execute_reply":"2022-01-09T11:26:14.322586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spliting Data","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-09T11:26:14.324112Z","iopub.execute_input":"2022-01-09T11:26:14.324659Z","iopub.status.idle":"2022-01-09T11:26:20.238043Z","shell.execute_reply.started":"2022-01-09T11:26:14.324626Z","shell.execute_reply":"2022-01-09T11:26:20.237128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.242999Z","iopub.execute_input":"2022-01-09T11:26:20.243233Z","iopub.status.idle":"2022-01-09T11:26:20.260849Z","shell.execute_reply.started":"2022-01-09T11:26:20.243206Z","shell.execute_reply":"2022-01-09T11:26:20.259701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.262479Z","iopub.execute_input":"2022-01-09T11:26:20.262901Z","iopub.status.idle":"2022-01-09T11:26:20.274628Z","shell.execute_reply.started":"2022-01-09T11:26:20.262857Z","shell.execute_reply":"2022-01-09T11:26:20.273682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.276221Z","iopub.execute_input":"2022-01-09T11:26:20.276566Z","iopub.status.idle":"2022-01-09T11:26:20.286924Z","shell.execute_reply.started":"2022-01-09T11:26:20.276509Z","shell.execute_reply":"2022-01-09T11:26:20.286087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.288503Z","iopub.execute_input":"2022-01-09T11:26:20.290954Z","iopub.status.idle":"2022-01-09T11:26:20.301243Z","shell.execute_reply.started":"2022-01-09T11:26:20.290895Z","shell.execute_reply":"2022-01-09T11:26:20.300130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_target.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.302562Z","iopub.execute_input":"2022-01-09T11:26:20.303280Z","iopub.status.idle":"2022-01-09T11:26:20.315678Z","shell.execute_reply.started":"2022-01-09T11:26:20.303233Z","shell.execute_reply":"2022-01-09T11:26:20.314841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_target.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.317316Z","iopub.execute_input":"2022-01-09T11:26:20.317811Z","iopub.status.idle":"2022-01-09T11:26:20.331051Z","shell.execute_reply.started":"2022-01-09T11:26:20.317769Z","shell.execute_reply":"2022-01-09T11:26:20.330247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test = merged_dfs(source='test', add_lag=False)\n# X_test = _extract_temporal(X_test, train=False)\n# X_test.drop(columns=['timestamp']+to_drop, inplace=True)\n# gc.collect()\n\n# X_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.332247Z","iopub.execute_input":"2022-01-09T11:26:20.333527Z","iopub.status.idle":"2022-01-09T11:26:20.343540Z","shell.execute_reply.started":"2022-01-09T11:26:20.333482Z","shell.execute_reply":"2022-01-09T11:26:20.342424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 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-09T11:26:20.345036Z","iopub.execute_input":"2022-01-09T11:26:20.345506Z","iopub.status.idle":"2022-01-09T11:26:20.357938Z","shell.execute_reply.started":"2022-01-09T11:26:20.345464Z","shell.execute_reply":"2022-01-09T11:26:20.356892Z"},"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-09T11:26:20.359412Z","iopub.execute_input":"2022-01-09T11:26:20.359863Z","iopub.status.idle":"2022-01-09T11:26:20.371485Z","shell.execute_reply.started":"2022-01-09T11:26:20.359822Z","shell.execute_reply":"2022-01-09T11:26:20.370617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Linear Regression**","metadata":{}},{"cell_type":"code","source":"print(\"LinearRegression\")\ncheckModelPerformane(LinearRegression())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:20.372922Z","iopub.execute_input":"2022-01-09T11:26:20.373378Z","iopub.status.idle":"2022-01-09T11:26:24.383300Z","shell.execute_reply.started":"2022-01-09T11:26:20.373339Z","shell.execute_reply":"2022-01-09T11:26:24.382391Z"},"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-09T11:26:24.384555Z","iopub.execute_input":"2022-01-09T11:26:24.385146Z","iopub.status.idle":"2022-01-09T11:26:27.112531Z","shell.execute_reply.started":"2022-01-09T11:26:24.385098Z","shell.execute_reply":"2022-01-09T11:26:27.111632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**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-09T11:26:27.113808Z","iopub.execute_input":"2022-01-09T11:26:27.114381Z","iopub.status.idle":"2022-01-09T11:26:29.798184Z","shell.execute_reply.started":"2022-01-09T11:26:27.114336Z","shell.execute_reply":"2022-01-09T11:26:29.797197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"ElasticNet regression \")\ncheckModelPerformane(ElasticNet())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:29.799973Z","iopub.execute_input":"2022-01-09T11:26:29.800535Z","iopub.status.idle":"2022-01-09T11:26:32.259594Z","shell.execute_reply.started":"2022-01-09T11:26:29.800485Z","shell.execute_reply":"2022-01-09T11:26:32.258561Z"},"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-09T11:26:32.265531Z","iopub.execute_input":"2022-01-09T11:26:32.268392Z","iopub.status.idle":"2022-01-09T11:26:33.887257Z","shell.execute_reply.started":"2022-01-09T11:26:32.268323Z","shell.execute_reply":"2022-01-09T11:26:33.886296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Decision tree**","metadata":{}},{"cell_type":"code","source":"print(\"Decision tree regression\")\ncheckModelPerformane(DecisionTreeRegressor(random_state=42))","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:26:33.892915Z","iopub.execute_input":"2022-01-09T11:26:33.895686Z","iopub.status.idle":"2022-01-09T11:30:13.267142Z","shell.execute_reply.started":"2022-01-09T11:26:33.895616Z","shell.execute_reply":"2022-01-09T11:30:13.265930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3-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-09T11:30:13.268761Z","iopub.execute_input":"2022-01-09T11:30:13.269030Z","iopub.status.idle":"2022-01-09T11:30:13.275101Z","shell.execute_reply.started":"2022-01-09T11:30:13.269000Z","shell.execute_reply":"2022-01-09T11:30:13.274189Z"},"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-09T11:30:13.276468Z","iopub.execute_input":"2022-01-09T11:30:13.277034Z","iopub.status.idle":"2022-01-09T11:30:13.297389Z","shell.execute_reply.started":"2022-01-09T11:30:13.276996Z","shell.execute_reply":"2022-01-09T11:30:13.296213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**SGDRegressor**","metadata":{}},{"cell_type":"markdown","source":"took alot of time to run","metadata":{}},{"cell_type":"code","source":"# print(\"SGDRegressor\")\n# checkModelPerformane(SGDRegressor())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:13.299211Z","iopub.execute_input":"2022-01-09T11:30:13.299635Z","iopub.status.idle":"2022-01-09T11:30:13.317939Z","shell.execute_reply.started":"2022-01-09T11:30:13.299569Z","shell.execute_reply":"2022-01-09T11:30:13.317038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Random Forest**","metadata":{}},{"cell_type":"markdown","source":"Took alot of time","metadata":{}},{"cell_type":"code","source":"# print(\"Random Forest\")\n# checkModelPerformane(RandomForestRegressor())","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:13.320106Z","iopub.execute_input":"2022-01-09T11:30:13.320357Z","iopub.status.idle":"2022-01-09T11:30:13.340432Z","shell.execute_reply.started":"2022-01-09T11:30:13.320330Z","shell.execute_reply":"2022-01-09T11:30:13.339564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**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-09T11:30:13.341864Z","iopub.execute_input":"2022-01-09T11:30:13.342112Z","iopub.status.idle":"2022-01-09T11:30:13.357159Z","shell.execute_reply.started":"2022-01-09T11:30:13.342084Z","shell.execute_reply":"2022-01-09T11:30:13.356190Z"},"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-09T11:30:13.358804Z","iopub.execute_input":"2022-01-09T11:30:13.359186Z","iopub.status.idle":"2022-01-09T11:30:18.585087Z","shell.execute_reply.started":"2022-01-09T11:30:13.359153Z","shell.execute_reply":"2022-01-09T11:30:18.583969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fine tune model","metadata":{}},{"cell_type":"code","source":"# from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:18.590553Z","iopub.execute_input":"2022-01-09T11:30:18.590896Z","iopub.status.idle":"2022-01-09T11:30:18.596062Z","shell.execute_reply.started":"2022-01-09T11:30:18.590860Z","shell.execute_reply":"2022-01-09T11:30:18.594455Z"},"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-09T11:30:18.597633Z","iopub.execute_input":"2022-01-09T11:30:18.597928Z","iopub.status.idle":"2022-01-09T11:30:18.617877Z","shell.execute_reply.started":"2022-01-09T11:30:18.597882Z","shell.execute_reply":"2022-01-09T11:30:18.616961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid_search.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:18.619396Z","iopub.execute_input":"2022-01-09T11:30:18.619993Z","iopub.status.idle":"2022-01-09T11:30:18.634927Z","shell.execute_reply.started":"2022-01-09T11:30:18.619954Z","shell.execute_reply":"2022-01-09T11:30:18.633952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:18.636484Z","iopub.execute_input":"2022-01-09T11:30:18.636790Z","iopub.status.idle":"2022-01-09T11:30:18.652892Z","shell.execute_reply.started":"2022-01-09T11:30:18.636753Z","shell.execute_reply":"2022-01-09T11:30:18.652078Z"},"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-09T11:30:18.654478Z","iopub.execute_input":"2022-01-09T11:30:18.654949Z","iopub.status.idle":"2022-01-09T11:30:18.667860Z","shell.execute_reply.started":"2022-01-09T11:30:18.654916Z","shell.execute_reply":"2022-01-09T11:30:18.666843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.DataFrame(grid_search.cv_results_)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T11:30:18.669282Z","iopub.execute_input":"2022-01-09T11:30:18.669773Z","iopub.status.idle":"2022-01-09T11:30:18.680625Z","shell.execute_reply.started":"2022-01-09T11:30:18.669735Z","shell.execute_reply":"2022-01-09T11:30:18.679816Z"},"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-09T11:30:18.682179Z","iopub.execute_input":"2022-01-09T11:30:18.682760Z","iopub.status.idle":"2022-01-09T11:30:18.700146Z","shell.execute_reply.started":"2022-01-09T11:30:18.682622Z","shell.execute_reply":"2022-01-09T11:30:18.699201Z"},"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-09T11:30:18.701563Z","iopub.execute_input":"2022-01-09T11:30:18.702057Z","iopub.status.idle":"2022-01-09T11:30:18.716149Z","shell.execute_reply.started":"2022-01-09T11:30:18.702023Z","shell.execute_reply":"2022-01-09T11:30:18.714969Z"},"trusted":true},"execution_count":null,"outputs":[]}]}