.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/getting_started/plot_skore_getting_started.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_getting_started_plot_skore_getting_started.py: .. _example_skore_getting_started: ====================== Skore: getting started ====================== .. GENERATED FROM PYTHON SOURCE LINES 10-26 This getting started guide illustrates how to use skore and why: #. Get assistance when developing your ML/DS projects to avoid common pitfalls and follow recommended practices. * :class:`skore.EstimatorReport`: get an insightful report on your estimator * :class:`skore.CrossValidationReport`: get an insightful report on your cross-validation results * :class:`skore.ComparisonReport`: benchmark your skore estimator reports * :func:`skore.train_test_split`: get diagnostics when splitting your data #. Track your ML/DS results using skore's :class:`~skore.Project` (for storage). .. GENERATED FROM PYTHON SOURCE LINES 28-34 Machine learning evaluation and diagnostics =========================================== Skore implements new tools or wraps some key scikit-learn class / functions to automatically provide insights and diagnostics when using them, as a way to facilitate good practices and avoid common pitfalls. .. GENERATED FROM PYTHON SOURCE LINES 36-44 Model evaluation with skore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ In order to assist its users when programming, skore has implemented a :class:`skore.EstimatorReport` class. Let us load some synthetic data and get the estimator report for a :class:`~sklearn.linear_model.LogisticRegression`: .. GENERATED FROM PYTHON SOURCE LINES 46-61 .. code-block:: Python from sklearn.datasets import make_classification from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from skore import EstimatorReport X, y = make_classification(n_classes=2, n_samples=100_000, n_informative=4) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) log_reg = LogisticRegression(random_state=0) log_reg_report = EstimatorReport( log_reg, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test ) .. GENERATED FROM PYTHON SOURCE LINES 62-64 Now, we can display the help tree to see all the insights that are available to us (skore detected that we are doing binary classification): .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python log_reg_report.help() .. rst-class:: sphx-glr-script-out .. code-block:: none ╭────────────────── Tools to diagnose estimator LogisticRegression ───────────────────╮ │ EstimatorReport │ │ ├── .metrics │ │ │ ├── .accuracy(...) (↗︎) - Compute the accuracy score. │ │ │ ├── .brier_score(...) (↘︎) - Compute the Brier score. │ │ │ ├── .log_loss(...) (↘︎) - Compute the log loss. │ │ │ ├── .precision(...) (↗︎) - Compute the precision score. │ │ │ ├── .precision_recall(...) - Plot the precision-recall curve. │ │ │ ├── .recall(...) (↗︎) - Compute the recall score. │ │ │ ├── .roc(...) - Plot the ROC curve. │ │ │ ├── .roc_auc(...) (↗︎) - Compute the ROC AUC score. │ │ │ ├── .custom_metric(...) - Compute a custom metric. │ │ │ └── .report_metrics(...) - Report a set of metrics for our estimator. │ │ ├── .cache_predictions(...) - Cache estimator's predictions. │ │ ├── .clear_cache(...) - Clear the cache. │ │ └── Attributes │ │ ├── .X_test │ │ ├── .X_train │ │ ├── .y_test │ │ ├── .y_train │ │ ├── .estimator_ │ │ └── .estimator_name_ │ │ │ │ │ │ Legend: │ │ (↗︎) higher is better (↘︎) lower is better │ ╰─────────────────────────────────────────────────────────────────────────────────────╯ .. GENERATED FROM PYTHON SOURCE LINES 69-70 We can get the report metrics that was computed for us: .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python df_log_reg_report_metrics = log_reg_report.metrics.report_metrics() df_log_reg_report_metrics .. raw:: html
LogisticRegression
Metric Label / Average
Precision 0 0.875310
1 0.871969
Recall 0 0.872449
1 0.874839
ROC AUC 0.944256
Brier score 0.092121


