<jupyter_start><jupyter_text>Task 3  Imports<jupyter_code>import numpy as np
import pandas as pd
from joblib import dump, load
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams.update({'font.size': 16, 'axes.labelweight': 'bold', 'figure.figsize': (8,6)})<jupyter_output><empty_output><jupyter_text>Part 1: Recall as a final goal of this project. We want to build and deploy ensemble machine learning models in the cloud, where features are outputs of different climate models and the target is the actual rainfall observation. In this milestone, you'll actually build these ensemble machine learning models in the cloud.  **Your tasks:**1. Read the data CSV from your s3 bucket. 2. Drop rows with nans. 3. Split the data into train (80%) and test (20%) portions with `random_state=123`. 4. Carry out EDA of your choice on the train split. 5. Train ensemble machine learning model using `RandomForestRegressor` and evaluate with metric of your choice (e.g., `RMSE`) by considering `Observed` as the target column. 6. Discuss your results. Are you getting better results with ensemble models compared to the individual climate models? > Recall that individual columns in the data are predictions of different climate models.<jupyter_code>## You could download it from your bucket, or you can use the file that I have in my bucket. 
## You should be able to access it from my bucket using your key and secret
aws_credentials ={"key": "<KEY>","secret": "<KEY>"} 
df = pd.read_csv("s3://mds-s3-student60/ml_data_SYD.csv", index_col=0, parse_dates=True)
df = df.dropna()
X = df.drop(columns=['observed'])
y = df['observed']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)
X_train.describe()
model = RandomForestRegressor(n_estimators=100, max_depth=20)
model.fit(X_train,y_train)
y_predicted = model.predict(X_train)
train_rms = mean_squared_error(y_train, y_predicted, squared=False)
train_rms
y_predicted = model.predict(X_test)
test_rms = mean_squared_error(y_test, y_predicted, squared=False)
test_rms
for col in X_test.columns.tolist():
    print(f"model {col} RMSE: {mean_squared_error(y_test, X_test[col], squared=False)}")<jupyter_output>model ACCESS-CM2 RMSE: 11.03899918385165
model ACCESS-ESM1-5 RMSE: 10.695305350555104
model AWI-ESM-1-1-LR RMSE: 10.996615645823058
model BCC-CSM2-MR RMSE: 10.761381439070494
model BCC-ESM1 RMSE: 10.61557814309429
model CMCC-CM2-HR4 RMSE: 10.643203823048916
model CMCC-CM2-SR5 RMSE: 11.480614439621686
model CMCC-ESM2 RMSE: 11.246493184224887
model CanESM5 RMSE: 11.151317797663006
model EC-Earth3-Veg-LR RMSE: 10.453606322852785
model FGOALS-g3 RMSE: 9.687787801590718
model GFDL-CM4 RMSE: 10.5116821515291
model INM-CM4-8 RMSE: 11.451635236712267
model INM-CM5-0 RMSE: 12.25022302545882
model KIOST-ESM RMSE: 9.600479601763467
model MIROC6 RMSE: 11.352975765167592
model MPI-ESM-1-2-HAM RMSE: 10.932004152106805
model MPI-ESM1-2-HR RMSE: 9.96982272985151
model MPI-ESM1-2-LR RMSE: 10.260886028100495
model MRI-ESM2-0 RMSE: 9.92279490286555
model NESM3 RMSE: 9.978136636933371
model NorESM2-LM RMSE: 10.410145010506907
model NorESM2-MM RMSE: 10.93973997074141
model SAM0-UNICON RMSE: 11.678749130003[...]<jupyter_text>> Yes, we are getting better results with the ensemble of models compared to the individual models as can be seen above.  Part 2:  Preparation for deploying model next week  Complete task 4 from the milestone3 before coming here We’ve found ```n_estimators=100, max_depth=5``` to be the best hyperparameter settings with MLlib (from the task 4 from milestone3), here we then use the same hyperparameters to train a scikit-learn model.<jupyter_code>model = RandomForestRegressor(n_estimators=100, max_depth=20)
model.fit(X_train, y_train)
print(f"Train RMSE: {mean_squared_error(y_train, model.predict(X_train), squared=False):.2f}")
print(f" Test RMSE: {mean_squared_error(y_test, model.predict(X_test), squared=False):.2f}")
# ready to deploy
dump(model, "model.joblib")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09463344513655965, 'fraction_numerical': 0.11403929084810734, 'mean_word_length': 5.969949916527546, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '500092', 'license': 'MIT', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1454, 'n_words': 452, 'path': 'Milestone3-Task3.ipynb', 'repo_name': 'UBC-MDS/DSCI_525_group21', 'size': '4224'}