Chapter 07
duration prediction
NotebookPython 3 (ipykernel)14 cells
In [1]python · cell 1
python
!python -VOutput
Python 3.12.1
In [2]python · cell 2
python
import pandas as pdIn [3]python · cell 3
python
import pickleIn [4]python · cell 4
python
from sklearn.feature_extraction import DictVectorizer
from sklearn.metrics import root_mean_squared_errorIn [5]python · cell 5
python
import mlflow
mlflow.set_tracking_uri("http://localhost:5000")
mlflow.set_experiment("nyc-taxi-experiment")Output
2025/05/22 12:10:31 INFO mlflow.tracking.fluent: Experiment with name 'nyc-taxi-experiment' does not exist. Creating a new experiment.
<Experiment: artifact_location='mlflow-artifacts:/1', creation_time=1747915831003, experiment_id='1', last_update_time=1747915831003, lifecycle_stage='active', name='nyc-taxi-experiment', tags={}>In [16]python · cell 6
python
def read_dataframe(filename):
df = pd.read_parquet(filename)
df['duration'] = df.lpep_dropoff_datetime - df.lpep_pickup_datetime
df.duration = df.duration.apply(lambda td: td.total_seconds() / 60)
df = df[(df.duration >= 1) & (df.duration <= 60)]
categorical = ['PULocationID', 'DOLocationID']
df[categorical] = df[categorical].astype(str)
df['PU_DO'] = df['PULocationID'] + '_' + df['DOLocationID']
return dfIn [17]python · cell 7
python
df_train = read_dataframe('https://d37ci6vzurychx.cloudfront.net/trip-data/green_tripdata_2021-01.parquet')
df_val = read_dataframe('https://d37ci6vzurychx.cloudfront.net/trip-data/green_tripdata_2021-02.parquet')In [18]python · cell 8
python
categorical = ['PU_DO'] #'PULocationID', 'DOLocationID']
numerical = ['trip_distance']
dv = DictVectorizer()
train_dicts = df_train[categorical + numerical].to_dict(orient='records')
X_train = dv.fit_transform(train_dicts)
val_dicts = df_val[categorical + numerical].to_dict(orient='records')
X_val = dv.transform(val_dicts)In [19]python · cell 9
python
target = 'duration'
y_train = df_train[target].values
y_val = df_val[target].valuesIn [21]python · cell 10
python
import xgboost as xgbIn [26]python · cell 11
python
from pathlib import PathIn [28]python · cell 12
python
models_folder = Path('models')
models_folder.mkdir(exist_ok=True)In [29]python · cell 13
python
with mlflow.start_run():
train = xgb.DMatrix(X_train, label=y_train)
valid = xgb.DMatrix(X_val, label=y_val)
best_params = {
'learning_rate': 0.09585355369315604,
'max_depth': 30,
'min_child_weight': 1.060597050922164,
'objective': 'reg:linear',
'reg_alpha': 0.018060244040060163,
'reg_lambda': 0.011658731377413597,
'seed': 42
}
mlflow.log_params(best_params)
booster = xgb.train(
params=best_params,
dtrain=train,
num_boost_round=30,
evals=[(valid, 'validation')],
early_stopping_rounds=50
)
y_pred = booster.predict(valid)
rmse = root_mean_squared_error(y_val, y_pred)
mlflow.log_metric("rmse", rmse)
with open("models/preprocessor.b", "wb") as f_out:
pickle.dump(dv, f_out)
mlflow.log_artifact("models/preprocessor.b", artifact_path="preprocessor")
mlflow.xgboost.log_model(booster, artifact_path="models_mlflow")Output
/usr/local/python/3.12.1/lib/python3.12/site-packages/xgboost/callback.py:386: UserWarning: [12:17:00] WARNING: /workspace/src/objective/regression_obj.cu:250: reg:linear is now deprecated in favor of reg:squarederror. self.starting_round = model.num_boosted_rounds()
[0] validation-rmse:11.44482 [1] validation-rmse:10.77202 [2] validation-rmse:10.18363 [3] validation-rmse:9.67396 [4] validation-rmse:9.23166 [5] validation-rmse:8.84808 [6] validation-rmse:8.51883 [7] validation-rmse:8.23597 [8] validation-rmse:7.99320 [9] validation-rmse:7.78709 [10] validation-rmse:7.61022 [11] validation-rmse:7.45952 [12] validation-rmse:7.33049 [13] validation-rmse:7.22098 [14] validation-rmse:7.12713 [15] validation-rmse:7.04752 [16] validation-rmse:6.98005 [17] validation-rmse:6.92232 [18] validation-rmse:6.87112 [19] validation-rmse:6.82740 [20] validation-rmse:6.78995 [21] validation-rmse:6.75792 [22] validation-rmse:6.72994 [23] validation-rmse:6.70547 [24] validation-rmse:6.68390 [25] validation-rmse:6.66421 [26] validation-rmse:6.64806 [27] validation-rmse:6.63280 [28] validation-rmse:6.61924 [29] validation-rmse:6.60773
/usr/local/python/3.12.1/lib/python3.12/site-packages/mlflow/xgboost/__init__.py:168: UserWarning: [12:17:36] WARNING: /workspace/src/c_api/c_api.cc:1427: Saving model in the UBJSON format as default. You can use file extension: `json`, `ubj` or `deprecated` to choose between formats. xgb_model.save_model(model_data_path) [31m2025/05/22 12:17:41 WARNING mlflow.models.model: Model logged without a signature and input example. Please set `input_example` parameter when logging the model to auto infer the model signature.[0m
🏃 View run stylish-auk-139 at: http://localhost:5000/#/experiments/1/runs/494792a9a00b48b1b1ef16a3dd8aeebe 🧪 View experiment at: http://localhost:5000/#/experiments/1
In [ ]python · cell 14
python
