.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/getting_started/plot_working_with_projects.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_working_with_projects.py: .. _example_working_with_projects: ===================== Working with projects ===================== This example provides an overview of the functionalities and the different types of items that we can store in a skore :class:`~skore.Project`. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Creating and loading the skore project ====================================== .. GENERATED FROM PYTHON SOURCE LINES 17-18 We create and load the skore project from the current directory: .. GENERATED FROM PYTHON SOURCE LINES 20-24 .. code-block:: Python import skore my_project = skore.open("my_project", create=True) .. GENERATED FROM PYTHON SOURCE LINES 25-30 Storing integers ================ Now, let us store our first object using :func:`~skore.Project.put`, for example an integer: .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python my_project.put("my_int", 3) .. GENERATED FROM PYTHON SOURCE LINES 35-38 Here, the name of the object is ``my_int`` and the integer value is 3. We can read it from the project by using :func:`~skore.Project.get`: .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python my_project.get("my_int") .. rst-class:: sphx-glr-script-out .. code-block:: none 3 .. GENERATED FROM PYTHON SOURCE LINES 43-45 Careful; like in a traditional Python dictionary, the ``put`` method will *overwrite* past data if we use a key that already exists! .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python my_project.put("my_int", 30_000) .. GENERATED FROM PYTHON SOURCE LINES 50-51 We can check the updated value: .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python my_project.get("my_int") .. rst-class:: sphx-glr-script-out .. code-block:: none 30000 .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. seealso:: Skore does not exactly *overwrite*, but stores the history of items. For more information about the tracking of items using their history, see :ref:`example_tracking_items`. .. GENERATED FROM PYTHON SOURCE LINES 63-64 By using the :func:`~skore.Project.delete` method, we can also delete an object: .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python my_project.put("my_int_2", 10) .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python my_project.delete("my_int_2") .. GENERATED FROM PYTHON SOURCE LINES 72-73 We can display all the keys in our project: .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python my_project.keys() .. rst-class:: sphx-glr-script-out .. code-block:: none ['my_int'] .. GENERATED FROM PYTHON SOURCE LINES 78-80 Storing strings and texts ========================= .. GENERATED FROM PYTHON SOURCE LINES 82-83 We just stored a integer, now let us store some text using strings! .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: Python my_project.put("my_string", "Hello world!") .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python my_project.get("my_string") .. rst-class:: sphx-glr-script-out .. code-block:: none 'Hello world!' .. GENERATED FROM PYTHON SOURCE LINES 91-94 :func:`~skore.Project.get` infers the type of the inserted object by default. For example, strings are assumed to be in Markdown format. Hence, we can customize the display of our text: .. GENERATED FROM PYTHON SOURCE LINES 96-109 .. code-block:: Python my_project.put( "my_string_2", ( """Hello world!, **bold**, *italic*, `code` ```python def my_func(x): return x+2 ``` """ ), ) .. GENERATED FROM PYTHON SOURCE LINES 110-112 Moreover, we can also explicitly tell skore the way we want to display an object, for example in HTML: .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. code-block:: Python my_project.put( "my_string_3", "

Title

bold, italic, etc.

", display_as="HTML", ) .. GENERATED FROM PYTHON SOURCE LINES 122-123 Note that the `display_as` is only used for the UI, and not in this notebook at hand: .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python my_project.get("my_string_3") .. rst-class:: sphx-glr-script-out .. code-block:: none '

Title

bold, italic, etc.

' .. GENERATED FROM PYTHON SOURCE LINES 128-129 We can also conveniently use a Python f-string: .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python x = 2 y = [1, 2, 3, 4] my_project.put("my_string_4", f"The value of `x` is {x} and the value of `y` is {y}.") .. GENERATED FROM PYTHON SOURCE LINES 136-138 Storing many kinds of data ========================== .. GENERATED FROM PYTHON SOURCE LINES 140-141 Python list: .. GENERATED FROM PYTHON SOURCE LINES 143-147 .. code-block:: Python my_list = [1, 2, 3, 4] my_project.put("my_list", my_list) my_list .. rst-class:: sphx-glr-script-out .. code-block:: none [1, 2, 3, 4] .. GENERATED FROM PYTHON SOURCE LINES 148-149 Python dictionary: .. GENERATED FROM PYTHON SOURCE LINES 151-158 .. code-block:: Python my_dict = { "company": "probabl", "year": 2023, } my_project.put("my_dict", my_dict) my_dict .. rst-class:: sphx-glr-script-out .. code-block:: none {'company': 'probabl', 'year': 2023} .. GENERATED FROM PYTHON SOURCE LINES 159-160 Numpy array: .. GENERATED FROM PYTHON SOURCE LINES 162-168 .. code-block:: Python import numpy as np my_arr = np.random.randn(3, 3) my_project.put("my_arr", my_arr) my_arr .. rst-class:: sphx-glr-script-out .. code-block:: none array([[-0.70465328, 1.06035438, -2.09164595], [ 0.52130042, -2.09041285, -0.33714385], [-0.8565072 , -0.58992501, 0.45809152]]) .. GENERATED FROM PYTHON SOURCE LINES 169-170 Pandas data frame: .. GENERATED FROM PYTHON SOURCE LINES 172-178 .. code-block:: Python import pandas as pd my_df_pandas = pd.DataFrame(np.random.randn(10, 5)) my_project.put("my_df_pandas", my_df_pandas) my_df_pandas.head() .. raw:: html
0 1 2 3 4
0 -0.440443 0.329873 0.363951 1.502414 -1.210765
1 0.662174 -0.404660 1.436439 0.371513 -0.477336
2 0.837074 -0.423741 0.264147 0.787013 1.464432
3 0.752040 0.999106 -0.602795 -1.694948 -0.139065
4 2.605264 -1.306096 1.983309 0.910859 1.026853


.. GENERATED FROM PYTHON SOURCE LINES 179-180 Polars data frame: .. GENERATED FROM PYTHON SOURCE LINES 182-188 .. code-block:: Python import polars as pl my_df_polars = pl.DataFrame(np.random.randn(10, 5)) my_project.put("my_df_polars", my_df_polars) my_df_polars.head() .. raw:: html
shape: (5, 5)
column_0column_1column_2column_3column_4
f64f64f64f64f64
-0.085119-1.0538990.278963-0.7448090.244702
0.3571280.339314-0.2278540.188571-0.386617
0.046801-1.0185232.041355-0.7127340.399751
0.2265450.3058810.535751.702937-0.119411
-0.225015-0.752811-0.1649891.369761.558969


.. GENERATED FROM PYTHON SOURCE LINES 189-190 Skrub :class:`~skrub.TableReport`: .. GENERATED FROM PYTHON SOURCE LINES 192-201 .. code-block:: Python from skrub.datasets import fetch_employee_salaries from skrub import TableReport dataset = fetch_employee_salaries() employees_df, salaries = dataset.X, dataset.y my_tablereport = TableReport(employees_df) my_project.put("my_tablereport", my_tablereport) my_tablereport .. rst-class:: sphx-glr-script-out .. code-block:: none Processing column 1 / 8 Processing column 2 / 8 Processing column 3 / 8 Processing column 4 / 8 Processing column 5 / 8 Processing column 6 / 8 Processing column 7 / 8 Processing column 8 / 8 .. raw:: html

Please enable javascript

The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").



.. GENERATED FROM PYTHON SOURCE LINES 202-207 Storing data visualizations =========================== Note that, in the dashboard, the interactivity of plots is supported, for example for Altair and Plotly. .. GENERATED FROM PYTHON SOURCE LINES 209-210 Matplotlib figure: .. GENERATED FROM PYTHON SOURCE LINES 212-228 .. code-block:: Python import matplotlib.pyplot as plt x = np.linspace(0, 2, 100) fig, ax = plt.subplots(layout="constrained") ax.plot(x, x, label="linear") ax.plot(x, x**2, label="quadratic") ax.plot(x, x**3, label="cubic") ax.set_xlabel("x label") ax.set_ylabel("y label") ax.set_title("Simple Plot") ax.legend() plt.show() my_project.put("my_figure", fig) .. image-sg:: /auto_examples/getting_started/images/sphx_glr_plot_working_with_projects_001.png :alt: Simple Plot :srcset: /auto_examples/getting_started/images/sphx_glr_plot_working_with_projects_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 229-230 Altair chart: .. GENERATED FROM PYTHON SOURCE LINES 233-252 .. code-block:: Python import altair as alt alt.renderers.enable("default") num_points = 100 df_plot = pd.DataFrame( {"x": np.random.randn(num_points), "y": np.random.randn(num_points)} ) my_altair_chart = ( alt.Chart(df_plot) .mark_circle() .encode(x="x", y="y", tooltip=["x", "y"]) .interactive() .properties(title="My title") ) my_project.put("my_altair_chart", my_altair_chart) .. GENERATED FROM PYTHON SOURCE LINES 253-263 .. note:: For Plotly figures, some users reported the following error when running Plotly cells: ``ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed``. This is a Plotly issue which is documented `here `_; to solve it, we recommend installing ``nbformat`` in your environment, e.g. with: .. code-block:: console pip install --upgrade nbformat .. GENERATED FROM PYTHON SOURCE LINES 265-266 Plotly figure: .. GENERATED FROM PYTHON SOURCE LINES 268-279 .. code-block:: Python import plotly.express as px df = px.data.iris() fig = px.scatter( df, x=df.sepal_length, y=df.sepal_width, color=df.species, size=df.petal_length ) my_project.put("my_plotly_fig", fig) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 280-281 Animated Plotly figure: .. GENERATED FROM PYTHON SOURCE LINES 283-303 .. code-block:: Python df = px.data.gapminder() my_anim_plotly_fig = px.scatter( df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", size="pop", color="continent", hover_name="country", log_x=True, size_max=55, range_x=[100, 100_000], range_y=[25, 90], ) my_project.put("my_anim_plotly_fig", my_anim_plotly_fig) my_anim_plotly_fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 304-305 PIL image: .. GENERATED FROM PYTHON SOURCE LINES 307-317 .. code-block:: Python import io import PIL my_pil_image = PIL.Image.new("RGB", (100, 100), color="red") with io.BytesIO() as output: my_pil_image.save(output, format="png") my_project.put("my_pil_image", my_pil_image) .. GENERATED FROM PYTHON SOURCE LINES 318-322 Storing scikit-learn models and pipelines ========================================= First of all, we can store a scikit-learn model: .. GENERATED FROM PYTHON SOURCE LINES 324-330 .. code-block:: Python from sklearn.linear_model import Lasso my_model = Lasso(alpha=2) my_project.put("my_model", my_model) my_model .. raw:: html
Lasso(alpha=2)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 331-332 We can also store scikit-learn pipelines: .. GENERATED FROM PYTHON SOURCE LINES 334-343 .. code-block:: Python from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler my_pipeline = Pipeline( [("standard_scaler", StandardScaler()), ("lasso", Lasso(alpha=2))] ) my_project.put("my_pipeline", my_pipeline) my_pipeline .. raw:: html
Pipeline(steps=[('standard_scaler', StandardScaler()),
                    ('lasso', Lasso(alpha=2))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 344-345 Moreover, we can store fitted scikit-learn pipelines: .. GENERATED FROM PYTHON SOURCE LINES 347-357 .. code-block:: Python from sklearn.datasets import load_diabetes diabetes = load_diabetes() X = diabetes.data[:150] y = diabetes.target[:150] my_pipeline.fit(X, y) my_project.put("my_fitted_pipeline", my_pipeline) my_pipeline .. raw:: html
Pipeline(steps=[('standard_scaler', StandardScaler()),
                    ('lasso', Lasso(alpha=2))])
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.


.. GENERATED FROM PYTHON SOURCE LINES 358-363 Cleanup the project ------------------- Let's clear the skore project (to avoid any conflict with other documentation examples). .. GENERATED FROM PYTHON SOURCE LINES 365-366 .. code-block:: Python my_project.clear() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.313 seconds) .. _sphx_glr_download_auto_examples_getting_started_plot_working_with_projects.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_working_with_projects.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_working_with_projects.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_working_with_projects.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_