Spaces:
Running
Running
File size: 6,058 Bytes
2e28476 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
#!/usr/bin/env python
# coding: utf-8
# # <span style="font-width:bold; font-size: 3rem; color:#333;">Training Pipeline</span>
#
# ## ποΈ This notebook is divided into the following sections:
#
# 1. Select features for the model and create a Feature View with the selected features
# 2. Create training data using the feature view
# 3. Train model
# 4. Evaluate model performance
# 5. Save model to model registry
# ### <span style='color:#ff5f27'> π Imports
# In[1]:
import os
from datetime import datetime, timedelta
import pandas as pd
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
from xgboost import plot_importance
from sklearn.metrics import mean_squared_error, r2_score
import hopsworks
from functions import util
import warnings
warnings.filterwarnings("ignore")
# ## <span style="color:#ff5f27;"> π‘ Connect to Hopsworks Feature Store </span>
# In[2]:
project = hopsworks.login()
api_key = os.getenv('HOPSWORKS_API_KEY')
project_name = os.getenv('HOPSWORKS_PROJECT')
project = hopsworks.login(project=project_name, api_key_value=api_key)
fs = project.get_feature_store()
secrets = util.secrets_api(project.name)
# In[3]:
# Retrieve feature groups
air_quality_fg = fs.get_feature_group(
name='air_quality',
version=1,
)
weather_fg = fs.get_feature_group(
name='weather',
version=1,
)
# ---
#
# ## <span style="color:#ff5f27;"> π Feature View Creation and Retrieving </span>
# In[4]:
# Select features for training data.
selected_features = air_quality_fg.select(['pm25', 'past_air_quality']).join(weather_fg.select_all(), on=['city'])
selected_features.show(10)
# In[9]:
feature_view = fs.get_or_create_feature_view(
name='air_quality_fv',
description="weather features with air quality as the target",
version=1,
labels=['pm25'],
query=selected_features,
)
# In[10]:
start_date_test_data = "2024-03-01"
# Convert string to datetime object
test_start = datetime.strptime(start_date_test_data, "%Y-%m-%d")
# In[11]:
X_train, X_test, y_train, y_test = feature_view.train_test_split(
test_start=test_start
)
# In[12]:
X_train
# In[13]:
# Drop the index columns - 'date' (event_time) and 'city' (primary key)
train_features = X_train.drop(['date', 'city'], axis=1)
test_features = X_test.drop(['date', 'city'], axis=1)
# In[14]:
y_train
# The `Feature View` is now saved in Hopsworks and you can retrieve it using `FeatureStore.get_feature_view(name='...', version=1)`.
# ---
# ## <span style="color:#ff5f27;">𧬠Modeling</span>
#
# We will train a regression model to predict pm25 using our 4 features (wind_speed, wind_dir, temp, precipitation)
# In[16]:
# Creating an instance of the XGBoost Regressor
xgb_regressor = XGBRegressor()
# Fitting the XGBoost Regressor to the training data
xgb_regressor.fit(train_features, y_train)
# In[17]:
# Predicting target values on the test set
y_pred = xgb_regressor.predict(test_features)
# Calculating Mean Squared Error (MSE) using sklearn
mse = mean_squared_error(y_test.iloc[:,0], y_pred)
print("MSE:", mse)
# Calculating R squared using sklearn
r2 = r2_score(y_test.iloc[:,0], y_pred)
print("R squared:", r2)
# In[18]:
df = y_test
df['predicted_pm25'] = y_pred
# In[19]:
df['date'] = X_test['date']
df = df.sort_values(by=['date'])
df.head(5)
# In[20]:
# Creating a directory for the model artifacts if it doesn't exist
model_dir = "air_quality_model"
if not os.path.exists(model_dir):
os.mkdir(model_dir)
images_dir = model_dir + "/images"
if not os.path.exists(images_dir):
os.mkdir(images_dir)
# In[21]:
file_path = images_dir + "/pm25_hindcast.png"
plt = util.plot_air_quality_forecast("lahore", "pakistan-lahore-cantonment", df, file_path, hindcast=True)
plt.show()
# In[22]:
# Plotting feature importances using the plot_importance function from XGBoost
plot_importance(xgb_regressor, max_num_features=5)
feature_importance_path = images_dir + "/feature_importance.png"
plt.savefig(feature_importance_path)
plt.show()
# ---
# ## <span style='color:#ff5f27'>π Model Registry</span>
#
# One of the features in Hopsworks is the model registry. This is where you can store different versions of models and compare their performance. Models from the registry can then be served as API endpoints.
# ### <span style="color:#ff5f27;">βοΈ Model Schema</span>
# The model needs to be set up with a [Model Schema](https://docs.hopsworks.ai/machine-learning-api/latest/generated/model_schema/), which describes the inputs and outputs for a model.
#
# A Model Schema can be automatically generated from training examples, as shown below.
# In[23]:
from hsml.schema import Schema
from hsml.model_schema import ModelSchema
# Creating input and output schemas using the 'Schema' class for features (X) and target variable (y)
input_schema = Schema(X_train)
output_schema = Schema(y_train)
# Creating a model schema using 'ModelSchema' with the input and output schemas
model_schema = ModelSchema(input_schema=input_schema, output_schema=output_schema)
# Converting the model schema to a dictionary representation
schema_dict = model_schema.to_dict()
# In[24]:
# Saving the XGBoost regressor object as a json file in the model directory
xgb_regressor.save_model(model_dir + "/model.json")
# In[25]:
res_dict = {
"MSE": str(mse),
"R squared": str(r2),
}
# In[26]:
mr = project.get_model_registry()
# Creating a Python model in the model registry named 'air_quality_xgboost_model'
aq_model = mr.python.create_model(
name="air_quality_xgboost_model",
metrics= res_dict,
model_schema=model_schema,
input_example=X_test.sample().values,
description="Air Quality (PM2.5) predictor",
)
# Saving the model artifacts to the 'air_quality_model' directory in the model registry
aq_model.save(model_dir)
# ---
# ## <span style="color:#ff5f27;">βοΈ **Next:** Part 04: Batch Inference</span>
#
# In the following notebook you will use your model for Batch Inference.
#
# In[ ]:
|