# ML-Fundamentals - Python Numpy Basics

## Table of Contents

## 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

**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`

: First element becomes the last, second becomes the second to the last etc.

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

**Task:**

Find the indizes of all elements nonzero.

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

**Task:**

Generate a 10x10 array with random values and find the smallest and biggest 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.inf > np.nan)
print(np.nan - np.nan)
print(0.3 == 3 * 0.1)
```

**Task:**

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
```

**Task:**

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

```
### Your Solution
```

**Task:**

Negate all elements between 3 and 8 in place

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

**Task:**

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
```

**Task:**

Generate a random vector of length 100 and sort it.

```
### Your Solution
```

**Task:**

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)
```

**Task:**

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
```

**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`

```
### Your Solution
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 matric-vector product (dot product) of `arr`

and$ v $:

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

```
### Your Solution
```

## Summary and Outlook

[TODO]

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