ML-Fundamentals - Python Numpy Basics

Introduction

In this exercise you will learn numpy operations / features, which you will need throughout in 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

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

### Your Solution


Reverse the order of vector z: First element becomes the last, second becomes the second to the last etc.

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

Find the indizes of all elements nonzero.

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

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

### Your Solution


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

### Your Solution


Explain the following results:

print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(0.3 == 3 * 0.1)

Generate a 8x8 matrix and fill it with a chess patter 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


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

### Your Solution


Negate all elements between 3 and 8 in place

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


Explain the result (output) of the following code:

### Your Solution
print(sum(range(5),-1))
from numpy import *
print(sum(range(5), axis=-1)) # axis -1
### SOLUTION
# sum before was pytohn sum, now is numpy.sum

Generate a random vector of length 100 and sort it.

### Your Solution


Check if two arrays are equal:

1. All elements shall 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)

Generate (as less code as possible) the following matrix with np.array and save it under 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
### Your Solution


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


Explain the following operations on arr

### Your Solution
print(arr)
print('--------1-------')
print(arr * 5.)
print('--------2-------')
print(np.arange(arr.shape))
print(arr * np.arange(arr.shape))
print('--------3------')
print(arr.T * np.arange(arr.shape))
print('--------4-------')
print(arr * np.arange(arr.shape))

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

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

### Your Solution


Summary and Outlook

[TODO]

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