The right way to Use Exploratory Notebooks [Best Practices]

Jupyter notebooks have been some of the controversial instruments within the information science group. There are some outspoken critics, in addition to passionate fans. However, many information scientists will agree that they are often actually worthwhile – if used properly. And that’s what we’re going to deal with on this article, which is the second in my sequence on Software Patterns for Data Science & ML Engineering. I’ll present you finest practices for utilizing Jupyter Notebooks for exploratory information evaluation.

However first, we have to perceive why notebooks had been established within the scientific group. When data science was sexy, notebooks weren’t a factor but. Earlier than them, we had IPython, which was built-in into IDEs similar to Spyder that attempted to imitate the way in which RStudio or Matlab labored. These instruments gained vital adoption amongst researchers.

In 2014, Venture Jupyter developed from IPython. Its utilization sky-rocketed, pushed primarily by researchers who jumped to work in trade. Nevertheless, approaches for utilizing notebooks that work properly for scientific tasks don’t essentially translate properly to analyses performed for the enterprise and product items of enterprises. It’s not unusual for information scientists employed proper out of college to battle to satisfy the brand new expectations they encounter across the construction and presentation of their analyses.

On this article, we’ll speak about Jupyter notebooks particularly from a enterprise and product standpoint. As I already talked about, Jupyter notebooks are a polarising matter, so let’s go straight into my opinion.

Jupyter notebooks ought to be used for purely exploratory duties or ad-hoc evaluation ONLY.

A pocket book ought to be nothing greater than a report. The code it incorporates shouldn’t be vital in any respect. It’s solely the outcomes it generates that matter. Ideally, we should always be capable to disguise the code within the pocket book as a result of it’s only a means to reply questions.

For instance: What are the statistical traits of those tables? What are the properties of this coaching dataset? What’s the impression of placing this mannequin into manufacturing? How can we be sure that this mannequin outperforms the earlier one? How has this AB check carried out?

Jupyter notebooks are useful in different domains and for different purposes
Jupyter notebooks are helpful in numerous domains and for various functions | Supply: Writer

Jupyter pocket book: pointers for efficient storytelling

Writing Jupyter notebooks is principally a means of telling a narrative or answering a query about an issue you’ve been investigating. However that doesn’t imply you must present the express work you’ve finished to succeed in your conclusion.

Notebooks should be refined.

They’re primarily created for the author to know a problem but additionally for his or her fellow friends to achieve that information with out having to dive deep into the issue themselves.


The non-linear and tree-like nature of exploring datasets in notebooks, which usually include irrelevant sections of exploration streams that didn’t result in any reply, isn’t the way in which the pocket book ought to have a look at the tip. The pocket book ought to include the minimal content material that finest solutions the questions at hand. You must at all times touch upon and provides rationales about every of the assumptions and conclusions. Government summaries are at all times advisable as they’re excellent for stakeholders with a obscure curiosity within the matter or restricted time. They’re additionally an effective way to arrange peer reviewers for the total pocket book delve.


The viewers for notebooks is usually fairly technical or business-savvy. Therefore, you’re anticipated to make use of superior terminology. However, government summaries or conclusions ought to at all times be written in easy language and hyperlink to sections with additional and deeper explanations. If you end up struggling to craft a pocket book for a non-technical viewers, perhaps you need to take into account making a slide deck as a substitute. There, you should use infographics, customized visualizations, and broader methods to clarify your concepts.

The different stakeholders of a data scientist all have different demands
The completely different stakeholders of an information scientist all have completely different calls for | Supply: Writer


All the time present context for the issue at hand. Knowledge by itself isn’t ample for a cohesive story. Now we have to border the entire evaluation inside the area we’re working in in order that the viewers feels comfy studying it. Use hyperlinks to the corporate’s present information base to help your statements and acquire all of the references in a devoted part of the pocket book.

The right way to construction Jupyter pocket book’s content material