.. GENERATED FROM PYTHON SOURCE LINES 76-77 We can also plot the ROC curve that was generated for us: .. GENERATED FROM PYTHON SOURCE LINES 79-85 .. code-block:: Python import matplotlib.pyplot as plt roc_plot = log_reg_report.metrics.roc() roc_plot.plot() plt.tight_layout() .. image-sg:: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_001.png :alt: plot skore getting started :srcset: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. seealso:: For more information about the motivation and usage of :class:`skore.EstimatorReport`, see :ref:`example_estimator_report`. .. GENERATED FROM PYTHON SOURCE LINES 93-98 Cross-validation with skore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ skore has also (re-)implemented a :class:`skore.CrossValidationReport` class that contains several :class:`skore.EstimatorReport` for each fold. .. GENERATED FROM PYTHON SOURCE LINES 100-104 .. code-block:: Python from skore import CrossValidationReport cv_report = CrossValidationReport(log_reg, X, y, cv_splitter=5) .. GENERATED FROM PYTHON SOURCE LINES 105-106 We display the cross-validation report helper: .. GENERATED FROM PYTHON SOURCE LINES 108-110 .. code-block:: Python cv_report.help() .. rst-class:: sphx-glr-script-out .. code-block:: none ╭─────────────────── Tools to diagnose estimator LogisticRegression ───────────────────╮ │ CrossValidationReport │ │ ├── .metrics │ │ │ ├── .accuracy(...) (↗︎) - Compute the accuracy score. │ │ │ ├── .brier_score(...) (↘︎) - Compute the Brier score. │ │ │ ├── .log_loss(...) (↘︎) - Compute the log loss. │ │ │ ├── .precision(...) (↗︎) - Compute the precision score. │ │ │ ├── .precision_recall(...) - Plot the precision-recall curve. │ │ │ ├── .recall(...) (↗︎) - Compute the recall score. │ │ │ ├── .roc(...) - Plot the ROC curve. │ │ │ ├── .roc_auc(...) (↗︎) - Compute the ROC AUC score. │ │ │ ├── .custom_metric(...) - Compute a custom metric. │ │ │ └── .report_metrics(...) - Report a set of metrics for our estimator. │ │ ├── .cache_predictions(...) - Cache the predictions for sub-estimators │ │ │ reports. │ │ ├── .clear_cache(...) - Clear the cache. │ │ └── Attributes │ │ ├── .X │ │ ├── .y │ │ ├── .estimator_ │ │ ├── .estimator_name_ │ │ ├── .estimator_reports_ │ │ └── .n_jobs │ │ │ │ │ │ Legend: │ │ (↗︎) higher is better (↘︎) lower is better │ ╰──────────────────────────────────────────────────────────────────────────────────────╯ .. GENERATED FROM PYTHON SOURCE LINES 111-112 We display the metrics for each fold: .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python df_cv_report_metrics = cv_report.metrics.report_metrics() df_cv_report_metrics .. raw:: html
LogisticRegression
Split #0 Split #1 Split #2 Split #3 Split #4
Metric Label / Average
Precision 0 0.872391 0.875463 0.874875 0.877140 0.875163
1 0.876635 0.873640 0.872531 0.867285 0.870365
Recall 0 0.877449 0.873451 0.872251 0.865654 0.869652
1 0.871549 0.875650 0.875150 0.878651 0.875850
ROC AUC 0.946881 0.945183 0.944682 0.945068 0.945238
Brier score 0.090180 0.091689 0.092021 0.092021 0.091441


.. GENERATED FROM PYTHON SOURCE LINES 118-119 We display the ROC curves for each fold: .. GENERATED FROM PYTHON SOURCE LINES 121-125 .. code-block:: Python roc_plot_cv = cv_report.metrics.roc() roc_plot_cv.plot() plt.tight_layout() .. image-sg:: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_002.png :alt: plot skore getting started :srcset: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 126-128 We can retrieve the estimator report of a specific fold to investigate further, for example the first fold: .. GENERATED FROM PYTHON SOURCE LINES 130-134 .. code-block:: Python log_reg_report_fold = cv_report.estimator_reports_[0] df_log_reg_report_fold_metrics = log_reg_report_fold.metrics.report_metrics() df_log_reg_report_fold_metrics .. raw:: html
LogisticRegression
Metric Label / Average
Precision 0 0.872391
1 0.876635
Recall 0 0.877449
1 0.871549
ROC AUC 0.946881
Brier score 0.090180


