Exercise broadcasting

Introduction

This notebook provides a small collection of exercises about broadcasting. You will work your way from from easy to more advanced examples. You'll perform certain tasks using loops on the one hand and broadcasting on the other, and compare the two approaches with respect to memory usage and computation time.

Requirements

Knowledge

This notebook is the exercise part for the chapter about broadcasting from the notebook NumpyAndArrays.

Study also the numpy documentation for broadcasting:

It might be of help to also read and solve the exercises for all previous chapters.

Modules

import numpy as np
import sys

Exercises

Consider the following matrix.

M = np.arange(25).reshape(5,5)
print(M)

Task Add, subtract, multiply and divide all elements by 4.

Exercise memory usage

In the following task you will see how much storage and computional time we actually save using broadcasting.

Task Generate a 5000x5000 random matrix and multiply it by 4 using two different approaches:

  • by making use of numpy broadcasting
  • using a 5000x5000 matrix filled with 4s Compare the storage used by using the nbytes funtion for array objects and sys.getsizeof(.) for numbers.

Hint: On this website you find more information about the Memory Management in Python.

print(help(sys.getsizeof))

Exercise computation time

In the exercise above we saw that broadcasting saves a lot of memory. Now lets see how much computation time we save.

Task Generate a random 5000x5000 matrix and multiply each element by 4.0 using two different approaches:

  • using broadcasting
  • using a python loop

Measure the computation time needed and compare them.

Use time.time() to get the actual system time.

import time as time

General exercises

Task Given the following array A, generate from it an (additional) column vector B (shape=(4,1)) and broadcast them by multiplication * into a square matrix. Explain the result.

A = np.arange(1,5)
print(A)

Task

Given are the following arrays:

A = np.random.randint(0,10, size=(2,3,1))
B = np.random.randint(0,10, size=(2,3,3))
C = np.random.randint(0,10, size=(1,3,2))

Which of the following operations are not permitted? Think about it before you try it out. Explain why?

A*B
A*C
B*C

Task Given a unit vector in the form of an array and a matrix of data points.
Calculate the euclidian distance between each data point to that unit vector.

np.random.seed(42)
Unit = np.array([1,0,0])
print(Unit)
print()
Data = np.random.rand(6,3).round(1)
print(Data)

Task Given a matrix with three unit vectors and a matrix of data points, calculate the distance between each unit vector and each data point.

np.random.seed(42)
Axes = np.eye(3)
Data = np.random.rand(6,3).round(1)

print(Axes)
print()
print(Data)
print(Data.shape)

Licenses

Notebook License (CC-BY-SA 4.0)

The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).

exercise_broadcasting
Oliver Fischer
is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://gitlab.com/deep.TEACHING.

Code License (MIT)

The following license only applies to code cells of the notebook.

Copyright 2019 Oliver Fischer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.