On this part, I’ll clarify the pocket book structure I usually use. It could look like numerous work, however I like to recommend making a pocket book template with the next sections, leaving placeholders for the specifics of your job. Such a personalized template will prevent numerous time and guarantee consistency throughout notebooks.

  1. Title: Ideally, the title of the related JIRA job (or every other issue-tracking software program) linked to the duty. This permits you and your viewers to unambiguously join the reply (the pocket book) to the query (the JIRA job).
  2. Description: What do you need to obtain on this job? This ought to be very transient.
  3. Desk of contents: The entries ought to hyperlink to the pocket book sections, permitting the reader to leap to the half they’re interested by. (Jupyter creates HTML anchors for every headline which might be derived from the unique headline by headline.decrease().exchange(” “, “-“), so you possibly can hyperlink to them with plain Markdown hyperlinks similar to [section title](#section-title). You can too place your personal anchors by including <a id=’your-anchor’></a> to markdown cells.)
  4. References: Hyperlinks to inner or exterior documentation with background data or particular data used inside the evaluation offered within the pocket book.
  5. TL;DR or government abstract: Clarify, very concisely, the outcomes of the entire exploration and spotlight the important thing conclusions (or questions) that you just’ve provide you with.
  6. Introduction & background: Put the duty into context, add details about the important thing enterprise precedents across the subject, and clarify the duty in additional element.
  7. Imports: Library imports and settings. Configure settings for third-party libraries, similar to matplotlib or seaborn. Add setting variables similar to dates to repair the exploration window.
  8. Knowledge to discover: Define the tables or datasets you’re exploring/analyzing and reference their sources or hyperlink their information catalog entries. Ideally, you floor how every dataset or desk is created and the way regularly it’s up to date. You can hyperlink this part to every other piece of documentation.
  9. Evaluation cells
  10. Conclusion: Detailed clarification of the important thing outcomes you’ve obtained within the Evaluation part, with hyperlinks to particular components of the notebooks the place readers can discover additional explanations.

Bear in mind to at all times use Markdown formatting for headers and to focus on vital statements and quotes. You’ll be able to test the completely different Markdown syntax choices in Markdown Cells — Jupyter Notebook 6.5.2 documentation.

Example template for an exploratory notebook
Instance template for an exploratory pocket book | Supply: Writer

The right way to manage code in Jupyter pocket book

For exploratory duties, the code to provide SQL queries, pandas information wrangling, or create plots isn’t vital for readers.

Nevertheless, it can be crucial for reviewers, so we should always nonetheless keep a top quality and readability.

My ideas for working with code in notebooks are the next:

Transfer auxiliary capabilities to plain Python modules

Typically, importing capabilities outlined in Python modules is best than defining them within the pocket book. For one, Git diffs inside .py information are means simpler to learn than diffs in notebooks. The reader must also not have to know what a perform is doing beneath the hood to observe the pocket book.

For instance, you usually have capabilities to learn your information, run SQL queries, and preprocess, remodel, or enrich your dataset. All of them ought to be moved into .py filed after which imported into the pocket book in order that readers solely see the perform name. If a reviewer desires extra element, they’ll at all times have a look at the Python module immediately.

I discover this particularly helpful for plotting capabilities, for instance. It’s typical that I can reuse the identical perform to make a barplot a number of instances in my pocket book. I’ll have to make small modifications, similar to utilizing a unique set of knowledge or a unique title, however the general plot structure and magnificence would be the identical. As an alternative of copying and pasting the identical code snippet round, I simply create a utils/ module and create capabilities that may be imported and tailored by offering arguments.

Right here’s a quite simple instance:

import matplotlib.pyplot as plt
import numpy as np
def create_barplot(information, x_labels, title='', xlabel='', ylabel='', bar_color='b', bar_width=0.8, type='seaborn', figsize=(8, 6)):
    """Create a customizable barplot utilizing Matplotlib.
    - information: Listing or array of knowledge to be plotted.
    - x_labels: Listing of labels for the x-axis.
    - title: Title of the plot.
    - xlabel: Label for the x-axis.
    - ylabel: Label for the y-axis.
    - bar_color: Colour of the bars (default is blue).
    - bar_width: Width of the bars (default is 0.8).
    - type: Matplotlib type to use (e.g., 'seaborn', 'ggplot', 'default').
    - figsize: Tuple specifying the determine dimension (width, peak).
    - None
    fig, ax = plt.subplots(figsize=figsize)
    x = np.arange(len(information))
 , information, colour=bar_color, width=bar_width)

    title=”Customizable Bar Plot”,

When creating these Python modules, keep in mind that the code remains to be a part of an exploratory evaluation. So until you’re utilizing it in every other a part of the venture, it doesn’t have to be excellent. Simply readable and comprehensible sufficient to your reviewers.

Placing functions for plotting, data loading, data preparation, and implementations of evaluation metrics in plain Python modules keeps a Jupyter notebook focused on the exploratory analysis
Putting capabilities for plotting, information loading, information preparation, and implementations of analysis metrics in plain Python modules retains a Jupyter pocket book centered on the exploratory evaluation | Supply: Writer

Utilizing SQL immediately in Jupyter cells

There are some instances during which information isn’t in reminiscence (e.g., in a pandas DataFrame) however within the firm’s information warehouse (e.g., Redshift). In these instances, many of the information exploration and wrangling might be finished by SQL.

There are a number of methods to make use of SQl wit Jupyter notebooks. JupySQL permits you to write SQL code immediately in pocket book cells and exhibits the question consequence as if it was a pandas DataFrame. You can too retailer SQL scripts in accompanying information or inside the auxiliary Python modules we mentioned within the earlier part.

Whether or not it’s higher to make use of one or the opposite relies upon principally in your aim:

If you happen to’re operating an information exploration round a number of tables from an information warehouse and also you need to present to your friends the standard and validity of the information, then displaying SQL queries inside the pocket book is normally the best choice. Your reviewers will recognize that they’ll immediately see the way you’ve queried these tables, what sort of joins you needed to make to reach at sure views, what filters you wanted to use, and so forth.

Nevertheless, for those who’re simply producing a dataset to validate a machine studying mannequin and the principle focus of the pocket book is to indicate completely different metrics and explainability outputs, then I’d suggest to cover the dataset extraction as a lot as potential and maintain the queries in a separate SQL script or Python module.

We’ll now see an instance of the right way to use each choices.

Studying & executing from .sql scripts

We will use .sql information which might be opened and executed from the pocket book by a database connector library.

Let’s say we have now the next question in a select_purchases.sql file:

SELECT * FROM public.ecommerce_purchases WHERE product_id = 123

Then, we may outline a perform to execute SQL scripts:

import psycopg2
def execute_sql_script(filename, connection_params):
    Execute a SQL script from a file utilizing psycopg2.
    - filename: The title of the SQL script file to execute.
    - connection_params: A dictionary containing PostgreSQL connection parameters,
                        similar to 'host', 'port', 'database', 'person', and 'password'.
    - None
    host = connection_params.get('host', 'localhost')
    port = connection_params.get('port', '5432')
    database = connection_params.get('database', '')
    person = connection_params.get('person', '')
    password = connection_params.get('password', '')
        conn = psycopg2.join(
        cursor = conn.cursor()
        with open(filename, 'r') as sql_file:
            sql_script = sql_file.learn()
        consequence = cursor.fetchall()
        column_names = [desc[0] for desc in cursor.description]
        df = pd.DataFrame(consequence, columns=column_names)

        return df

    besides Exception as e:
        print(f"Error: {e}")
        if 'conn' in locals():

Observe that we have now supplied default values for the database connection parameters in order that we don’t should specify them each time. Nevertheless, keep in mind to by no means retailer secrets and techniques or different delicate data inside your Python scripts! (Later within the sequence, we’ll talk about completely different options to this downside.)

Now we will use the next one-liner inside our pocket book to execute the script:

df = execute_sql_script('select_purchases.sql', connection_params)

Utilizing JupySQL

Historically, ipython-sql has been the device of alternative to question SQL from Jupyter notebooks. But it surely has been sundown by its unique creator in April 2023, who recommends switching to JupySQL, which is an actively maintained fork. Going ahead, all enhancements and new options will solely be added to JupySQL.

To put in the library for utilizing it with Redshift, we have now to do:

pip set up jupysql sqlalchemy-redshift redshift-connector 'sqlalchemy<2'

(You can too use it together with different databases similar to snowflake or duckdb,)

In your Jupyter pocket book now you can use the %load_ext sql magic command to allow SQL and use the next snippet to create a sqlalchemy Redshift engine:

from os import environ
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
person = environ["REDSHIFT_USERNAME"]
password = environ["REDSHIFT_PASSWORD"]
host = environ["REDSHIFT_HOST"]
url = URL.create(
engine = create_engine(url)

Then, simply move the engine to the magic command:

%sql engine --alias redshift-sqlalchemy

And also you’re able to go!

Now it’s simply so simple as utilizing the magic command and write any question that you just need to execute and you’ll get the ends in the cell’s output:

SELECT * FROM public.ecommerce_purchases WHERE product_id = 123

Be sure cells are executed so as

I like to recommend you at all times run all code cells earlier than pushing the pocket book to your repository. Jupyter notebooks save the output state of every cell when it’s executed. That implies that the code you wrote or edited may not correspond to the proven output of the cell.

Working a pocket book from prime to backside can be a very good check to see in case your pocket book relies on any person enter to execute accurately. Ideally, every little thing ought to simply run by with out your intervention. If not, your evaluation is most probably not reproducible by others – and even by your future self.

A method of checking {that a} pocket book has been run in-order is to make use of the nbcheckorder pre-commit hook. It checks if the cell’s output numbers are sequential. In the event that they’re not, it signifies that the pocket book cells haven’t been executed one after the opposite and prevents the Git commit from going by.

Pattern .pre-commit-config.yaml:

- repo: native
   rev: v0.2.0
     - id: nbcheckorder

If you happen to’re not utilizing pre-commit but, I extremely suggest you undertake this little device. I like to recommend you to start out studying about it by this introduction to pre-commit by Elliot Jordan. Later, you possibly can undergo its extensive documentation to know all of its options.

Filter out cells’ output

Even higher than the tip earlier than, filter out all cells’ output within the pocket book. One profit you get is that you could ignore the cells states and outputs, however alternatively, it forces reviewers to run the code in native in the event that they need to see the outcomes. There are a number of methods to do that mechanically.

You should utilize the nbstripout along with pre-commit as defined by Florian Rathgeber, the device’s creator, on GitHub:

- repo: native
  rev: 0.6.1
    - id: nbstripout

You can too use nbconvert –ClearOutputpPreprocessor in a customized pre-commit hook as defined by Yury Zhauniarovich:

  - repo: native
      - id: jupyter-nb-clear-output
        title: jupyter-nb-clear-output
        information: .ipynb$
        levels: [ commit ]
        language: python
        entry: jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace
        additional_dependencies: [ 'nbconvert' ]

Produce and share studies with Jupyter pocket book

Now, right here comes a not very well-solved question within the trade. What’s one of the simplest ways to share your notebooks together with your workforce and exterior stakeholders?

By way of sharing analyses from Jupyter notebooks, the sphere is split between three various kinds of groups that foster alternative ways of working.

The translator groups

These groups consider that individuals from enterprise or product items gained’t be comfy studying Jupyter notebooks. Therefore, they adapt their evaluation and studies to their anticipated viewers.

Translator groups take their findings from the notebooks and add them to their firm’s information system (e.g., Confluence, Google Slides, and so forth.). As a unfavorable facet impact, they lose a number of the traceability of notebooks, as a result of it’s now tougher to evaluation the report’s model historical past. However, they’ll argue, they’re able to convey their outcomes and evaluation extra successfully to the respective stakeholders.

If you wish to do that, I like to recommend preserving a hyperlink between the exported doc and the Jupyter pocket book in order that they’re at all times in sync. On this setup, you possibly can maintain notebooks with much less textual content and conclusions, centered extra on the uncooked info or information proof. You’ll use the documentation system to broaden on the chief abstract and feedback about every of the findings. On this means, you possibly can decouple each deliverables – the exploratory code and the ensuing findings.

The all in-house groups

These groups use native Jupyter notebooks and share them with different enterprise items by constructing options tailor-made to their firm’s information system and infrastructure. They do consider that enterprise and product stakeholders ought to be capable to perceive the information scientist’s notebooks and really feel strongly about the necessity to maintain a totally traceable lineage from findings again to the uncooked information.

Nevertheless, it’s unlikely the finance workforce goes to GitHub or Bitbucket to learn your pocket book.

I’ve seen a number of options applied on this house. For instance, you should use instruments like nbconvert to generate PDFs from Jupyter notebooks or export them as HTML pages, in order that they are often simply shared with anybody, even exterior the technical groups.

You’ll be able to even transfer these notebooks into S3 and permit them to be hosted as a static website with the rendered view. You can use a CI/CD workflow to create and push an HTML rendering of your pocket book to S3 when the code will get merged into a selected department.

The third-party device advocates

These groups use instruments that allow not simply the event of notebooks but additionally the sharing with different folks within the organisation. This usually entails coping with complexities similar to guaranteeing safe and easy entry to inner information warehouses, information lakes, and databases.

A number of the most generally adopted instruments on this house are Deepnote, Amazon SageMaker, Google Vertex AI, and Azure Machine Learning. These are all full-fledged platforms for operating notebooks that permit spinning-up digital environments in distant machines to execute your code. They supply interactive plotting, information, and experiments exploration, which simplifies the entire information science lifecycle. For instance, Sagemaker permits you to visualise all of your experiments data that you’ve got tracked with Sagemaker Experiments, and Deepnote affords additionally level and click on visualization with their Chart Blocks.

On prime of that, Deepnote and SageMaker will let you share the pocket book with any of your friends to view it and even to allow real-time collaboration utilizing the identical execution setting.

There are additionally open-source options similar to JupyterHub, however the setup effort and upkeep that you might want to function it isn’t price it. Spinning up a JupyterHub on-premises could be a suboptimal answer, and solely in only a few instances does it make sense to do it (e.g: very specialised varieties of workloads which require particular {hardware}). Through the use of Cloud companies, you possibly can leverage economies of scale which assure a lot better fault-tolerant architectures than different corporations which function in a unique enterprise can supply. You must assume the preliminary setup prices, delegate its upkeep to a platform operations workforce to stick with it and operating for Knowledge Scientists, and assure information safety and privateness. Due to this fact, belief in managed companies will keep away from infinite complications concerning the infrastructure that’s higher not having.

My basic recommendation for exploring these merchandise: If your organization is already utilizing a cloud supplier like AWS, Google Cloud Platform, or Azure it could be a good suggestion to undertake their pocket book answer, as accessing your organization’s infrastructure will probably be simpler and appear much less dangerous. interactive dashboards assist ML groups to collaborate and share experiment outcomes with stakeholders throughout the corporate.

Right here’s an instance of how Neptune helped the ML workforce at Respo.Imaginative and prescient secure time by sharing ends in a standard setting.

I just like the dashboards as a result of we’d like a number of metrics, so that you code the dashboard as soon as, have these kinds, and simply see them on one display screen. Then, every other individual can view the identical factor, in order that’s fairly good.

Łukasz Grad, Chief Knowledge Scientist at ReSpo.Imaginative and prescient

Embracing efficient Jupyter pocket book practices

On this article, we’ve mentioned finest practices and recommendation for optimizing the utility of Jupyter notebooks.

A very powerful takeaway:

All the time strategy making a pocket book with the supposed viewers and last goal in thoughts. In that means, you know the way a lot focus to placed on the completely different dimensions of the pocket book (code, evaluation, government abstract, and so forth).

All in all, I encourage information scientists to make use of Jupyter notebooks, however completely for answering exploratory questions and reporting functions.

Manufacturing artefacts similar to fashions, datasets, or hyperparameters shouldn’t hint again to notebooks. They need to have their origin in manufacturing methods which might be reproducible and re-runnable. For instance, SageMaker Pipelines or Airflow DAGs which might be well-maintained and completely examined.

These final ideas about traceability, reproducibility, and lineage would be the start line for the subsequent article in my sequence on Software Patterns in Data Science and ML Engineering, which can deal with the right way to uplevel your ETL abilities. Whereas typically ignored by information scientists, I consider mastering ETL is core and significant to ensure the success of any machine studying venture.

Was the article helpful?

Thanks to your suggestions!

Discover extra content material subjects:

Leave a Reply

Your email address will not be published. Required fields are marked *