{"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 pandas as pd \nimport matplotlib.pyplot as plt \nimport numpy as np \nimport seaborn as sns\nimport matplotlib\nfrom tqdm.notebook import tqdm \nfrom matplotlib import rc\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.feature_selection import SelectKBest, mutual_info_regression\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import Lasso, Ridge\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.neural_network import MLPRegressor\nfrom sklearn.model_selection import GridSearchCV\n\nrc(\"font\", **{\"family\": \"sans-serif\", \"sans-serif\": \"DejaVu Sans\"})\nrc(\"figure\", **{\"dpi\": 200})\nrc(\n    \"axes\",\n    **{\"spines.right\": False, \"spines.top\": False, \"xmargin\": 0.0, \"ymargin\": 0.05}\n)","metadata":{"_cell_guid":"994dad23-6226-4b87-a0be-56f9a378ac40","_uuid":"349a1362-e369-49ae-906d-1b5f8a48475d","jupyter":{"outputs_hidden":false},"collapsed":false,"execution":{"iopub.status.busy":"2022-05-10T19:24:51.9888Z","iopub.execute_input":"2022-05-10T19:24:51.991287Z","iopub.status.idle":"2022-05-10T19:24:53.672886Z","shell.execute_reply.started":"2022-05-10T19:24:51.991165Z","shell.execute_reply":"2022-05-10T19:24:53.672001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/eurecom-aml-2022-challenge-1/public/train.csv', low_memory=True)\ndf_test = pd.read_csv('../input/eurecom-aml-2022-challenge-1/public/test_feat.csv', low_memory=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:24:53.67491Z","iopub.execute_input":"2022-05-10T19:24:53.675277Z","iopub.status.idle":"2022-05-10T19:26:38.360312Z","shell.execute_reply.started":"2022-05-10T19:24:53.67523Z","shell.execute_reply":"2022-05-10T19:26:38.358539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Training dataset size: \", len(df))\nprint(\"Testing dataset size: \", len(df_test))","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:26:38.362293Z","iopub.execute_input":"2022-05-10T19:26:38.362611Z","iopub.status.idle":"2022-05-10T19:26:38.371479Z","shell.execute_reply.started":"2022-05-10T19:26:38.362567Z","shell.execute_reply":"2022-05-10T19:26:38.370268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data analysis\n","metadata":{}},{"cell_type":"code","source":"\"\"\"\npd.options.display.max_columns = df.shape[1]\ndf.head()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T20:40:55.327467Z","iopub.execute_input":"2022-05-09T20:40:55.327751Z","iopub.status.idle":"2022-05-09T20:40:55.344892Z","shell.execute_reply.started":"2022-05-09T20:40:55.327697Z","shell.execute_reply":"2022-05-09T20:40:55.343715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All features are numerical and continuous.","metadata":{}},{"cell_type":"code","source":"\"\"\"\nnum_duplicate = len(df) - df.duplicated().value_counts()\nprint(\"Number of duplicates: \", num_duplicate)\n\nnum_nan = df.isnull().sum().sum()\nprint(\"Number of missing values: \", num_nan)\ndf_without_nan = df.dropna()\nprint(\"Number of columns containing missing values: \", len(df)-len(df_without_nan)) \n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T12:56:07.651375Z","iopub.execute_input":"2022-05-09T12:56:07.651675Z","iopub.status.idle":"2022-05-09T12:56:36.784836Z","shell.execute_reply.started":"2022-05-09T12:56:07.651645Z","shell.execute_reply":"2022-05-09T12:56:36.783641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is no duplicate rows in the dataset. But there is more than 1M missing values among around 108,000 records.","metadata":{}},{"cell_type":"code","source":"\"\"\"\n# Plot mean and variance of the features\narray_mean_std = df.describe().iloc[1:3, 1:].values\n\nfig, ax1 = plt.subplots()\n\nfeatures_indexes = np.arange(0, 112)\nax1.set_xlabel('Features')\n\nax1.set_ylabel('Mean', color = 'b')\nax1.bar(features_indexes - 0.4, array_mean_std[0], width = 0.6, color = 'b')\nax1.set_ylim(top=4000, bottom=0)\nax1.tick_params(axis='y', labelcolor='b')\n\nax2 = ax1.twinx()\nax2.set_ylabel('Variance', color = 'g')\nax2.bar(features_indexes + 0.4, array_mean_std[1], width = 0.6, color = 'g')\nax2.set_ylim(top=800, bottom=0)\nax2.tick_params(axis='y', labelcolor='g')\n\nfig.tight_layout()\nplt.title(\"Mean and variance of the features\")\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:02.555887Z","iopub.execute_input":"2022-05-10T19:48:02.557317Z","iopub.status.idle":"2022-05-10T19:48:16.766367Z","shell.execute_reply.started":"2022-05-10T19:48:02.557254Z","shell.execute_reply":"2022-05-10T19:48:16.765356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The distributions of the different features are really diverse as their means and variances vary a lot between each other. In addition, some features are outliers are they have small variance (< 0.5). In the preprocessing, we should remove outliers and standardize features if the scale is not meaningful for some models.","metadata":{}},{"cell_type":"code","source":"\"\"\"\ncorr = df.corr() # Correlation matrix\n\n# Heatmap showing correlation with target\nplt.figure(figsize=(4, 8))\ncmap = sns.diverging_palette(240, 10, n=9, as_cmap=True)\nheatmap = sns.heatmap(corr[['fact_temperature']].sort_values(by='fact_temperature', ascending=False),vmin=-1, vmax=1, annot=False, cmap=cmap)\nheatmap.set_title('Features Correlating with Temperature')\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:49:33.413465Z","iopub.execute_input":"2022-05-09T16:49:33.414116Z","iopub.status.idle":"2022-05-09T16:49:34.105306Z","shell.execute_reply.started":"2022-05-09T16:49:33.414083Z","shell.execute_reply":"2022-05-09T16:49:34.104199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Many features that have negligible values for correlation with the temperature. To train and test our models we may keep only features with high correlation with fact_temperature.","metadata":{}},{"cell_type":"markdown","source":"### Distribution of train and test points in the dataset","metadata":{}},{"cell_type":"code","source":"\"\"\"\ntr_coordinates = df[['fact_latitude','fact_longitude']].drop_duplicates()\nte_coordinates = df_test[['fact_latitude','fact_longitude']].drop_duplicates()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-08T19:55:16.568092Z","iopub.execute_input":"2022-05-08T19:55:16.569334Z","iopub.status.idle":"2022-05-08T19:55:16.771118Z","shell.execute_reply.started":"2022-05-08T19:55:16.569257Z","shell.execute_reply":"2022-05-08T19:55:16.770041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nmerged = te_coordinates.merge(tr_coordinates, how='left', indicator=True)\ntropic_coordinates = merged[merged['_merge']=='left_only']\ncommon_coordinates = merged[merged['_merge']=='both']\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-08T19:55:17.006061Z","iopub.execute_input":"2022-05-08T19:55:17.006706Z","iopub.status.idle":"2022-05-08T19:55:17.056276Z","shell.execute_reply.started":"2022-05-08T19:55:17.006669Z","shell.execute_reply":"2022-05-08T19:55:17.055538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nfig, (ax0, ax1) = plt.subplots(1, 2, figsize=[10, 3], dpi=200, sharex=True, sharey=True)\nax0.scatter(tr_coordinates['fact_longitude'], tr_coordinates['fact_latitude'], \n            s=1, c='tab:green', label='Train data')\nax1.scatter(common_coordinates['fact_longitude'], common_coordinates['fact_latitude'], \n            s=1, c='tab:orange', label='In-domain test data')\nax1.scatter(tropic_coordinates['fact_longitude'], tropic_coordinates['fact_latitude'], \n            s=1, c='tab:red', label='Out-domain test data')\nax0.legend(), ax1.legend()\nfig.suptitle('Distribution of train and test points in the dataset', y=1.01, fontsize=14)\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-08T19:58:16.934638Z","iopub.execute_input":"2022-05-08T19:58:16.935711Z","iopub.status.idle":"2022-05-08T19:58:18.038018Z","shell.execute_reply.started":"2022-05-08T19:58:16.935662Z","shell.execute_reply":"2022-05-08T19:58:18.037101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Data Preprocessing\n","metadata":{}},{"cell_type":"markdown","source":"### Removing outliers","metadata":{}},{"cell_type":"code","source":"df_without_outlier = df.loc[:, df.std()>0.5]","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:16.768146Z","iopub.execute_input":"2022-05-10T19:48:16.768412Z","iopub.status.idle":"2022-05-10T19:48:21.139269Z","shell.execute_reply.started":"2022-05-10T19:48:16.768381Z","shell.execute_reply":"2022-05-10T19:48:21.138344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_nan_new = df_without_outlier.isnull().sum().sum()\nprint(\"Number of missing values: \", num_nan_new)\ndf_without_nan_new = df_without_outlier.dropna()\nprint(\"Number of columns containing missing values: \", len(df_without_outlier)-len(df_without_nan_new)) ","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:21.14076Z","iopub.execute_input":"2022-05-10T19:48:21.140996Z","iopub.status.idle":"2022-05-10T19:48:22.78596Z","shell.execute_reply.started":"2022-05-10T19:48:21.14096Z","shell.execute_reply":"2022-05-10T19:48:22.78495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Handling missing values in the dataset","metadata":{}},{"cell_type":"code","source":"def preprocess_nan(df, mode='naive'):\n    # remove the rows with nan \n    if mode == 'naive':\n        df_tr = df.dropna()\n        X = df_tr.iloc[:, 1:-1].values\n        y = df_tr.iloc[:, -1].values\n    # replace nan with the mean of the corresponding column\n    elif mode == 'mean':\n        column_means = df.mean(axis=0)\n        df_tr = df.fillna(column_means, axis=0)\n        X = df_tr.iloc[:, 1:-1].values\n        y = df_tr.iloc[:, -1].values\n    return X, y, df_tr","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:22.787511Z","iopub.execute_input":"2022-05-10T19:48:22.78776Z","iopub.status.idle":"2022-05-10T19:48:22.795468Z","shell.execute_reply.started":"2022-05-10T19:48:22.787728Z","shell.execute_reply":"2022-05-10T19:48:22.794426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y, df_tr = preprocess_nan(df_without_outlier, mode='naive') # mode = 'naive' or \"mean\"","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:22.797782Z","iopub.execute_input":"2022-05-10T19:48:22.798275Z","iopub.status.idle":"2022-05-10T19:48:24.454953Z","shell.execute_reply.started":"2022-05-10T19:48:22.798233Z","shell.execute_reply":"2022-05-10T19:48:24.453845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Standardising data","metadata":{}},{"cell_type":"code","source":"X_scale = StandardScaler().fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:48:26.021825Z","iopub.execute_input":"2022-05-10T19:48:26.022635Z","iopub.status.idle":"2022-05-10T19:48:30.896644Z","shell.execute_reply.started":"2022-05-10T19:48:26.022583Z","shell.execute_reply":"2022-05-10T19:48:30.895717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reducing the input space","metadata":{}},{"cell_type":"markdown","source":"#### By dimensionality reduction","metadata":{}},{"cell_type":"code","source":"\"\"\"\n# Plot number of components over the cumulative explained variance\npca = PCA().fit(X_scale)\nplt.plot(np.cumsum(pca.explained_variance_ratio_))\nplt.xlabel('Number of components')\nplt.ylabel('Cumulative explained variance');\nplt.title('Fig.2: Number of components over the cumulative explained variance');\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-10T19:50:39.859362Z","iopub.execute_input":"2022-05-10T19:50:39.860485Z","iopub.status.idle":"2022-05-10T19:50:40.281928Z","shell.execute_reply.started":"2022-05-10T19:50:39.860433Z","shell.execute_reply":"2022-05-10T19:50:40.281016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=30)\nX_pca = pca.fit_transform(X_scale)\nprint(\"Explained varaiance for 30 principal components: \", np.sum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:55:11.044069Z","iopub.execute_input":"2022-05-09T16:55:11.044326Z","iopub.status.idle":"2022-05-09T16:55:27.571969Z","shell.execute_reply.started":"2022-05-09T16:55:11.044301Z","shell.execute_reply":"2022-05-09T16:55:27.571254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### By feature selection","metadata":{}},{"cell_type":"code","source":"\"\"\"\ndef select_features(X_train, y_train):\n    fs = SelectKBest(score_func=mutual_info_regression, k=30)\n    fs.fit(X_train, y_train)\n    X_train_fs = fs.transform(X_train)\n    return X_train_fs, fs\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T15:35:49.496854Z","iopub.execute_input":"2022-05-09T15:35:49.497114Z","iopub.status.idle":"2022-05-09T15:35:49.502288Z","shell.execute_reply.started":"2022-05-09T15:35:49.497087Z","shell.execute_reply":"2022-05-09T15:35:49.501154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ndf_samples = df_tr.sample(n = 100000) # select samples randomly (instead of using all the training dataset to perform the feature selection)\nX_samples = df_samples.iloc[:, 1:-1].values\nX_samples_scale = StandardScaler().fit_transform(X_samples)\ny_samples = df_samples.iloc[:, -1].values\n\nX_train_fs, fs = select_features(X_samples_scale, y_samples)\n\n# plot the scores\nplt.bar([i for i in range(len(fs.scores_))], fs.scores_)\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T15:37:52.130694Z","iopub.execute_input":"2022-05-09T15:37:52.130989Z","iopub.status.idle":"2022-05-09T15:39:34.259176Z","shell.execute_reply.started":"2022-05-09T15:37:52.130963Z","shell.execute_reply":"2022-05-09T15:39:34.25821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# Get top 30 most important features \nmask = fs.get_support()\nnew_features = [] \nfeature_names = list((df_tr.drop(['fact_temperature'],axis=1)).columns.values)\nfor bool, feature in zip(mask, feature_names):\n    if bool:\n        new_features.append(feature)\n\n# Select these features for X_fs\ncol_selection_fs = new_features\nX_fs = df_tr[col_selection_fs].values\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T15:41:02.988888Z","iopub.execute_input":"2022-05-09T15:41:02.98941Z","iopub.status.idle":"2022-05-09T15:41:03.446591Z","shell.execute_reply.started":"2022-05-09T15:41:02.989368Z","shell.execute_reply":"2022-05-09T15:41:03.445456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing of the testing dataset","metadata":{}},{"cell_type":"code","source":"# Apply same pre-processing on test dataset\ncol_selection = df_tr.columns[:-1]\ndf_test_new = df_test[col_selection]\nX_test = df_test_new.iloc[:, 1:].values\n\nscaler = StandardScaler()\nscaler.fit(X)\nX_test_scale = scaler.transform(X_test)\nX_test_pca = pca.transform(X_test_scale)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:56:28.233083Z","iopub.execute_input":"2022-05-09T16:56:28.234211Z","iopub.status.idle":"2022-05-09T16:56:29.731662Z","shell.execute_reply.started":"2022-05-09T16:56:28.234172Z","shell.execute_reply":"2022-05-09T16:56:29.731002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Model Selection","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_scale, y, random_state=1, test_size=0.9)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:25:59.799754Z","iopub.execute_input":"2022-05-09T18:25:59.800084Z","iopub.status.idle":"2022-05-09T18:26:02.016693Z","shell.execute_reply.started":"2022-05-09T18:25:59.800059Z","shell.execute_reply":"2022-05-09T18:26:02.015911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ystd = 1.0\ndef compute_rmse(y, ypred, ystd=1.):\n    return np.mean((y - ypred)**2)**0.5 * ystd","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:26:02.01783Z","iopub.execute_input":"2022-05-09T18:26:02.018009Z","iopub.status.idle":"2022-05-09T18:26:02.023553Z","shell.execute_reply.started":"2022-05-09T18:26:02.017987Z","shell.execute_reply":"2022-05-09T18:26:02.022297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"############ Try different models ############\nmodel = Lasso(alpha=0.1)  # complexity 0(n)\n# model = Ridge(alpha=0.1)  # complexity 0(n)\n# model = RandomForestRegressor(max_depth=10, min_samples_leaf=5, n_estimators=10)\n# model = MLPRegressor(max_iter=30, validation_fraction=0.2, early_stopping=True, tol=1e-3, n_iter_no_change=5,\n#                    hidden_layer_sizes=(300, 100, 100, ), learning_rate_init=1e-3, random_state=0, verbose=1)     # complexity 0(n*n1 + n1*n2* + ...) ~= O(Kn)\n###############################################\n\nmodel.fit(X_train, y_train)\ny_train_pred = model.predict(X_train)\ny_val_pred = model.predict(X_val)\nprint(f'Train RMSE: {compute_rmse(y_train, y_train_pred, ystd=1.0):.3f}')\nprint(f'Valid RMSE: {compute_rmse(y_val, y_val_pred, ystd=1.0):.3f}')","metadata":{"execution":{"iopub.status.busy":"2022-05-09T18:26:02.025497Z","iopub.execute_input":"2022-05-09T18:26:02.025934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameter Optimisation","metadata":{}},{"cell_type":"markdown","source":"Note: Skip this part is no optimisation is made with grid search.","metadata":{}},{"cell_type":"code","source":"def get_cv_idx(n, test_size=0.2, n_splits=5):\n    train_idx, test_idx = [], []\n    for _ in range(n_splits):\n        idx = np.random.permutation(n)\n        train_size = int(n * (1 - test_size)) if isinstance(test_size, float) else n - test_size\n        train_idx.append(idx[:train_size])\n        test_idx.append(idx[train_size:])\n    return train_idx, test_idx","metadata":{"execution":{"iopub.status.busy":"2022-05-09T15:58:45.939512Z","iopub.execute_input":"2022-05-09T15:58:45.940522Z","iopub.status.idle":"2022-05-09T15:58:45.947509Z","shell.execute_reply.started":"2022-05-09T15:58:45.940447Z","shell.execute_reply":"2022-05-09T15:58:45.946314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idx, cv_idx = get_cv_idx(len(X_train), test_size=0.9, n_splits=5)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:09:30.254159Z","iopub.execute_input":"2022-05-09T16:09:30.254433Z","iopub.status.idle":"2022-05-09T16:09:30.307981Z","shell.execute_reply.started":"2022-05-09T16:09:30.254406Z","shell.execute_reply":"2022-05-09T16:09:30.30685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"########## grid search parameters ########## \nparam_grid_lasso = {\n    \"alpha\": [0.001, 0.01, 0.1, 1.0]\n}\n\nparam_grid_ridge = {\n    \"alpha\": [0.01, 0.1, 1.0, 10.0]\n}\n\nparam_grid_rf = {\n   'max_depth' : [5, 8, 10], 'min_samples_leaf': [10, 50, 100],'n_estimators': [200, 250, 300]\n}\n########## grid search parameters ########## \n\nbest_model = GridSearchCV(model, \n                      param_grid_lasso, # TO MODIFY\n                      n_jobs=-1, \n                      verbose=1,\n                      cv=zip(train_idx, cv_idx), \n                      scoring='neg_root_mean_squared_error').fit(X_train, y_train)\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:09:41.689936Z","iopub.execute_input":"2022-05-09T16:09:41.69023Z","iopub.status.idle":"2022-05-09T16:09:42.689281Z","shell.execute_reply.started":"2022-05-09T16:09:41.6902Z","shell.execute_reply":"2022-05-09T16:09:42.688481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Best parameters set found on cv set:\")\nprint(best_model.best_params_)\nprint()\nprint(\"Grid scores on cv set:\")\nmeans = best_model.cv_results_[\"mean_test_score\"]\nstds = best_model.cv_results_[\"std_test_score\"]\nfor mean, std, params in zip(means, stds, best_model.cv_results_[\"params\"]):\n    print(\"%0.3f (+/-%0.03f) for %r\" % (-mean * ystd, (std * ystd) * 2, params))\nprint()\nprint(\"Error on the validation set\")\ny_val_pred = best_model.predict(X_val)\nprint(f'Valid RMSE: {compute_rmse(y_val, y_val_pred, ystd=1.0):.3f}')","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:09:48.589593Z","iopub.execute_input":"2022-05-09T16:09:48.589902Z","iopub.status.idle":"2022-05-09T16:09:48.658448Z","shell.execute_reply.started":"2022-05-09T16:09:48.589861Z","shell.execute_reply":"2022-05-09T16:09:48.657641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Evaluation","metadata":{}},{"cell_type":"code","source":"y_test_pred = best_model.predict(X_test_pca)  # If parameter optimisation using grid search\n# y_test_pred = model.predict(X_test_pca) # If not using the \"Parameter Optimisation\" part","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:10:10.474088Z","iopub.execute_input":"2022-05-09T16:10:10.474426Z","iopub.status.idle":"2022-05-09T16:10:10.500243Z","shell.execute_reply.started":"2022-05-09T16:10:10.474366Z","shell.execute_reply":"2022-05-09T16:10:10.499598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n# Submission\n","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame(data={'index': df_test['index'].values,\n                                   'fact_temperature': y_test_pred.squeeze()})\n# Save the predictions into a csv file\n# Notice that this file should be saved under the directory `/kaggle/working` \n# so that you can download it later\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:10:13.959289Z","iopub.execute_input":"2022-05-09T16:10:13.959513Z","iopub.status.idle":"2022-05-09T16:10:15.569018Z","shell.execute_reply.started":"2022-05-09T16:10:13.959489Z","shell.execute_reply":"2022-05-09T16:10:15.567826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check the submission file\n! head -6 \"/kaggle/working/submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-05-09T16:10:17.909223Z","iopub.execute_input":"2022-05-09T16:10:17.909508Z","iopub.status.idle":"2022-05-09T16:10:18.246288Z","shell.execute_reply.started":"2022-05-09T16:10:17.909478Z","shell.execute_reply":"2022-05-09T16:10:18.24531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}