| import argparse |
| import os |
| import pandas as pd |
| import time |
| import mlflow |
| from mlflow.models.signature import infer_signature |
| from sklearn.model_selection import train_test_split |
| from sklearn.preprocessing import StandardScaler, FunctionTransformer, OneHotEncoder |
| from sklearn.compose import ColumnTransformer |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.pipeline import Pipeline |
|
|
|
|
| if __name__ == "__main__": |
|
|
| |
| EXPERIMENT_NAME = "appointment_cancellation_detector" |
|
|
| |
| mlflow.set_tracking_uri("https://lekhal15-mlflow_demo_1.hf.space/") |
|
|
| |
| mlflow.set_experiment(EXPERIMENT_NAME) |
|
|
| |
| experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME) |
|
|
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
|
|
| print("training model...") |
|
|
| |
| start_time = time.time() |
|
|
| |
| mlflow.sklearn.autolog(log_models=False) |
|
|
| |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--n_estimators", default=1) |
| parser.add_argument("--min_samples_split", default=2) |
| args = parser.parse_args() |
|
|
| |
| df = pd.read_csv( |
| "https://full-stack-assets.s3.eu-west-3.amazonaws.com/Deployment/doctolib_simplified_dataset_01.csv" |
| ) |
|
|
| |
| X = df.iloc[:, 3:-1] |
| y = df.iloc[:, -1].apply(lambda x: 0 if x == "No" else 1) |
|
|
| |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) |
|
|
| print(df.columns) |
|
|
| |
| def date_processing(df): |
| df = df.copy() |
|
|
| |
| df["ScheduledDay"] = pd.to_datetime( |
| df["ScheduledDay"], yearfirst=True, infer_datetime_format=True |
| ) |
| df["AppointmentDay"] = pd.to_datetime( |
| df["AppointmentDay"], yearfirst=True, infer_datetime_format=True |
| ) |
|
|
| |
| df["time_difference_between_scheduled_and_appointment"] = ( |
| df["AppointmentDay"] - df["ScheduledDay"] |
| ).dt.days |
|
|
| |
| df = df.drop(["ScheduledDay", "AppointmentDay"], axis=1) |
|
|
| return df |
|
|
| date_preprocessor = FunctionTransformer(date_processing) |
|
|
| |
| X_train_after_date_processing = date_processing(X_train) |
| categorical_features = X_train_after_date_processing.select_dtypes( |
| "object" |
| ).columns |
| categorical_transformer = OneHotEncoder( |
| drop="first", handle_unknown="error", sparse=False |
| ) |
|
|
| numerical_feature_mask = ~X_train_after_date_processing.columns.isin( |
| X_train_after_date_processing.select_dtypes("object").columns |
| ) |
| numerical_features = X_train_after_date_processing.columns[numerical_feature_mask] |
| numerical_transformer = StandardScaler() |
|
|
| feature_preprocessor = ColumnTransformer( |
| transformers=[ |
| ("categorical_transformer", categorical_transformer, categorical_features), |
| ("numerical_transformer", numerical_transformer, numerical_features), |
| ] |
| ) |
|
|
| |
| n_estimators = int(args.n_estimators) |
| min_samples_split = int(args.min_samples_split) |
|
|
| model = Pipeline( |
| steps=[ |
| ("Dates_preprocessing", date_preprocessor), |
| ("features_preprocessing", feature_preprocessor), |
| ( |
| "Regressor", |
| RandomForestClassifier( |
| n_estimators=n_estimators, min_samples_split=min_samples_split |
| ), |
| ), |
| ] |
| ) |
|
|
| |
| with mlflow.start_run(experiment_id=experiment.experiment_id) as run: |
| model.fit(X_train, y_train) |
| predictions = model.predict(X_train) |
|
|
| |
| mlflow.sklearn.log_model( |
| sk_model=model, |
| artifact_path="appointment_cancellation_detector", |
| registered_model_name="appointment_cancellation_detector_RF", |
| signature=infer_signature(X_train, predictions), |
| ) |
|
|
| print("...Done!") |
| print(f"---Total training time: {time.time()-start_time}") |
|
|