Chapter 10
random forest
NotebookPython 3 (ipykernel)12 cells
In [1]python · cell 1
python
import pickle
import pandas as pd
from sklearn.feature_extraction import DictVectorizer
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_errorIn [24]python · cell 2
python
from sklearn.pipeline import make_pipelineIn [2]python · cell 3
python
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")
mlflow.set_experiment("green-taxi-duration")Output
2022/06/01 12:27:06 INFO mlflow.tracking.fluent: Experiment with name 'green-taxi-duration' does not exist. Creating a new experiment.
<Experiment: artifact_location='s3://mlflow-models-alexey/1', experiment_id='1', lifecycle_stage='active', name='green-taxi-duration', tags={}>In [3]python · cell 4
python
def read_dataframe(filename: str):
df = pd.read_parquet(filename)
df['duration'] = df.lpep_dropoff_datetime - df.lpep_pickup_datetime
df.duration = df.duration.dt.total_seconds() / 60
df = df[(df.duration >= 1) & (df.duration <= 60)]
categorical = ['PULocationID', 'DOLocationID']
df[categorical] = df[categorical].astype(str)
return df
def prepare_dictionaries(df: pd.DataFrame):
df['PU_DO'] = df['PULocationID'] + '_' + df['DOLocationID']
categorical = ['PU_DO']
numerical = ['trip_distance']
dicts = df[categorical + numerical].to_dict(orient='records')
return dictsIn [5]python · cell 5
python
df_train = read_dataframe('data/green_tripdata_2021-01.parquet')
df_val = read_dataframe('data/green_tripdata_2021-02.parquet')
target = 'duration'
y_train = df_train[target].values
y_val = df_val[target].values
dict_train = prepare_dictionaries(df_train)
dict_val = prepare_dictionaries(df_val)In [26]python · cell 6
python
with mlflow.start_run():
params = dict(max_depth=20, n_estimators=100, min_samples_leaf=10, random_state=0)
mlflow.log_params(params)
pipeline = make_pipeline(
DictVectorizer(),
RandomForestRegressor(**params, n_jobs=-1)
)
pipeline.fit(dict_train, y_train)
y_pred = pipeline.predict(dict_val)
rmse = mean_squared_error(y_pred, y_val, squared=False)
print(params, rmse)
mlflow.log_metric('rmse', rmse)
mlflow.sklearn.log_model(pipeline, artifact_path="model")Output
{'max_depth': 20, 'n_estimators': 100, 'min_samples_leaf': 10, 'random_state': 0} 15.136777093556063
In [10]python · cell 7
python
from mlflow.tracking import MlflowClientIn [20]python · cell 8
python
MLFLOW_TRACKING_URI = 'http://127.0.0.1:5000'
RUN_ID = 'b4d3bca8aa8e46a6b8257fe4541b1136'
client = MlflowClient(tracking_uri=MLFLOW_TRACKING_URI)In [21]python · cell 9
python
path = client.download_artifacts(run_id=RUN_ID, path='dict_vectorizer.bin')In [22]python · cell 10
python
with open(path, 'rb') as f_out:
dv = pickle.load(f_out)In [23]python · cell 11
python
dvOutput
DictVectorizer()
In [ ]python · cell 12
python
