Home
Course Guidelines
About the course Prerequite Material References
Python
Jupyter Notebooks Python overview
Exercises
Before the semester start: Installation and exercise setup Week 1: Introduction to Python and libraries Week 2: Vector representations Week 3: Linear Algebra Week 4: Linear Transformations Week 5: Models and least squares Week 6: Assignment 1 - Gaze Estimation Week 7: Model selection and descriptive statistics Week 8: Filtering Week 9: Classification Week 10: Evaluation Week 11: Dimensionality reduction Week 12: Clustering and refresh on gradients Week 13: Neural Networks Week 14: Convolutional Neural Networks (CNN's)
Tutorials
Week 1: Data analysis, manipulation and plotting Week 2: Linear algebra Week 3: Transformations tutorial Week 4: Projection and Least Squares tutorial Week 7: Cross-validation and descriptive statistics tutorial Week 8: Filtering tutorial Week 11: Gradient Descent / Ascent
In-class Exercises
In-class 1 In-class 2 In-class 10 In-class 3 In-class 4 In-class 8
Explorer

Document

  • Overview

Content

  • Installation
    • Task 1 Download Anaconda
    • Task 2 Prepare Anaconda
  • Using Anaconda
  • Setting the default environment
    • Task 3 Setting default environment
    • Editors and workflow
      • Jupyter Lab
    • Workflows
      • Task 4 Editor workflow
      • Task 5 Jupyter Lab workflow
  • Python REPL
  • Documentation and help

Quick guide through the structure of the material for the course

Live walk through

TA's will begin the session with providing a quick live overview of the structure of the material for the course, such as:

  • Information about lecture preparation and exercises on LearnIT.
  • The material folder on LearnIT.
  • The IML webpage
  • Grasple

Installation and setup

This exercise will walk you through the installation process for setting up the programming environment required for the IML course. Please follow the provided guidelines carefully. We strongly advise against installing newer versions of software packages, even if they are available, as doing so may result in an unstable installation.

Info

While the installation may work on Linux, we have only tested and support installations on Windows and Mac

Anaconda is used to manage packages and environments.

Installation

Task 1: Download Anaconda
  1. Go to the anaconda webpage and download the installer for Python 3.11 . Click on 'Free Download' in the upper right corner and provide your ITU email.
  2. Install Anaconda by following the installation guide for your platform .
Tip

Windows users should check the "use Anaconda as default Python" checkbox when prompted during the installation.

Task 2: Prepare Anaconda
  1. Copy the course material (Course Material tab on LearnIt under Exercises/jupyterlab/W00) for this week before proceeding with this task.

  2. On macOS and Linux, open a terminal. On Windows, open the Anaconda Prompt application.

  3. Navigate to your local folder containing the yml environment file.

  4. Execute the command

    conda env create -f iml_env.yml
    conda env create -f iml_env.yml

    This creates a new environment called iml and automatically installs the necessary packages used for the course, including Scikit Learn , Numpy , and Pytorch .

Using Anaconda

Anaconda allows easy switching between environments, each of which may contain different versions of Python and packages. Although we only use a single environment for IML, you may want to create your own custom environments. This practice prevents you from accidentally disrupting other projects by, for example, updating a package with breaking API changes.

Environments need to be activated to be used. To activate an environment, use the following command:

conda activate [env-name]
conda activate [env-name]
Tip

The name of the active environment is shown in the terminal to the left of the usual prompt.

We have listed a number of commonly used Anaconda commands here:

Lists installed packages and versions in the current environment:

conda list
conda list

Install the specified package using Anaconda:

conda install [package]
conda install [package]

Remove the specified package:

conda uninstall [package]
conda uninstall [package]

Create new environment with specified name and python version:

conda env create -n <name> python=[version]
conda env create -n  python=[version]

Update specific package or all package in currently active environment:

conda update [package/--all]
conda update [package/--all]

Additional information can be found using conda --help , conda <command> --help , or the official Anaconda documentation .

Setting the default environment

Activating the same environment every time you open a terminal can be tiresome. You can easily set up a default environment to automatically activate upon opening a terminal

Task 3: Setting default environment

For Windows users:

  1. Open the file Activate.bat in C:\Users\<YourUserName>\AppData\Local\Continuum\anaconda3\Scripts . If it doesn't exist in this location, try searching for the file Activate.bat in your user folder.

  2. Add conda activate iml to the end of the file.

  3. Test that the environment is automatically activated by opening a new anaconda prompt (find the app in the start menu).

For macOS and Linux users:

  1. Open the ~/.bashrc file (or equivalent if you are using another shell).

  2. Add the line conda activate iml to the end of the file.

  3. Open a new terminal and check that the environment is activated (the prompt should be preceded by (iml) )

Editors and workflow

During the course, you may either want to use Python directly from the terminal using a text editor such as Visual Studio Code or SublimeText , or indirectly from an IDE such as PyCharm or Wing . These editors are either free or have free editions for students. We recommend you to start with one of these as they make debugging significantly easier.

Important

We recommend that you use use either Jupyter Lab (not notebook) or Visual Studio Code to solve the exercises as you may otherwise risk bad formatting of the exercises.

Jupyter Lab

Jupyter Lab will be used for some of the exercises. Jupyter Lab is a web-based IDE for developing interactive scripts in notebook format, making it possible to intertwine markdown-based text with code and visual output. The course will primarily use it to introduce theoretical concepts as well as initial data exploration.

We assume that you have activated the iml environment using conda activate iml . To start Jupyter Lab, either run jupyter lab from the terminal, or use the Anaconda Navigator application.

Workflows

Task 4: Editor workflow

This exercise aims to introduce you to a basic workflow using Scikit Image and Python with either a text editor and terminal, or using the Jupyter Lab environment.

  1. In a text editor or IDE create a new document called ex1_editor.py .

  2. Enter the code snippet and save the changes

    from skimage import data from matplotlib import pyplot as plt img = data.astronaut() plt.show(img)
    from skimage import data
    from matplotlib import pyplot as plt
                         
    img = data.astronaut()
    plt.show(img)

See here for a full list of possible sample images in Scikit Image.

  1. In a terminal (or Anaconda prompt on Windows) navigate to the directory where the script is located.

  2. Activate the iml environment by conda activate iml .

  3. Run the python script by typing python ex1_editor.py in the terminal.

Task 5: Jupyter Lab workflow
  1. Open a terminal (or Anaconda Prompt on windows) and activate the iml environment using the command conda activate iml .

  2. Start Jupyter Lab by running the command jupyter lab . This should open Jupyter in a browser window.

  3. Create a new notebook by clicking the Python 3 button under notebook in the center of the screen as shown to the right.

    Alternatively, you can create new files by navigating to File $\triangleright$ New $\triangleright$ Notebook and then selecting Python 3 as the kernel.

  4. Paste the following code into the code cell:

    from skimage import data from matplotlib import pyplot as plt img = data.astronaut() plt.imshow(img)
    from skimage import data
    from matplotlib import pyplot as plt
                         
    img = data.astronaut()
    plt.imshow(img)

    Execute the code by pressing Shift + Enter.

  5. Change the code so that it converts the color image to grayscale and show the result. Use the documentation for skimage.color.rgb2gray here .

  6. Paste the following code into a new cell:

    import numpy as np # Create a one-dimensional array of zeros zeros_array = np.zeros(10) print("Original array:", zeros_array) # Replace the fifth element zeros_array[4] = 1 print("Modified array:", zeros_array)
    import numpy as np
       
    # Create a one-dimensional array of zeros
    zeros_array = np.zeros(10)
    print("Original array:", zeros_array)
    
    # Replace the fifth element
    zeros_array[4] = 1
    print("Modified array:", zeros_array)
    Execute the code by pressing Shift + Enter.

  7. Change the code so that the first and last elements of the array are modified to -1. Then, use the code below to plot your modified array:

    import matplotlib.pyplot as plt plt.figure(figsize=(10, 5)) plt.stem(zeros_array, basefmt=" ", use_line_collection=True) plt.title('Plot of the Modified Array') plt.xlabel('Index') plt.ylabel('Value') plt.grid(True) plt.show()
    import matplotlib.pyplot as plt
    
    plt.figure(figsize=(10, 5))
    plt.stem(zeros_array, basefmt=" ", use_line_collection=True) 
    plt.title('Plot of the Modified Array')
    plt.xlabel('Index')
    plt.ylabel('Value')
    plt.grid(True)
    plt.show()
  8. Paste the following code into a new cell:

    from scipy import stats # Through scipy.stats.describe() we obtain descriptive statistics description = stats.describe(zeros_array) print("Mean: ", description.mean)
    from scipy import stats
       
    # Through scipy.stats.describe() we obtain descriptive statistics
    description = stats.describe(zeros_array)
    print("Mean: ", description.mean)
    Execute the code by pressing Shift + Enter.

  9. Write a new print statement, which outputs the variance, and the minimum and maxium values identified in the previous step. Use the documentation for scipy.stats.describe here .

  10. Paste the following code into a new cell:

    import torch # Create a tensor tensor = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0]) mean_value = torch.mean(tensor) print("Mean of elements:", mean_value)
    import torch
       
    # Create a tensor
    tensor = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
    mean_value = torch.mean(tensor)
    print("Mean of elements:", mean_value)
    Execute the code by pressing Shift + Enter.

  11. Write a new print statement, which outputs the sum of the previously created tensor . Use the documentation for torch.sum here .

Python REPL

Since Python is an interpreted language, it has a REPL (read-eval-print-loop) which allows you to write and execute code line by line. To start the REPL, simply run python in the terminal without any arguments. You should be presented with the following output:

Python 3.9.5 (default, May 18 2021, 12:31:01) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Python 3.9.5 (default, May 18 2021, 12:31:01)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can then type any Python code in here including module imports.

Documentation and help

Links to documentation pages can be found in the references page on LearnIT. Additionally, Python has a function, help , to get documentation for any class , function , or module . It is especially useful in combination with the interactive REPL and Jupyter Lab.