Deep Dive in Machine Learning with Python

Part — II: Getting familiar with Jupyter Notebook

Rajesh Sharma
Analytics Vidhya
6 min readOct 6, 2019

--

Welcome to the second blog of Deep Dive in Machine Learning with Python, I hope you all have python anaconda distribution installed in your system. In case not, then please go through the previous blog (Deep Dive in ML with Python — Part-I) where we typically covered a high-level overview of Python & ML, and refer to the anaconda installation links.

In this blog, we will cover how to prominently use Jupyter Notebook for coding purposes and execute our first python program. By now, many of you might be wondering:

Q1. What is Jupyter?

In 2014, Fernando Pérez announced a spin-off project from IPython called Project Jupyter.

Project Jupyter’s name is a reference to the three core programming languages supported by Jupyter, which are Julia, Python, and R, and also an homage to Galileo’s notebooks recording the discovery of the moons of Jupiter.

Jupyter is language agnostic and it supports execution environments (means kernels) in several dozen languages among which are Python, Julia, R, Haskell, Ruby, and others.

Q2. What is a Jupyter Notebook?

Jupyter Notebook is a web-based data science tool that provides an interactive computational environment for writing code or creating documents. The term “notebook” is closely related to entities like Jupyter web application and Jupyter Python web server.

A Jupyter Notebook document is a JSON document, and containing an ordered list of input/output cells that can contain code, text (using Markdown), mathematics, plots, and rich media, usually ending with the “.ipynb” extension. It was formerly known as IPython Notebooks.

A Jupyter Notebook can be converted to several pen standard output formats (HTML, presentation slides, LaTeX, PDF, ReStructuredText, Markdown, Python) through “Download As” in the web interface, via the NBCONVERT library.

Other products

The Jupyter Notebook has become a popular user interface and there are some other products as well like:

  • JupyterHub

It is a multi-user server for Jupyter Notebooks

  • JupyterLab

I prefer working on JupyterLab as it offers all the familiar building blocks of the classic Jupyter Notebook (notebook, terminal, text editor, file browser, rich outputs, etc.) in a flexible and powerful user interface

  • Colab

Colaboratory (also known as Colab) is a free Jupyter notebook environment that runs in the cloud and stores its notebooks on Google Drive. Colab only supports the Python 2 and Python 3 kernels and does not support the other Jupyter kernels Julia and R.

How to run Jupyter Notebook?

Step — 1: Run command prompt

  • Type Jupyter notebook
  • Hit enter
Command prompt window

Below window will appear after hitting enter:

Mainly called as Jupyter Server

As soon as Jupyter Server runs, its console will get open in your browser(refer to below image)

Jupyter Console

How to add a directory via Jupyter console?

Step — 1: Click on New from the top right corner and select Folder from the drop-down(look at the below image)

This will add Untitled Folder at your location.

How to rename a newly added directory via Jupyter console?

Step — 1: Select the checkbox appearing on the left side of the Untitled Folder

Step — 2: Then, click on Rename appearing on the top left corner

Step — 3: Provide a new name to your directory and hit enter

Renamed a directory

How to create Python-3 Jupyter Notebook from the console?

Step — 1: Click on New from the top right corner and select Python 3 from the drop-down(refer to below image)

This will create a fresh Jupyter Notebook like below:

New notebook with an empty code cell

How to efficiently code in Jupyter Notebook?

In Jupyter notebook, we use its cells to write either our code or any heading(refer to below image):

How to Convert any cell into a Markdown Cell?

Step — 1: Select the cell and click on the drop-down from the top menu bar

Step — 2: Select Markdown from the drop-down

Creating Markdown Cell

You can use markdown cells for tasks like writing HTML tags, create headings, import images, and others.

How to write a heading in Markdown cell?

Within a markdown cell, you can write headings by starting your text with #, ##, ### or ####.

If your text is enclosed within ** then it appears in Bold. Similarly, if the text is enclosed within _ (aka underscores) then it appears Italic.

Refer to below image:

How to execute a cell?

You can execute a cell by using either ‘Ctrl + Enter’ or ‘Shift + Enter’.

  • ‘Ctrl + Enter’: By using this, control remains on the same cell after its execution
  • ‘Shift + Enter’: By using this, it executes the current cell and control moves on to next cell

How to add or delete a cell?

If you want to add a cell above any cell, then select a cell and press ‘a’ as a keyboard shortcut. Similarly, if you want to a cell below any cell, then press ‘b’ as a keyboard shortcut.

If you want to delete a cell then select a cell and press ‘dd’ as a keyboard shortcut.

NOTE

1. You can use options from menu bar dropdowns for performing these operations. However, I prefer to use keyboard shortcuts.

2. Refer to help for more info on notebook shortcuts.

Now, its time to run our first python program

First python code execution

Yeah, as simple as that!!! Look at the below examples:

By seeing the above image, you would be amazed that without even using a print the python version got displayed as an output result.

This is the unique feature of jupyter notebook as it prints the result of last command executed in a cell.

So, let us have a look at the following examples:

Here, in Example-2, nothing got printed because we just assigned some values(aka 4+5) to a python object(aka addition).

In Example-3, after assigning some values to python objects(aka name and addition) we asked the interpreter to show what object name is holding. Hence, it was the last command executed within the cell so it displayed ‘Steve Smith’ as an output.

This will clear all the doubts, here first we assigned “Steve Smith” to name then we assigned 4+5 i.e. 9 to addition. Then, we tried to see what name and addition are containing, so the addition will be executed last in this cell. Hence, 9 comes as cell output.

So, we come to the end of this blog. In the next blog, we will move ahead with some python hands-on coding.

Happy learning!!!!

Blog-3: Python Essentials

Reference links:

https://en.wikipedia.org/wiki/Project_Jupyter

--

--

Rajesh Sharma
Analytics Vidhya

It can be messy, it can be unstructured but it always speaks, we only need to understand its language!!