PX1224: Computational Skills for Problem Solving

Python and Jupyter Notebook

Python is a complete programming language that contains the functionality required by programmers to write all kinds of code, from making movies to predicting the weather. Python is a relatively modern language. It is very powerful and contains a library of functions capable of many diverse tasks. However, not everything that we will find useful is incorporated into Python. For example, there is no straightforward way to make plots using the basic Python language.

Python Packages

Thankfully, there are a large number of additional packages that are specifically written for scientific computing. These packages are libraries of extra commands which add more functionality than plain python. The ones that we will make most use of are:

  • NumPy -- Package for scientific computing with Python. It contains array functionality, linear algebra, Fourier transform, and random number capabilities. To find details on a function go to Documentation, then open the NumPy Reference Guide (or the new User Guide) and use the Quick Search bar on the left, or look in the index (top right).
  • Matplotlib -- Python 2D plotting library. This provides a straightforward way to make and save plots, histograms, scatter plots and log plots. The website is not particularly useful for details on a specific function as it gives you exactly the same information as comes up in the object explorer (see below) when you type the function in. If you want to find out how to use a function, is it is best to use Ctrl+F to search this page for the function you want or look in the examples section.
  • SciPy -- Open-source Python software for mathematics, science, and engineering. This contains a large number of packages which can perform some fairly complex analysis. We will only make use of a small part of the available material in SciPy in this course. To find details on a function go to Documentation, then open the SciPy Reference Guide and use the Quick Search bar on the left, or look in the index (top right).

There are other packages that you may meet in later years, but NumPy, SciPy and Matplotlib are the ones that you will use the most. When we begin programming, we will ensure that both NumPy and Matplotlib are available for you to use. Indeed, most of the functions you use in the courses are actually from NumPy or Matplotlib (rather than Python's standard library).

Jupyter Notebook

The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

The Jupyter Notebook has become a popular user interface for cloud computing, and major cloud providers have adopted the Jupyter Notebook or derivative tools as a frontend interface for cloud users. Examples include Amazon's SageMaker Notebooks, Google's Colaboratory and Microsoft's Azure Notebook.

Finally, Jupiter Notebook, with its ability to combine coding along with document formatting, mathematical equations and dynamically produced graphs, is ideal for scientific analysis and reporting. For this reason, Jupyter Notebook is used by all the research groups in the department and is the preferred programming environment for all the Computational-based modules in the department.

Jupyter Notebook Basics

The Jupyter Notebook offers a wide range of functionalities, which are thoroughly explained in the Jupiter Notebook documentation pages. For this week's purpose, a brief introduction is shown below.

Create a new Notebook

First let's create a new Jupyter notebook. Once you start Jupyter Notebook, create a new Python 3 notebook as shown below, highlighted in a read circle:

Name your new Notebook

Once you created a new Notebook, it will be called "Untitled". You can click on that name and change it to something more appropriate - for today's work change the name to "Week 1"

Run a command in a cell

You can run a simple command, by entering it in a Code Cell. Enter, for example "2+4" in the your notebooks cell. To execute the code hit Shift+Enter. The notebook will display the result and will add a new Code Cell.

Add text to your notebook

You can add text to your Notebooks, using Markdown Cells. Markdown Cells make it possible to add headings, lists and even mathematical equations into you Notebook - you can see a list of all of the possible things in the documentation page.

The image below shows how you can change a Code Cell to a Markdown Cell

Create a Level 1 Heading

Once you change the cell to a Markdown Cell, let's create a Level 1 heading by typing "# Week1"

Move cells up or down

With your "Week 1" heading created, lets try and move it at the top of the documents. You can do this by clicking the Up Arrow Icon as shown in the example:

Exporting your code

During this course you will have to upload your code onto Learning Central, both for the weekly PX1224 sessions and for your Assignments. Those of you familiar with your operating system, you should be able to find the location of your python code and share it as required (e.g. email it to collaborator or upload it to Learning Central).

An additional way to export you code is shown in the image below. By clicking on Files>Downlaod as> you will be presented with several options that are useful in different situations. For the purposes of this module, you will be required to upload your code on LC either as a Notebook (.ipynb) or plain text python files (.py). Once you click on your choice, the file will be exported/saved at a specified folder - usually your Downloads folder.

Installing Python and Jupyter

Jupyter Notebooks are mainly aimed to be run on the cloud and during the second and third year of your degree you will, indeed, be using cloud computing to run you code.

However, as part of your training, for Year 1 you are required to install and run python and and jupyter on your own laptop or PC.

The preferred python/jupyter distribution for this module is the Anaconda Individual Edition, a popular Python distribution platform which offers an extensive cloud-based repository of over 7,500 data science and machine learning packages

Please try to install the 64-Bit Python3.X for your Operating System by selecting the appropriate installer If you have any trouble installing the software, contact the PX1224 module organiser.