Skip to content

A crash course on NumPy for students enrolled in the course INF264 – Introduction to Machine Learning.

Notifications You must be signed in to change notification settings

odinhg/inf264-python-crash-course

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

INF264 – Python Crash Course – An Introduction to NumPy and Matplotlib

Welcome to the INF264 Python crash course. This repository contain guides (Jupyter notebooks) to help you get familiar with NumPy and Matplotlib, two Python libraries required for completing homeworks and the two mandatory projects in INF264.

If you encounter any problems, feel free to ask a group leader or post your questions in the Discord channel #python-crash-course.

You are also very welcome to join the dedicated crash course session on Monday, August 19th from 0815 to 1000 in the room Aktivt rom 2+3 - Marineholmen (ground floor at Høyteknologisenteret).

Prerequisites

We will assume that you are familiar with the basics of Python programming including topics such as data types and structures, conditional statements, functions, loops etc. If you feel you need to brush up on your Python skills, have a look at the free online course Python Programming MOOC 2023 (part 1 – 7).

Table of Contents

Installing Required Software for INF264

1. Installing Anaconda

To install Anaconda, click the link below corresponding to your operating system and follow the instructions there.

2. Create an Environment

We will now create a new Anaconda environment for INF264.

  1. Open a terminal window.
  2. Run the command conda create --name=INF264 python to create a new environment named INF264 with the latest Python version.
  3. Run the command conda activate INF264 to activate the newly create environment.
  4. Run conda install -c conda-forge numpy scikit-learn jupyter pandas matplotlib ipykernel to download and install the required packages.

To test that everything is working as expected, create a new notebook by running jupyter-lab (if you use VS Code, see below) and create and run a code cell with the following content:

import sklearn
import numpy
import pandas
import matplotlib.pyplot

Notebooks in VS Code Instructions

If your are using VSCode, you can also work with Jupyter notebooks there.

  1. Install the Jupyter extension in VS Code (if you do not have it).
  2. Create a new file File >> New File ... and choose Jupyter Notebook.
  3. Click Select Kernel in the upper right corner and choose the kernel INF264 that we created using Anaconda.
  4. Edit the code cell by clicking on it and paste the above code with imports.
  5. Press the "play button" on the cell (or Ctrl + Enter) to run the code cell.
  6. If everything is installed properly, you should see no error messages.

animation showing how to create a jupyter notebook in vs code

Useful notebook shortcuts:

  • Arrow keys to navigate between cells.
  • Shift + Enter to run the current cell and move to the next one.
  • Ctrl + Enter to run the current cell and stay in the same cell.
  • Esc to enter command mode.
  • Enter to enter edit mode.
  • A to insert a new cell above the current cell.
  • B to insert a new cell below the current cell.
  • DD to delete the current cell.
  • M to change the current cell to a Markdown cell.
  • Y to change the current cell to a code cell.

3. Managing the Environment

Enter the environment by running conda activate INF264. To exit the INF264 environment, simply run conda deactivate.

(Optional) To update all packages in your environment, run conda update --all (when the environment is already activated).

(Optional) At the end of the semester, if you want to clean up and delete the INF264 environment from your system, run the command conda env remove --name INF264.

Course Content

The course content mainly consists of Jupyter notebooks (see below) containing many examples and some exercies to help you get familiar with both NumPy and Matplotlib.

What is NumPy?

NumPy, short for Numerical Python, is a powerful library for numerical computing in Python. It provides support for arrays, matrices, and many mathematical functions to operate on these data structures efficiently.

For more details about the differences and advantages of using NumPy arrays as oposed to Python lists when working with numerical data, see "What is NumPy?" from the NumPy documentation.

It is convention to import NumPy under the alias np by writing

import numpy as np

in the beginning of the Python file or notebook where you want to use NumPy functions.

The (very useful) NumPy documentation can be found here (link).

What is Matplotlib?

Matplotlib is a popular and flexible library for creating many types of data visualizations and plots.

It is convention to import matplotlib's pyplot module under the alias plt by writing

import matplotlib.pyplot as plt

The documentation for matplotlib can be found here (link).

Notebooks with Examples and Exercises

You can download all the notebooks by downloading this repository as a ZIP file (see screenshot below), or use git clone if you are familiar with git.

Download zipped repo

If you use VS Code, go to File >> Open Folder... and choose the directory notebooks (after unzipping the repository). Remember to choose the INF264 environment as the kernel for the notebooks.

NumPy Notebooks

  1. Creating NumPy Arrays
  2. Indexing NumPy Arrays
  3. Reshaping, Transposing and Concatenating Arrays
  4. Basic Array Operations and Broadcasting
  5. Matrix and Vector Algebra in NumPy
  6. Reduction Operations in NumPy
  7. Other Useful NumPy Functions

It is recommended to work through the NumPy notebooks in the order they are listed.

Matplotlib Notebook

Download and go through the notebook A Short Introduction to Matplotlib.

Solutions

You are encouraged to try to complete all the exercises on your own. If you get completly stuck, notebooks with solutions can be found in the directory notebooks/with_solutions.

About

A crash course on NumPy for students enrolled in the course INF264 – Introduction to Machine Learning.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published