.. GENERATED FROM PYTHON SOURCE LINES 135-139 .. seealso:: For more information about the motivation and usage of :class:`skore.CrossValidationReport`, see :ref:`example_use_case_employee_salaries`. .. GENERATED FROM PYTHON SOURCE LINES 141-149 Comparing estimators reports ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :class:`skore.ComparisonReport` enables users to compare several estimator reports (corresponding to several estimators) on a same test set, as in a benchmark of estimators. Apart from the previous ``log_reg_report``, let use define another estimator report: .. GENERATED FROM PYTHON SOURCE LINES 151-158 .. code-block:: Python from sklearn.ensemble import RandomForestClassifier rf = RandomForestClassifier(max_depth=2, random_state=0) rf_report = EstimatorReport( rf, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test ) .. GENERATED FROM PYTHON SOURCE LINES 159-161 Now, let us compare these two estimator reports, that were applied to the exact same test set: .. GENERATED FROM PYTHON SOURCE LINES 163-167 .. code-block:: Python from skore import ComparisonReport comparison_report = ComparisonReport(reports=[log_reg_report, rf_report]) .. GENERATED FROM PYTHON SOURCE LINES 168-170 As for the :class:`~skore.EstimatorReport` and the :class:`~skore.CrossValidationReport`, we have a helper: .. GENERATED FROM PYTHON SOURCE LINES 172-174 .. code-block:: Python comparison_report.help() .. rst-class:: sphx-glr-script-out .. code-block:: none ╭──────────────────────────── Tools to compare estimators ─────────────────────────────╮ │ ComparisonReport │ │ ├── .metrics │ │ │ ├── .accuracy(...) (↗︎) - Compute the accuracy score. │ │ │ ├── .brier_score(...) (↘︎) - Compute the Brier score. │ │ │ ├── .log_loss(...) (↘︎) - Compute the log loss. │ │ │ ├── .precision(...) (↗︎) - Compute the precision score. │ │ │ ├── .precision_recall(...) - Plot the precision-recall curve. │ │ │ ├── .recall(...) (↗︎) - Compute the recall score. │ │ │ ├── .roc(...) - Plot the ROC curve. │ │ │ ├── .roc_auc(...) (↗︎) - Compute the ROC AUC score. │ │ │ ├── .custom_metric(...) - Compute a custom metric. │ │ │ └── .report_metrics(...) - Report a set of metrics for the estimators. │ │ ├── .cache_predictions(...) - Cache the predictions for sub-estimators │ │ │ reports. │ │ ├── .clear_cache(...) - Clear the cache. │ │ └── Attributes │ │ ├── .estimator_reports_ │ │ ├── .n_jobs │ │ └── .report_names_ │ │ │ │ │ │ Legend: │ │ (↗︎) higher is better (↘︎) lower is better │ ╰──────────────────────────────────────────────────────────────────────────────────────╯ .. GENERATED FROM PYTHON SOURCE LINES 175-176 Let us display the result of our benchmark: .. GENERATED FROM PYTHON SOURCE LINES 178-181 .. code-block:: Python benchmark_metrics = comparison_report.metrics.report_metrics() benchmark_metrics .. raw:: html
Estimator LogisticRegression RandomForestClassifier
Metric Label / Average
Precision 0 0.875310 0.830577
1 0.871969 0.861436
Recall 0 0.872449 0.868782
1 0.874839 0.821532
ROC AUC 0.944256 0.919813
Brier score 0.092121 0.149600


.. GENERATED FROM PYTHON SOURCE LINES 182-183 We have the result of our benchmark. .. GENERATED FROM PYTHON SOURCE LINES 185-187 We display the ROC curve for the two estimator reports we want to compare, by superimposing them on the same figure: .. GENERATED FROM PYTHON SOURCE LINES 189-193 .. code-block:: Python comparison_report.metrics.roc().plot() plt.tight_layout() .. image-sg:: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_003.png :alt: plot skore getting started :srcset: /auto_examples/getting_started/images/sphx_glr_plot_skore_getting_started_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 194-201 Train-test split with skore ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Skore has implemented a :func:`skore.train_test_split` function that wraps scikit-learn's :func:`sklearn.model_selection.train_test_split`. Let us load a dataset containing some time series data: .. GENERATED FROM PYTHON SOURCE LINES 203-211 .. code-block:: Python import pandas as pd from skrub.datasets import fetch_employee_salaries dataset = fetch_employee_salaries() X, y = dataset.X, dataset.y X["date_first_hired"] = pd.to_datetime(X["date_first_hired"]) X.head(2) .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading 'employee_salaries' from https://github.com/skrub-data/skrub-data-files/raw/refs/heads/main/employee_salaries.zip (attempt 1/3) .. raw:: html
gender department department_name division assignment_category employee_position_title date_first_hired year_first_hired
0 F POL Department of Police MSB Information Mgmt and Tech Division Records... Fulltime-Regular Office Services Coordinator 1986-09-22 1986
1 M POL Department of Police ISB Major Crimes Division Fugitive Section Fulltime-Regular Master Police Officer 1988-09-12 1988


