<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
import boto3
import awswrangler as wr
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
## using boto3 via awswrangler since I could not connect to s3 with pandas
df = wr.s3.read_csv(path="s3://mds-s3-student96/ml_data_SYD.csv", boto3_session= session,
                    index_col=0, parse_dates=True)
df.head()
df = df.dropna()
train_df, test_df = train_test_split(df, test_size=0.2, random_state=123)
# EDA
observed = train_df['Observed']
observed = observed.resample('Y').mean()
# Plot Annual Mean Rainfall 
observed.plot.line(xlabel="Year", ylabel="Observed Rainfall", legend=False)
plt.title('Annual Mean Rainfall')
plt.show()
# Examine Descriptive Statistics
train_df.describe()
# Training Ensemble Method
ensemble = RandomForestRegressor()
X_train = train_df.drop('Observed', axis = 1)
y_train = train_df['Observed']
ensemble.fit(X_train, y_train)

# Evaluating Results
X_test = test_df.drop('Observed', axis = 1)
y_test = test_df['Observed']
test_predict = ensemble.predict(X_test)
# Check RMSE of test_predict
mean_squared_error(y_test, test_predict, squared=False)
# Calculate RMSE of individual model in the test set
models = X_test.columns
scores = []
for m in models:
    score = mean_squared_error(y_test, X_test[m], squared=False)
    scores.append(score)
    
test_results = pd.DataFrame({'Models':models, 'RMSE':scores})
test_results<jupyter_output><empty_output><jupyter_text>Given that the Test RMSE of ensemble method is lower than all of the individual model's test set RMSE, we can conclude that we are getting better results with ensemble method.  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.09023551204022229, 'fraction_numerical': 0.014024874305371792, 'mean_word_length': 5.762075134168158, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '445790', 'license': 'MIT', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1140, 'n_words': 424, 'path': 'notebooks/milestone3/Milestone3-Task3.ipynb', 'repo_name': 'UBC-MDS/dsci525_group25', 'size': '3779'}