If you're an aspiring data scientist that works with Python or R, you need to know how to use Jupyter Notebook. It's an open-source and server-based IDE for manipulating data, sharing live code, as well as managing the data science workflow.
Let's take a look at how you can install and use Jupyter Notebook on your local machine.
How to Install and Launch Jupyter Notebook With Pip
When you install Jupyter Notebook into a virtual environment via the pip install command, you get to run it as an independent package or module in a virtual space.
To use this method, you must have Python installed on your machine. Otherwise, head to the python.org website to download and install the latest version of Python. However, if you're a Mac or Linux user, you probably have Python already installed by default.
If you're a Windows user, ensure that you add Python to the Windows path after installing it to make it executable from the command line.
Once you've satisfied the above conditions, open up the command prompt to your chosen location.
Next, create and activate a Python virtual environment if you've not done so already.
While in that virtual environment, run the pip install notebook command to install Jupyter Notebook.
Next, run jupyter notebook to launch the Jupyter Notebook server in your default browser.
How to Install and Launch Jupyter Notebook With Anaconda Distribution
Anaconda distribution is an IDE manager that lets you install Jupyter Notebook within a conda virtual environment.
You can also avoid the command line technicalities when you use Anaconda distribution. It lets you create and activate your virtual environment and install Jupyter Notebook via its Anaconda Navigator in a few clicks.
To use this option, you must download and install the latest compatible version of Anaconda distribution.
Once you have Anaconda installed on your machine, launch its Anaconda Navigator. To launch it on Windows, you can open up the Windows search bar and search for Anaconda. Then click on Anaconda Navigator in the search result menu to open it up.
Although the base path environment of Anaconda Navigator has Jupyter Notebook pre-installed into it, you need to install a new one each time you create an environment with it.
You can create a conda environment and install Jupyter Notebook into it via the CMD or Anaconda Navigator.
To use the Anaconda Navigator option, on the sidebar of the app, click on Environments.
Next, look to the lower-left corner of the app and click on Create.
Enter a preferred name for your virtual environment in the Name field. Then select your chosen language and click Create to make a conda virtual environment.
Once you successfully create an environment, select it and go to Home. In the Home menu, locate Jupyter Notebook and click on Install to install it in that environment.
Click on Launch once the installation completes.
Optionally, after installing Jupyter Notebook via the Anaconda Navigator, you can open a terminal or command prompt and use the activate env_name command to activate the conda virtual environment you just created.
Replace env_name with the name of the environment you just created via Anaconda Navigator. However, to use this method on Windows, ensure that you add Anaconda to the Windows path.
Alternatively, if you wish to avoid the Anaconda Navigator option entirely, you can create a conda environment and install Jupyter Notebook into it via the terminal only.
To do that, open your terminal and run the command conda create --name env_name. If you're a Windows user, you can use conda.bat create --name env_name.
Use the pip install notebook command to install Jupyter Notebook into that environment. Replace env_name with your preferred name.
Next, launch Jupyter Notebook by running the jupyter notebook command.
NB: Launching Jupyter Notebook via the command line is recommended. That way, you can create a separate folder for your project and make a virtual environment to the folder's location, as well as launch Jupyter Notebook to that same directory.
How to Use Jupyter Notebook
Now that you've installed Jupyter Notebook. Let's take a look at some of the ways you can use it.
Launching Jupyter Notebook takes you to its homepage, where you get to manage your files.
NB: Typically, when you open Jupyter Notebook to an empty directory via the CMD, the homepage also inherits that empty directory. Otherwise, all the files in the parent directory reflect on Jupyter Notebook's homepage.
How to Import a File Into Jupyter Notebook
You might want to work with an external file like an Excel document. You can import that into the Jupyter Notebook's directory if it's not there already.
To import a file into Jupyter Notebook, look to the upper-right corner of the app and click on Upload to browse your PC for a file.
Next, once you've uploaded the file, click on the Upload option appearing next to the file to add it to the parent folder directory.
Working With a New Notebook
Each notebook you open runs your code on files bearing .ipynb extension. To open a notebook or a Kernel, click on New. Then select Python 3.
To save the newly opened notebook with a new name, click on File.
Next, select the Save as option and give your notebook a preferred name.
How to Shut Down a Running Notebook
To shut down a running notebook, head to the homepage and select Running. It loads a list of currently running kernels.
Click on Shutdown next to any notebook you wish to close to deactivate it.
How to Add New Cells in Jupyter Notebook
While in the Jupyter Notebook Kernel, you get to write your code in cells. To add a new cell, click on Insert. Then select your preferred option.
Alternatively, you can use keyboard shortcuts to add new cells. To add a new cell above an existing one, click on it, then hit key A on your keyboard.
To add a cell below, repeat the process above, but this time, press the key B on your keyboard.
To move a cell up or down, tap the target cell. Next, click the arrow-up or arrow-down at the top of the Kernel.
How to Run a Line or Block of Code in Jupyter Notebook
To execute a line or block of code, click on the Run option at the upper part of the Kernel.
To run a code with the keyboard shortcut, hit Ctrl + Enter on your keyboard.
Loading a Dataset With Pandas in Jupyter Notebook: A Practical Example
You can load a data set into the Kernel as well. Let's take a look at the example code below for reading an Excel file with Python:
import pandas as pd
data=pd.read_excel(r"raw_data.xlsx")
data.head(10)
Note that once you import or paste the dataset into your working directory, you can load the Excel file without calling its full path. Press Ctrl + Enter to run the code above.
The code above returns this output:
How to Use Jupyter Notebook's Markdown Property
You can use the markdown property to write mathematical expressions and plain text. To get started with the markdown property of Jupyter Notebook, you need to master some basic syntax and rules.
Take a look at some of the rules below:
- $: open and close a markdown code
- \limits: denote a limit
- \sum: call the sum symbol
- \alpha: write an alpha symbol
- \beta: write a beta symbol
- \gamma: call a gamma symbol
- ^{}: superscript a character within the curly braze
- _{}: write a subscript of the character within the curly braze
- \cap: introduce a cap symbol
- \hat: put a hat symbol above the next character
To see how those rules work in practice, copy the markdown code below and paste it into a cell in the Kernel.
$\beta_0{^4} \cap \hat 6 \sum \limits \partial 5_{2}$
Next, tap the cell containing the code, then look to the top of the Kernel and click the Code dropdown.
Select Markdown from the list. Then run the markdown code.
Alternatively, you can press the key M on your keyboard to switch the selected cell to markdown. Hit the key Y to switch back to Code mode.
However, to get a better hold of the markdown property, you can check out the Jupyter Notebook Markdown Cells docs.
Make the Best Use of Jupyter Notebook
Jupyter Notebook lets you run and share real-time data science code via a local or remote server. With its markdown property, you can also share mathematical expressions and written texts with other people.
In addition to writing code with it, Jupyter Notebook is a productive and educative platform for tutors and learners to run data science or machine learning project chunks collaboratively.
However, this tool offers a lot of other features. The ones we discussed here should get you started.
Comments
Post a Comment