Exercise indexing and slicing

Introduction

This notebook provides a small collection of exercises about fancy indexing and boolean indexing.

Requirements

Knowledge

This notebook is the exercise part for the chapters about fancy indexing and boolean indexing from the notebook NumpyAndArrays, so all the knowledge you will need to solve these exercises is provided by this theory notebook.

It would be an asset if you have done the notebook exercises_indexing_and_slicing before to be more familiar with these basic thechniques.

Modules

import numpy as np

Exercises

Fancy Indexing

Consider the following array.

np.random.seed(42)
A = np.random.randint(0,10, size=10)
print(A)

Task Use fancy indexing to get the 2nd, 3th and 6th element.

Consider the following matrix.

np.random.seed(42)
M = np.random.randint(0, 100, size=(8,8)) 
M = np.array(M).astype(int)
print(M)

Task Use fancy indexing to get the 4th and 7th row.

Task Use fancy indexing to get the 3rd,4th and 7th column.

Solution

Task Use fancy indexing to get the elements (2,3), (4,4) and (3, 1).

[37, 14, 75]

Task Use fancy indexing to print the first two minor diagonals.

[92 87 37 88 61  6  8]

[74 87 57 79  2 59 70]

Task Draw 10 random elements with replacement from the matrix by fancy indexing.

Consider the following 4D array.

M1 = np.random.randint(0,100, size=(3,3)) 
M2 = np.random.randint(0,100, size=(3,3)) 
M3 = np.random.randint(0,100, size=(3,3))  
M4 = np.random.randint(0,100, size=(3,3)) 

M = np.array([[M1,M2],[M3,M4]]).astype(int)
print(M)

Task Get the diagonals from each of the inner matrices M1, M2, M3 and M4 by fancy indexing.

Boolean Indexing

Consider the following matrix.

np.random.seed(42)
M = np.random.randint(0,10, size=(6,6)) 
M = np.array(M).astype(int)
print(M)

Task Find the boolean mask which filters every element smaller 4 extract a vector containing only these elements

print(M[Mask])

Task Find the mask which filters every element between 4 and 8 and extract a vector containing only these elements.

print(M[Mask])

Task Find all elements which are divisible by 3 without remainder.

Task: Using fancy indexing, set all elements which are less than 5 to 0 and all elements which are 5 or greater to 10. Store the result in a new matix. Check that the original matrix has not changed!

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_fancy_indexing
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.