.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/integrations/plot_skore_mlflow_project.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_integrations_plot_skore_mlflow_project.py: .. _example_skore_mlflow_project: ========================================== Store and retrieve Skore reports in MLflow ========================================== The primilarly goal of `skore` is to create data science artifacts in the form of structured reports. Those reports can easily be used programmatically via the Python API. A subsequent aim is to store those reports that you create during your experiment cycle in a way that it is easy to retrieve them later on. Skore provides two natives ways to store reports: locally or on Skore Hub. Skore Hub provides additional interactivity features for you to explore, compare and share visual insights. In addition, Skore also provides an MLflow integration to store the content of reports directly as MLflow artifacts. This example shows how to persist reports in MLflow using :class:`~skore.Project` in ``mode="mlflow"``: log reports as MLflow runs and inspect them. To run this example against your own MLflow tracking server, use: .. code-block:: bash TRACKING_URI= PROJECT= python plot_skore_mlflow_project.py To try it locally, start an MLflow server with ``uvx mlflow server`` and set ``TRACKING_URI=http://127.0.0.1:5000``. For more setup details, see the `MLflow quickstart `_. .. GENERATED FROM PYTHON SOURCE LINES 34-39 Create a Skore report ===================== First, we start by creating a Skore report by evaluating a logistic regression model on the iris dataset using some cross-validation. .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. code-block:: Python from sklearn.datasets import load_iris from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from skore import evaluate X, y = load_iris(return_X_y=True, as_frame=True) estimator = make_pipeline(StandardScaler(), LogisticRegression()) report = evaluate(estimator, X, y, splitter=5) .. GENERATED FROM PYTHON SOURCE LINES 52-58 Store the Skore reports as MLflow artifacts =========================================== Now, we will store the different items of the Skore report as MLflow artifacts. For this matter, you need to create a :class:`~skore.Project` in ``mode="mlflow"`` and pass the information regarding the MLflow tracking server. .. GENERATED FROM PYTHON SOURCE LINES 59-73 .. code-block:: Python import io # MLflow/Alembic emits verbose DB initialization logs; silence them so the # example page focuses on skore usage rather than backend startup details. with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): # This creates an MLflow experiment with name `PROJECT`: project = Project( PROJECT, mode="mlflow", tracking_uri=TRACKING_URI, ) .. GENERATED FROM PYTHON SOURCE LINES 99-101 Once the project created, the same API used to store a report locally or on Skore Hub applies. .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: Python project.put("logistic-regression", report) .. GENERATED FROM PYTHON SOURCE LINES 104-115 Once that you stored the report, the artifacts will be available on the MLflow tracking server at the following URL: ``http:///#//1/runs//artifacts`` To find the run ID attributed by MLflow, you can check the section below. Retrieve the Skore report from MLflow tracking server ===================================================== Like for the other modes (local and Skore Hub), you can access what is stored in the project via the :meth:`~skore.Project.summarize` method. .. GENERATED FROM PYTHON SOURCE LINES 115-123 .. code-block:: Python import pandas as pd summary = project.summarize() pandas_summary = pd.DataFrame(summary).reset_index() pandas_summary[["id", "key", "report_type", "learner", "ml_task", "dataset"]] .. raw:: html
id key report_type learner ml_task dataset
0 2f0c4cf24011478c9f88ae069b13052d logistic-regression cross-validation LogisticRegression multiclass-classification 8f9eb48c


.. GENERATED FROM PYTHON SOURCE LINES 129-130 Then, you can retrieve a Skore report using the `"id"` column: .. GENERATED FROM PYTHON SOURCE LINES 130-134 .. code-block:: Python (run_id,) = pandas_summary["id"] loaded_report = project.get(run_id) loaded_report.metrics.summarize().frame() .. raw:: html
LogisticRegression
mean std
Metric Label
Accuracy 0.960000 0.043461
Precision 0 1.000000 0.000000
1 0.945455 0.081312
2 0.944444 0.078567
Recall 0 1.000000 0.000000
1 0.940000 0.089443
2 0.940000 0.089443
ROC AUC 0 1.000000 0.000000
1 0.996000 0.005477
2 0.996000 0.005477
Log loss 0.150357 0.040373
Fit time (s) 0.006280 0.000681
Predict time (s) 0.001043 0.000696


.. GENERATED FROM PYTHON SOURCE LINES 135-137 You can directly use MLflow to access information stored in the MLflow tracking server. .. GENERATED FROM PYTHON SOURCE LINES 137-142 .. code-block:: Python import mlflow mlflow_run = mlflow.get_run(run_id) mlflow_run.data.metrics .. rst-class:: sphx-glr-script-out .. code-block:: none {'accuracy': 0.96, 'accuracy_std': 0.043461349368017654, 'log_loss': 0.15035720436851405, 'log_loss_std': 0.040372774208441146, 'recall': 0.96, 'recall_std': 0.043461349368017654, 'precision': 0.96, 'precision_std': 0.043461349368017654, 'roc_auc': 0.9977777777777778, 'roc_auc_std': 0.0025153847605937046, 'fit_time': 0.006280393800011552, 'predict_time': 0.001042809599971406} .. GENERATED FROM PYTHON SOURCE LINES 143-152 Conclusion ========== Skore offers native integrations locally and with Skore Hub. However, if you are already using MLflow, you can use the ``mode="mlflow"`` option to store reports as MLflow artifacts directly inside the tracking server. However, you will not benefit from the interactive user interface provided by Skore Hub. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 24.562 seconds) .. _sphx_glr_download_auto_examples_integrations_plot_skore_mlflow_project.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_skore_mlflow_project.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_skore_mlflow_project.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_skore_mlflow_project.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_