.. GENERATED FROM PYTHON SOURCE LINES 212-214 We can observe that there is a ``date_first_hired`` which is time-based. Now, let us apply :func:`skore.train_test_split` on this data: .. GENERATED FROM PYTHON SOURCE LINES 216-222 .. code-block:: Python import skore X_train, X_test, y_train, y_test = skore.train_test_split( X, y, random_state=0, shuffle=False ) .. rst-class:: sphx-glr-script-out .. code-block:: none ╭─────────────────────────────── TimeBasedColumnWarning ───────────────────────────────╮ │ We detected some time-based columns (column "date_first_hired") in your data. We │ │ recommend using scikit-learn's TimeSeriesSplit instead of train_test_split. │ │ Otherwise you might train on future data to predict the past, or get inflated model │ │ performance evaluation because natural drift will not be taken into account. │ ╰──────────────────────────────────────────────────────────────────────────────────────╯ .. GENERATED FROM PYTHON SOURCE LINES 223-226 We get a ``TimeBasedColumnWarning`` advising us to use :class:`sklearn.model_selection.TimeSeriesSplit` instead! Indeed, we should not shuffle time-ordered data! .. GENERATED FROM PYTHON SOURCE LINES 228-233 .. seealso:: More methodological advice is available. For more information about the motivation and usage of :func:`skore.train_test_split`, see :ref:`example_train_test_split`. .. GENERATED FROM PYTHON SOURCE LINES 235-240 Tracking: skore project ======================= A key feature of skore is its :class:`~skore.Project` that allows to store items of many types. .. GENERATED FROM PYTHON SOURCE LINES 242-244 Setup: creating and loading a skore project ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 246-248 Let's start by creating a skore project directory named ``my_project.skore`` in our current directory: .. GENERATED FROM PYTHON SOURCE LINES 250-254 .. code-block:: Python my_project = skore.Project("my_project") .. GENERATED FROM PYTHON SOURCE LINES 264-269 Skore project: storing and retrieving some items ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Now that the project exists, we can store some useful items in it (in the same directory) using :func:`~skore.Project.put`), with a "universal" key-value convention: .. GENERATED FROM PYTHON SOURCE LINES 271-275 .. code-block:: Python my_project.put("my_int", 3) my_project.put("df_cv_report_metrics", df_cv_report_metrics) my_project.put("roc_plot", roc_plot) .. GENERATED FROM PYTHON SOURCE LINES 276-280 .. note :: With the skore :func:`~skore.Project.put`, there is no need to remember the API for each type of object: ``df.to_csv(...)``, ``plt.savefig(...)``, ``np.save(...)``, etc. .. GENERATED FROM PYTHON SOURCE LINES 282-283 We can retrieve the value of an item: .. GENERATED FROM PYTHON SOURCE LINES 285-287 .. code-block:: Python my_project.get("my_int") .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 288-290 .. code-block:: Python my_project.get("df_cv_report_metrics") .. raw:: html
(LogisticRegression, Split #0) (LogisticRegression, Split #1) (LogisticRegression, Split #2) (LogisticRegression, Split #3) (LogisticRegression, Split #4)
Metric Label / Average
Precision 0 0.872391 0.875463 0.874875 0.877140 0.875163
1 0.876635 0.873640 0.872531 0.867285 0.870365
Recall 0 0.877449 0.873451 0.872251 0.865654 0.869652
1 0.871549 0.875650 0.875150 0.878651 0.875850
ROC AUC 0.946881 0.945183 0.944682 0.945068 0.945238
Brier score 0.090180 0.091689 0.092021 0.092021 0.091441


.. GENERATED FROM PYTHON SOURCE LINES 291-296 .. seealso:: For more information about the functionalities and the different types of items that we can store in a skore :class:`~skore.Project`, see :ref:`example_working_with_projects`. .. GENERATED FROM PYTHON SOURCE LINES 298-300 Tracking the history of items ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 302-303 Suppose we store several values for a same item called ``my_key_metric``: .. GENERATED FROM PYTHON SOURCE LINES 305-311 .. code-block:: Python my_project.put("my_key_metric", 4) my_project.put("my_key_metric", 9) my_project.put("my_key_metric", 16) .. GENERATED FROM PYTHON SOURCE LINES 312-314 Skore does not overwrite items with the same name (key): instead, it stores their history so that nothing is lost: .. GENERATED FROM PYTHON SOURCE LINES 316-319 .. code-block:: Python history = my_project.get("my_key_metric", version="all") history .. rst-class:: sphx-glr-script-out .. code-block:: none [4, 9, 16] .. GENERATED FROM PYTHON SOURCE LINES 323-327 These tracking functionalities are very useful to: * never lose some key machine learning metrics, * and observe the evolution over time / runs. .. GENERATED FROM PYTHON SOURCE LINES 329-333 .. seealso:: For more functionalities about the tracking of items using their history, see :ref:`example_tracking_items`. .. GENERATED FROM PYTHON SOURCE LINES 335-343 .. admonition:: Stay tuned! These are only the initial features: skore is a work in progress and aims to be an end-to-end library for data scientists. Feedbacks are welcome: please feel free to join our `Discord `_ or `create an issue `_. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.853 seconds) .. _sphx_glr_download_auto_examples_getting_started_plot_skore_getting_started.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_getting_started.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_skore_getting_started.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_skore_getting_started.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_