Introduction
Experimentation is the day to day job of a Data Scientist. It is conducted in a lab environment which can be on-premise or it can be on Cloud. Many solutions exist which enable us to implement experiments e.g. Cloudera Data Science Workbench, Amazon SageMaker, Google Cloud AI Platform, Anaconda etc. In case, you are keen in experimenting with data on your on-premise then Anaconda distribution is commonly preferred but it does provide Cloud version as well. It is having bundle of data science libraries including a very interesting browser based interface for experimentation i.e. JupyterLab.
Learning Objectives
- What is Anaconda Navigator?
- How to install Anaconda?
- What is the difference between JupyterLab and JupyterNotebooks?
- Jupyter Notebooks in Anaconda
- Shortcuts in Jupyter Notebook
- Various Magic Commands in Jupyter Notebook
What is Anaconda Navigator?
Anaconda Navigator is a desktop graphical user interface (GUI) included in Anaconda® distribution that allows you to launch applications and easily manage conda packages, environments, and channels without using command-line commands. Navigator can search for packages on Anaconda Cloud or in a local Anaconda Repository. It is available for Windows, macOS, and Linux.
How to install Anaconda?
1. Go to the Anaconda Website and choose a Python 3.x graphical installer. Choose Python 3.
2. Locate your download and double click it.
When the screen below appears, click on Next.
3. Read the license agreement and click on I Agree.
4. Click on Next.
5. Note your installation location and then click Next.
6. This is an important part of the installation process. The recommended approach is to not check the box to add Anaconda to your path. This means you will have to use Anaconda Navigator or the Anaconda Command Prompt (located in the Start Menu under “Anaconda”) when you wish to use Anaconda (you can always add Anaconda to your PATH later if you don’t check the box). If you want to be able to use Anaconda in your command prompt (or git bash, cmder, powershell etc), please use the alternative approach and check the box. Then click on Install.
(Recommended Approach)
(Alternative Approach)
7. Click on Next.
8. Again click on Next.
9. Click on Finish.
What is the difference between JupyterLab and JupyterNotebook?
JupyterLab
- JupyterLab is the next generation of the Jupyter Notebook. It aims at fixing many usability issues of the Notebook.
- It offers more of an IDE-like experience. It also brings within the same interface a file browser, a tabbing functionality, consoles, terminals, text editors, Markdown editors, CSV editors, JSON editors, interactive maps, widgets, and so on.
JupyterNotebook
- Web-based interactive computational environment for creating Jupyter notebook documents.
- It is largely used for data analysis, data visualization and further interactive, exploratory computing.
Jupyter Notebooks are a web-based and interactive tool that the machine learning and data science community uses a lot. They are used for quick testing, as a reporting tool or even as highly sophisticated learning materials in online courses.
Open Jupyter Notebook using Anaconda Navigator
First of all open the Anaconda Navigator, then click on the launch button under the Jupyter Notebook tab.
Shortcuts in Jupyter Notebook
Press Ctrl+Shift+p or click on the small keyboard icon from the menu bar to get a list of command palette
Shortcuts in both command and edit modes:
- Shift + Enter run the current cell, select below
- Ctrl + Enter run selected cells
- Alt + Enter run the current cell, insert below
- Ctrl + S save and checkpoint
Shortcuts when in edit mode:
- Esc take you into command mode
- Tab code completion or indent
- Shift + Tab tooltip
- Ctrl + ] indent
- Ctrl + [ dedent
- Ctrl + A select all
- Ctrl + Z undo
- Ctrl + Shift + Z or Ctrl + Y redo
- Ctrl + Home go to cell start
- Ctrl + End go to the cell end
- Ctrl + Left go one word left
- Ctrl + Right go one word right
Once in command mode, press H (help) to get a list of keyboard shortcuts:
Few of the most commonly used shortcuts. Make sure you are in command mode by hitting Esc:
- Scroll up and down your cells with your Up and Down keys.
- Press A or B to insert a new cell above or below the active cell.
- M will transform the active cell to a Markdown cell.
- Y will set the active cell to a code cell.
- X will cut the selected cell
- C will copy the selected cell
- V will paste the cell which is being copied/cut
- Shift + V paste cells above
- S will save the notebook
- F will find/replace
- O will toggle output
- D + D (D twice) will delete the active cell.
- Z will undo cell deletion.
- For selecting multiple cells at once, hold Shift and press Up or Down
- Shift + Space scroll notebook up
- Space scroll notebook down
With multiple cells selected:
- Press Shift + M to merge your selection
- To split the active cell at the cursor, press Ctrl + Shift + – in edit mode
- You can also click and Shift + Click in the margin to the left of your cells to select them
Copying and pasting cells BETWEEN notebooks:
- Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copy
- Notebook 2: — Hit Esc to enter Command mode Ctrl + v to paste
Installing packages in the current kernel using ! pip install <package>
Using Python variables in shell commands by prepending a $ symbol:
Various Magic Commands in Jupyter Notebook
Magic Commands are shortcuts that significantly extend a notebook’s capabilities
Sharing a code sample from your notebook:
- Use the %pastebin magic function to select a range of cells
- Jupyter gives you a secret URL to share
Note- This link gets expired in 7 days
To have a list of defined variables, use %whos or %who_ls
- %whos displays the variable type plus some extra info: size, contents, etc.
- %who_ls only displays the variables name
Working with external files within Jupyter Notebook:
- %pycat file.py – opens script in the pager
- %load file.py – inserts the script into a cell
- %run file.py – runs script
- %run file.ipynb – runs notebook
- %notebook filename – exporting the current IPython history to a notebook file
Getting, setting or listing environment variables:
- %env – lists all environment variables
- %env var – get value for var
- %env var val – set value for var
Running commands in the shell:
%system – to use shell (mostly used to get current directory, date, etc)
Autosaving notebook to its checkpoint using %autosave :
Auto Saving every 120 seconds (2 minutes)
Executing different languages:
- %%HTML – to execute HTML code
- %%perl – to execute Perl in a subprocess
- %%javascript or %%js – to execute the block of Javascript code
- %%python3 – to execute code with python3 in a subprocess
- %%ruby – to execute Ruby code
Other magic commands:
- %history – print input history
- %lsmagic – list currently available magic functions
- %magic – print information about the magic function system
- %matplotlib – setting up matplotlib to work interactively
- %pwd – returns current working directory
- %quickref – shows a quick reference sheet
- %time – time execution of a Python statement or expression (it can be used both as a line and cell magic)
Conclusion
We have learned about Anaconda Navigator, Jupyter Notebook and Various shortcuts & magic commands. Jupyter Notebook is largely used for data analysis, data visualization and further interactive, exploratory computing. Various shortcuts and magic commands make this process much easier.