Python Numpy Basics

Introduction

In this exercise you will learn numpy operations / features, which you will need throughout nearly all data science tasks, when working with python.

Requirements

Knowledge

You should have a basic knowledge of:

  • numpy

Suitable sources for acquiring this knowledge are:

Python Modules

By deep.TEACHING convention, all python modules needed to run the notebook are loaded centrally at the beginning.

# External Modules
import numpy as np

Exercises

Task:

Generate a numpy-1D array of length 10, all elements being 0, except the 5th element which is 1

### Your Solution

Task:

Reverse the order of vector z: The first element becomes the last, the second becomes the second last etc.

z = np.arange(50)
### Your Solution

Task:

Find the indices of all elements that are nonzero.

z = np.array([1,2,0,0,4,0])
### Your Solution

Task:

Generate a 10x10 array with random values and find the smallest and largest value.

### Your Solution

Task:

Generate a vector of length 50 with random values and calculate the mean.

### Your Solution

Task:

Explain the following results:

print(0 * np.nan)
print(np.nan == np.nan)
print(np.nan is np.nan)
print(np.inf < np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(0.3 == 3 * 0.1) # How can you fix this?

Task:

Generate an 8x8 matrix and fill it with a chess pattern like:

array([[1., 0., 1., 0., 1., 0., 1., 0.], [0., 1., 0., 1., 0., 1., 0., 1.], [1., 0., 1., 0., 1., 0., 1., 0.], [0., 1., 0., 1., 0., 1., 0., 1.], [1., 0., 1., 0., 1., 0., 1., 0.], [0., 1., 0., 1., 0., 1., 0., 1.], [1., 0., 1., 0., 1., 0., 1., 0.], [0., 1., 0., 1., 0., 1., 0., 1.]])

### Your Solution

Task:

Generate a random 5x5 matrix and normalize (scale it by a factor) it. That means, the smallest value should become 0.0, the largest 1.0

### Your Solution

Task:

From each row, subtract the maximum value of that row.

z = np.arange(12).reshape((3,4))
z
### Your Solution

Task:

Divide each column by the sum of that column. Then verify that each column adds up to 1.

z = np.arange(12).reshape((3,4))
z
### Your Solution

Task:

Negate (multiply with -1) all elements between 3 and 8 in place.

Z = np.arange(11)
Z
### Your Solution

Task:

Explain the result (output) of the following code:

### Your Solution
print(sum(range(5),-1))
from numpy import *
print(sum(range(5), -1)) 
del sum

Task:

Generate a random vector of length 100 and sort it.

### Your Solution

Task:

Check if two arrays are equal:

1. All elements should be exactly the same
2. Equality within a tolerance
### Your Solution
A = np.random.random((3,4))
B = A.copy()
B[1,2] = A[1,2] * 1.00000000000001
print (A)

Task:

Generate (as little code as possible) the following matrix with np.array and save it as the variable arr.

\begin{bmatrix} 1 & 1 & 1 &1 &1 \ 1 & 2 & 1 & 1 & 1\ 1 & 1 & 3 & 1 & 1\ 1 &1 & 1 & 4 & 1 \end{bmatrix}

And calculate:

  • the sum of each line
  • the sum of each row
arr = np.ones([4,5])
arr
### Your Solution

Task:

Generate a 2x2 matrix from arr: It shall consist of the 4 values when taking the values of the 2nd and 4th column of arr and the even rows.

Use different methods: (see http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)

  • integer array indexes
  • slices
  • boolean arrays
### Your Solution

Task:

(see http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html)

Explain the following operations on arr

print(arr)
print('--------1-------')
print(arr * 5.)
print('--------2-------')
print(np.arange(arr.shape[1]))
print(arr * np.arange(arr.shape[1]))
print('--------3------')
print(arr.T * np.arange(arr.shape[0]))
print('--------4-------')
print(arr * np.arange(arr.shape[0]))

Task:

Calculate the matrix-vector product (dot product) of arr and$ v $:

with: $ v = (1,2,3,4,5)^T $

### Your Solution

Summary and Outlook

In this notebook you've picked up many of the essential numpy operations for maths and data science tasks.

The more you use the numpy library, the more of its functionality you'll discover and its usage will grow more intuitive and familiar.

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: Python Numpy Basics
by Klaus Strohmenger
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 Klaus Strohmenger

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.