Created
May 29, 2024 21:00
-
-
Save ayqazi/beb5cea5dafc768a4522797105c9f72f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mlflow | |
from mlflow.models import infer_signature | |
import pandas as pd | |
from sklearn import datasets | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score | |
mlflow.set_tracking_uri(uri="http://localhost:5000") | |
# Load the Iris dataset | |
X, y = datasets.load_iris(return_X_y=True) | |
# Split the data into training and test sets | |
X_train, X_test, y_train, y_test = train_test_split( | |
X, y, test_size=0.2, random_state=42 | |
) | |
# Define the model hyperparameters | |
params = { | |
"solver": "lbfgs", | |
"max_iter": 1000, | |
"multi_class": "auto", | |
"random_state": 8888, | |
} | |
# Train the model | |
lr = LogisticRegression(**params) | |
lr.fit(X_train, y_train) | |
# Predict on the test set | |
y_pred = lr.predict(X_test) | |
# Calculate metrics | |
accuracy = accuracy_score(y_test, y_pred) | |
# Create a new MLflow Experiment | |
mlflow.set_experiment("MLflow Quickstart") | |
# Start an MLflow run | |
with mlflow.start_run(): | |
# Log the hyperparameters | |
mlflow.log_params(params) | |
# Log the loss metric | |
mlflow.log_metric("accuracy", accuracy) | |
# Set a tag that we can use to remind ourselves | |
# what this run was for | |
mlflow.set_tag("Training Info", "Basic LR model for iris data") | |
# Infer the model signature | |
signature = infer_signature(X_train, lr.predict(X_train)) | |
# Log the model | |
model_info = mlflow.sklearn.log_model( | |
sk_model=lr, | |
artifact_path="iris_model", | |
signature=signature, | |
input_example=X_train, | |
registered_model_name="tracking-quickstart", | |
) | |
# Load the model back for predictions as a generic | |
# Python Function model | |
loaded_model = mlflow.pyfunc.load_model(model_info.model_uri) | |
predictions = loaded_model.predict(X_test) | |
iris_feature_names = datasets.load_iris().feature_names | |
result = pd.DataFrame(X_test, columns=iris_feature_names) | |
result["actual_class"] = y_test | |
result["predicted_class"] = predictions | |
print(result[:4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment