A Tufts University Data Lab Workshop
Written by Uku-Kaspar Uustalu
Slides: tufts.box.com/v/numpy-slides
Live offerings: go.tufts.edu/workshops
Contact: datalab-support@elist.tufts.edu
Table of Contents
Workshop Overview
This hands-on interactive workshop introduces you to scientific computing in Python and demonstrates how to work with matrices and multidimensional arrays using the popular NumPy library. The workshop is intended for those with some previous Python experience and draws comparisons between NumPy and alternatives like MATLAB, C/C++, and Fortran. The workshop notebook is suitable for self-guided study and covers the following:
- Importing Python packages and common aliases
- The NumPy
numpy.ndarray
a.k.a. np.array
data structure
- Different ways of creating, indexing, and slicing NumPy arrays
- Broadcasting and using mathematical operators with
np.array
- Universal functions and aggregation functions
- Iterating and mapping over elements in NumPy arrays
- Row-major vs column-major order and array flattening
- Shape manipulation and the difference between copies and views
- Various options for importing and exporting NumPy arrays
- Tweaking the textual representation of numbers using format strings
- Reading files with Python using
with
and open()
- Quick overview of creating and styling plots with Matplotlib
- Creating a figure with multiple subplots in Matplotlib
The notebook is designed to be run in a pre-configured cloud-computing environment either via Binder or Google Colab and does not require the installation of any software. Use of Binder is recommended as it provides a JupyterLab interface and allows for an experience similar to using a local installation. It is also possible to run the workshop notebook using a local Anaconda or Mambaforge installation. Instructions on how to install Anaconda or Mambaforge are available here: go.tufts.edu/installingPython
Running the Workshop Notebook using a Virtual JupyterLab Instance (Recommended)
- Click on the Launch Binder button above.
- A Binder instance will launch in a new tab with the message Starting Repository.
- Wait patiently and do not close the Binder tab. After a few minutes, a JupyterLab instance will launch.
- If the workshop notebook does not automatically open, double-click on
intro-numpy.ipynb
in the file browser on the left.
Running the Workshop Notebook using Google Colab
- Click on the Open in Colab button above. The Google Colab interface will open in a new tab.
- If you are not already signed into your Google account, click on Sign In in the upper-right and sign in with your Google credentials. You must be signed in with a Google account to be able to run notebooks in Google Colab.
- Optional: To retain any changes you make to this notebook, save a copy of the notebook to your Google Drive by clicking the Copy to Drive button or selecting File > Save a copy in Drive. This will save the notebook to the
Colab Notebooks
directory in your Google Drive. Once the saved copy pops up, close the original notebook and use the copy going forward. Feel free to rename the copy if desired. Any changes made to your personal copy will be automatically saved.
You might also see a message warning you that this notebook was not authored by Google and hence might contain malicious code. You can trust Data Lab notebooks, so click Run Anyway. But when running other third-party notebooks, you should review the code beforehand.
Running the Workshop Notebook using a Local Anaconda Installation
- Click on the Download Notebook button above to download the workshop notebook.
- Create a designated directory for this workshop and move the downloaded
intro-numpy.ipynb
notebook there.
- Launch Anaconda Navigator via the Start Menu (Windows) or from Applications (macOS).
- Windows: Start > Anaconda3 > Anaconda Navigator
- macOS: Applications > Anaconda Navigator
- Launch JupyterLab using the corresponding Launch button.
- JupyterLab will launch in a web browser. (A new tab will be generated if a browser is already open.)
- Within the JupyterLab file explorer on the left, navigate to the directory containing the
intro-numpy.ipynb
notebook.
- Once you are in the correct directory, double-click on
intro-numpy.ipynb
in the file browser to open the workshop notebook.
Running the Workshop Notebook using a Local Mambaforge Installation
- Launch Terminal (macOS/Linux) or Mambaforge Prompt (Windows).
- Windows: Start > Mambaforge > Mambaforge Prompt
- macOS: Applications > Utilities > Terminal
- Run the following commands by typing or pasting the command into the console and then pressing Enter or Return.
- Download and extract the workshop materials:
curl -Lo - https://github.com/tuftsdatalab/intro-numpy/archive/workshop.tar.gz | tar -xzf -
- Navigate into the extracted directory:
cd intro-numpy-workshop
- Create a new environment for the workshop:
mamba env create -f environment.yml
- Activate the workshop environment:
conda activate intro-numpy
- Open the workshop notebook in JupyterLab:
jupyter lab intro-numpy.ipynb
- JupyterLab will launch in a web browser. (A new tab will be generated if a browser is already open.)
- If the workshop notebook does not automatically open, double-click on
intro-numpy.ipynb
in the file browser on the left.
- Do not close the console! Closing the console will also terminate JupyterLab. Leave the console running in the background.