evaluate_example.py
1 from sklearn.datasets import load_iris 2 from sklearn.linear_model import LogisticRegression 3 from sklearn.model_selection import train_test_split 4 5 import mlflow 6 from mlflow.models import infer_signature 7 8 X, y = load_iris(return_X_y=True, as_frame=True) 9 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) 10 X_test_1, X_test_2, y_test_1, y_test_2 = train_test_split( 11 X_test, y_test, test_size=0.5, random_state=42 12 ) 13 model = LogisticRegression().fit(X_train, y_train) 14 15 predictions = model.predict(X_train) 16 signature = infer_signature(X_train, predictions) 17 18 with mlflow.start_run() as run: 19 model_info = mlflow.sklearn.log_model(model, name="model", signature=signature) 20 print(model_info.name) 21 22 # Evaluate the model URI 23 mlflow.evaluate( 24 model_info.model_uri, 25 X_test_1.assign(label=y_test_1), 26 targets="label", 27 model_type="classifier", 28 evaluators=["default"], 29 ) 30 print(mlflow.get_logged_model(model_info.model_id)) 31 32 # Evaluate the pyfunc model object 33 model = mlflow.pyfunc.load_model(model_info.model_uri) 34 assert model.model_id is not None 35 mlflow.evaluate( 36 model, 37 X_test_2.assign(label=y_test_2), 38 targets="label", 39 model_type="classifier", 40 evaluators=["default"], 41 ) 42 print(mlflow.get_logged_model(model_info.model_id))