0% found this document useful (0 votes)
57 views

Python Numpy Primer

Uploaded by

feriel.bouguecha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Python Numpy Primer

Uploaded by

feriel.bouguecha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Python & NumPy Primer

IC-CVLab

2.3.2021

1
Python

2
About Python
• High-level, focus on readable and concise code - fast prototyping.

• Interpreted language, dynamic typing - very flexible.

• Slow run-time (dynamic typing, memory management, …)


• Unless compiled to parallelized machined code (Numba)
https://public-cvlab.epfl.ch/articles/2020/n-queens-numba/

• Quick-and-dirty scripting, glue code - combine components written


in other languages (NumPy, SciPy, scikit-learn).

• Vast amount of libraries - from backend web development, games


to data science.

3
Working with Python
Installation
• Install the interpreter and packages manually
• OSX and some Linux distributions already include Python
• pip - package installer

• Get a readily available distribution


• Anaconda

Running
• Terminal (interactive or script mode)
$ python
>>> print('hello CS322')
'hello CS322'
>>> # Use Ctrl-D, Ctrl-Z or quit() to exit

$ python my_script.py
'hello 'CS322'
• Jupyter Notebook

4
Data Types
• Python is dynamically typed language.
• Data type inferred at runtime.
• Can change during runtime.

>>> a = 1
>>> print(type(a))
<class 'int'>

>>> a = 'python is fun'


>>> print(type(a))
<class 'str'>

• function type() returns the type of a value.

>>> a = 42 int
>>> a = 3.142 float 64 bit
>>> a = 3. float 64 bit
>>> a = 2+3j Complex
>>> a = True bool
>>> a = None None

5
Data Types
• Strings can be single, double or triple quoted.

>>> a = 'stay strong'


>>> a = "it is not that difficult”

>>> a = """This is a string that will span across


multiple lines. Using newline characters
and no spaces for the next lines.”"" # docstring

• Quotes do not change anything.

>>> a = 'this is a string'


>>> b = "this is a string"
>>> print(b == a)
True

6
ADT(Abstract Data Type) - list
• Contains a series of values.

>>> a = [] # empty
>>> b = [1, 2, 3, 4] # 4 elements
>>> c = [1, 'cat', 0.23] # mixed types
>>> d = [1, ['cat', 'dog'], 2, 3] # list in list

>>> a = ['great', 'minds', 'think', 'alike']


>>> print(a[0]) # zero indexed
'great'

>>> print(a[0:2]) # from start to end-1


['great', 'minds']

>>> a[2] = 'look' # manipulate vals at given index


>>> print(a)
['great','minds','look','alike']

7
ADT - list
• Length of a list.
>>> a = ['great','minds','think','alike']
>>> print(len(a)) #length of list
4

• Adding item to a list.


>>> a = ['great', 'minds', 'think', 'alike']
>>> a.append('sometimes')
['great', 'minds', 'think', 'alike', 'sometimes']

• Extend a list by another list.


>>> a = ['great', 'minds']
>>> b = ['think', 'alike']
>>> c = a + b
['great', 'minds', 'think', ‘alike']

>>> a.extend(b) # in place


['great', 'minds', 'think', 'alike']

8
ADT - tuple
• Similar as list but fixed in size and immutable = change not allowed.
• Declaration with parentheses.

>>> username = ('rick', 'morty', 'beth','squanchy')


>>> print(username[1])
'morty'
>>> username[0] = 'rick2' # from other universe
TypeError: 'tuple' object does not support
assignment

• Tuple unpacking

>>> a, b = (1, 2)
>>> print(b)
2

9
ADT - dictionary
• Collection of key:value pairs
Access value with keys
Key must be immutable and unique

>>> age = {‘carl': 23, 'elis': 25}


>>> print(age[‘carl'])
23

>>> age['carl'] = 32 # modify


>>> print(age['carl'])
32

>>> age['peter'] = 18 # add new key


>>> print(age)
{'carl': 32, 'peter': 18, 'elis': 23}

>>> print(age.keys()) # get keys


dict_keys(['elis', 'carl', ‘peter'])
>>> print(age.values()) # get values
dict_values([25, 23, 18])

10
Type Casting
>>> a = 42 # int -> float
>>> a = float(a)
>>> print(a, type(a))
42.0 <class 'float'>

>>> a = 3.14 # float -> int (floor, not round!)


>>> a = int(a)
>>> print(a, type(a))
3 <class 'int'>

>>> a = '123' # string -> int


>>> a = int(a)
>>> print(a, type(a))
123 <class 'int'>
>>> a = 123.14 # float -> string
>>> a = str(a)
>>> print(a, type(a))
'123.14' <class 'str'>

11
Type Casting
>>> a = [42, 21.2, 'black'] #list -> tuple
>>> a = tuple(a)
>>> print(a)
(42, 21.2, 'black')

>>> a = (42, 21.2, 'black') # tuple -> list


>>> a = list(a)
>>> print(a)
[42, 21.2, ‘black’]

12
Functions
>>> def print_hello():
print("hello world”)

>>> def multiply(a, b):


return a * b

• Default arguments and multiple return values.


>>> def add(a, b, c=0, d=1):
return a + b + c + d
>>> add(1, 2, d=5)
8

• Multiple return values - a function returns a tuple, use tuple unpacking.


>>> def min_max(l):
return min(l), max(l)
>>> m, x = min_max([1, 2, 3, 4])
>>> print(m, x)
1, 4

13
Builtin Functions
• Python comes with various useful builtin functions.

• range, zip, type, str, int, sorted, len, sum, max, min, abs, any, all, …

>>> list(range(9))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> type('hello')
str
>>> sorted((2, 1, 3))
[1, 2, 3]
>>> sorted('eba')
['a', 'b', 'e']

14
Builtin Functions
>>>len('abc')
3
>>>sum([1, 2, 3])
6
>>>max([1, 2, 3])
3
>>> min([1, 2, 3])
1
>>> abs(-2)
2
>>> a = [False, False, True]
>>> any(a)
True
>>> b = [False, False, False]
>>> any(b)
False
>>> c = [True, True, True]
>>> all(c)
True

15
List Comprehensions
• List comprehensions provide a concise way to create lists.

• Loosely follows mathematical set-builder notation.

{2x | x ∈ {0...10}}
>>> powers2 = [2**x for x in range(11)]
>>> print(powers2)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]

{2x | x ∈ {0...10} ∧ x is even}


>>> powers2ev = [2**x for x in range(11) if x % 2 == 0]
>>> print(powers2ev)
[1, 4, 16, 64, 256, 1024]

16
Import
• Gains access to code in another module by importing it.

>>> import numpy


>>> x = numpy.arange(5)

• Import module or just specified functions/variables/classes from the module.

>>> from numpy import arange


>>> x = arange(5)

• Select a name for imported module.

>>> import numpy as np


>>> x = np.arange(5)

17
Getting help
• In Jupiter Notebook - function name with “?”.

>>> sum?
Signature: sum(iterable, start=0, /)
Docstring:
Return the sum of a 'start' value (default: 0)
plus an iterable of numbers

When the iterable is empty, return the start value.


This function is intended specifically for use with
numeric values and may reject non-numeric types.
Type: builtin_function_or_method

• Read (Python and NumPy) documentation.

18
NumPy

19
About NumPy
• Core library for scientific computing in Python.

• High-performance multidimensional array - matrix operations.

• Wide ecosystem of libraries that take NumPy arrays as input.

NumPy Arrays
• high-performance multidimensional arrays.

• A grid of values, all of the same type.

• Indexed by a tuple of nonnegative integers.


• Indexing syntax similar to lists, tuples, and dictionaries

• The rank of the array is the number of dimensions

20
Creating Arrays I

# import numpy
>>> import numpy as np

# create a rank 1 array


>>> np.array([1, 2, 3])
array([1, 2, 3])

# create a rank 2 array


>>> np.array([[1, 2, 3], [4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6]])

21
Creating Arrays II

>>> np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])

>>> np.ones((2,3))
array([[1., 1., 1.],
[1., 1., 1.]])

>>> np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])

22
Creating Arrays III

>>> np.arange(1, 7, 1).reshape((2, 3))


array([[1, 2, 3],
[4, 5, 6]])

>>> np.linspace(1, 5, 9)
array([1. , 1.5, 2. , 2.5, 3. , 3.5, 4. ,4.5, 5. ])

23
Inspecting Arrays
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6]])

>>> x.shape
(2, 3)

>>> x.ndim
2

>>> x.size
6

>>> x.dtype
dtype(‘int64')

>>> y = np.array([1, 2, 3], dtype=np.float32)


>>> y.dtype
dtype('float32')

24
Indexing

• Indexing in python is zero-based.


>>> x = ['a', 'b', 'c', 'd', 'e']
>>> x[0]
'a'

• The n-th last entry can be indexed by ‘-n’.


>>> x[-1]
'e'

• For a rank 2 array, the first index refers to the row & the second
to the column.

25
Indexing multidimensional arrays

• A d-dimensional array - array of d-1 dimensional arrays

• A 2D numpy array is an array of 1D vectors (rows)

>>> m = np.arange(9).reshape((3, 3))


array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

>>> m[0]
array([0, 1, 2])

>>> m[1]
array([3, 4, 5])

• In the same way, a 3D array is an array of 2D matrices

26
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly

example: A[0, 2]

• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension

>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]

>>> print(A[0, 2])


3

27
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly

example: A[0, 2]

example: A[:, 1]

• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension

>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]

>>> print(A[:, 1]) # all the rows, column 1


array([2, 5])

28
Indexing multidimensional arrays
• NumPy also allows to index along a specific dimension explicitly

example: A[0, 2]

example: A[:, 1]

example: A[1, :]
• syntax:
• comma ‘,’ - separate dimensions
• colon ‘:’ - get all values in given dimension

>>> A = np.array([[1,2,3],[4,5,6]])
>>> print(A)
[[1 2 3]
[4 5 6]]

>>> print(A[1, :]) # all the columns, row 1


array([4, 5, 6])

29
Indexing multidimensional arrays
Structural Indexing
• Syntactic sugar used to add an extra dimension to an existing array.
>>> x = np.array([1, 2, 3])
x.shape
(3,)
>>> x[np.newaxis].shape
(1, 3)

>>> x[:, np.newaxis].shape


(3, 1)

>>> y = np.arange(24).reshape((2, 3, 4))


>>> y.shape
(2, 3, 4)

>>> y[:, np.newaxis, :, :].shape


(2, 1, 3, 4)

30
Indexing multidimensional arrays
Structural Indexing
• Useful for broadcasting.
• Explicit shape, e.g. row/column vectors.

inner product
outer product
(dot product)

31
Indexing multidimensional arrays
Structural Indexing

outer product >>> x1 = np.array([1, 2, 3])


>>> x2 = np.array([2, 4, 6])

>>> x1 = x1[:, np.newaxis]


>>> x1.shape
(3, 1)

>>> x2 = x2[np.newaxis]
>>> x2.shape
(1, 3)

>>> x1 @ x2
array([[ 2, 4, 6],
[ 4, 8, 12],
[ 6, 12, 18]])

32
Indexing multidimensional arrays
Structural Indexing

inner product >>> x1 = np.array([1, 2, 3])


(dot product) >>> x2 = np.array([2, 4, 6])

>>> x1 = x1[np.newaxis]
>>> x1.shape
(1, 3)

>>> x2 = x2[:, np.newaxis]


>>> x2.shape
(3, 1)

>>> x1 @ x2
array([[28]])

33
Slicing: Basics
• Slicing is indexing of the form [start : stop : step]
• start including
• stop excluding
>>> x = np.arange(1, 11)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

>>> x[1:10:2]
array([ 2, 4, 6, 8, 10])

• If not specified, start = 0, stop = last elem., step = 1


>>> x[::2]
array([ 1, 3, 5, 7, 9])

>>> x[9:0:-1]
array([10, 9, 8, 7, 6, 5, 4, 3, 2])

>>> x[::-1]
array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])

34
Slicing: Basics
• Slicing provides a view of the original array.

>>> y = x[0:2]
>>> y[:] = 42
>>> x
array([42, 42, 3, 4, 5, 6, 7, 8, 9, 10])

35
Slicing: Example
syntax: [start : stop : step]

36
Masking: Basics
• Masking is indexing an array with an identically shaped Boolean array.

• Elements indexed with True / False are taken / discarded, respectively.

>>> x = np.arange(1, 6)
>>> x
array([1, 2, 3, 4, 5])

>>> mask = np.array([True, False, False, True, False])


>>> x[mask]
array([1, 4])

37
Masking: Example
• Get all even numbers in [1, 6] which are multiplies of 3.

>>> m = np.arange(1, 7)
array([1, 2, 3, 4, 5, 6])

>>> div2 = (m % 2 == 0)
array([False, True, False, True, False, True])

>>> div3 = (m % 3 == 0)
array([False, False, True, False, False, True])

>>> mask = div2 & div3


array([False, False, False, False, False, True])

>>> m[mask]
array([6])

38
Type casting

• Use .astype to convert numpy arrays between types.

• Use dtype to check the type.

>>> m = np.arange(0, 5)
array([ 0, 1, 2, 3, 4])

>>> m.astype(np.float32)
array([ 0., 1., 2., 3., 4.])

>>> m.astype(np.bool)
array([False, True, True, True])

>>> m.dtype
dtype('bool')

39
Broadcasting

• Describes how numpy treats arrays with different shapes during


arithmetic operations.

• Fast - operation is vectorised, heavily parallelized.

• Performed without making needless copies of data.

40
How broadcast works
• Smaller array is “broadcast” across the larger array so that they have
compatible shapes

41
Broadcasting: Basic Example
• Add scalar to every element of a vector.

>>> my_vector = np.array([1, 3, 5, 7, 9])


>>> my_scalar = -5
>>> print(my_vector)
[1 3 5 7 9]

>>> new_vector = my_vector + my_scalar


>>> print(new_vector)
[-4 -2 0 2 4]

42
Broadcasting: Advanced Example
• Convert binary numbers (in rows) to decimal

>>> bnums = np.random.randint(0, 2, (3, 6))


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

>>> ords = (2 ** np.arange(6))


array([ 1, 2, 4, 8, 16, 32])

>>> dnums = bnums * ords[np.newaxis]


array([[ 0, 2, 4, 8, 16, 0],
[ 1, 0, 4, 0, 16, 32],
[ 1, 0, 4, 8, 16, 32]])

>>> np.sum(dnums, axis=1)


array([31, 53, 61])

43
Broadcasting Rules
• The corresponding dimensions of 2 arrays must satisfy one of the following:
• Same dimension
• One of the dimensions is 1
• Non-existent dimensions are treated as 1
A (2d array): 5 x 4 A (3d array): 15 x 3 x 5
B (1d array): 1 B (3d array): 15 x 1 x 5
Result (2d array): 5 x 4 Result (3d array): 15 x 3 x 5

A (2d array): 5 x 4 A (3d array): 15 x 3 x 5


B (1d array): 4 B (2d array): 3 x 1
Result (2d array): 5 x 4 Result (3d array): 15 x 3 x 5

>>> a = np.ones((15, 3, 5))


>>> b = np.arange(3).reshape((3, 1))
>>> c = a * b
>>> c.shape
(15, 3, 5)

44
Common Functions in Numpy

1 2 3
a=
<latexit sha1_base64="oUHV7uuCKEIPOD6evmzUyUZzaEY=">AAACG3icbVDLSgMxFM34rPVVdekmWBRXZaatr4VQcOOygn1AZyiZzG0bmskMSUYsQ//Djb/ixoUirgQX/o3pA9HWC4cczrmX3Hv8mDOlbfvLWlhcWl5Zzaxl1zc2t7ZzO7t1FSWSQo1GPJJNnyjgTEBNM82hGUsgoc+h4fevRn7jDqRikbjVgxi8kHQF6zBKtJHauSK5dH3oMpH6IdGS3Q8dfISLBiXXLZvnxODUBRH8NLRzebtgjwvPE2dK8mha1Xbuww0imoQgNOVEqZZjx9pLidSMchhm3URBTGifdKFlqCAhKC8d3zbEh0YJcCeSBkLjsfp7IiWhUoPQN51mv56a9Ubif14r0Z1zL2UiTjQIOvmok3CsIzwKCgdMAtV8YAihkpldMe0RSag2cWZNCM7syfOkXiw4pYJ9U85XLqZxZNA+OkDHyEFnqIKuURXVEEUP6Am9oFfr0Xq23qz3SeuCNZ3ZQ3/K+vwGfAOejQ==</latexit>
4 5 6

• When we do not specify the axis parameter:

>>> sum_all_vals = np.sum(a)


>>> print(sum_all_vals)
21

45
Common Functions in Numpy
• Sum along the columns:
>>> sum_along_cols = np.sum(a, axis=1)
>>> print(sum_along_cols)
[ 6 15 ]
 
1 2 3 6
<latexit sha1_base64="5lnxL8zySiaH1KTLC07IsuJUu4M=">AAACGXicbVDLSgMxFM3UV62vqks3waK4KjNtfe0KblxWsA/oDCWTuW1DM5khyYhl6G+48VfcuFDEpa78G9MHoq0XDjmccy+59/gxZ0rb9peVWVpeWV3Lruc2Nre2d/K7ew0VJZJCnUY8ki2fKOBMQF0zzaEVSyChz6HpD67GfvMOpGKRuNXDGLyQ9ATrMkq0kTp52/Whx0Tqh0RLdj9y8DEuGZRdt2KeU4MzF0Tw09DJF+yiPSm8SJwZKaBZ1Tr5DzeIaBKC0JQTpdqOHWsvJVIzymGUcxMFMaED0oO2oYKEoLx0ctkIHxklwN1IGgiNJ+rviZSESg1D33Sa/fpq3huL/3ntRHcvvJSJONEg6PSjbsKxjvA4JhwwCVTzoSGESmZ2xbRPJKHahJkzITjzJy+SRqnolIv2TaVQvZzFkUUH6BCdIAedoyq6RjVURxQ9oCf0gl6tR+vZerPep60Zazazj/6U9fkNI7ed2w==</latexit>
4 5 6 <latexit sha1_base64="bvlLQhDy5WrfSD62omUTYZFOrT8=">AAACDHicbVDLSsNAFJ3UV62vqks3g0VwVRLf7gpuXFawD2hCmUxu2qGTSZiZiCX0A9z4K25cKOLWD3Dn3zhtg2jrgYHDOecy9x4/4Uxp2/6yCguLS8srxdXS2vrG5lZ5e6ep4lRSaNCYx7LtEwWcCWhopjm0Ewkk8jm0/MHV2G/dgVQsFrd6mIAXkZ5gIaNEG6lbrrg+9JjI/Ihoye5HZ9h1sXPqggh+NJOyq/YEeJ44OamgHPVu+dMNYppGIDTlRKmOYyfay4jUjHIYldxUQULogPSgY6ggESgvmxwzwgdGCXAYS/OExhP190RGIqWGkW+SZr++mvXG4n9eJ9XhhZcxkaQaBJ1+FKYc6xiPm8EBk0A1HxpCqGRmV0z7RBKqTX8lU4Ize/I8aR5VneOqfXNSqV3mdRTRHtpHh8hB56iGrlEdNRBFD+gJvaBX69F6tt6s92m0YOUzu+gPrI9vxUCbaA==</latexit>
15
• Sum along the rows:
>>> sum_along_rows = np.sum(a, axis=0)
>>> print(sum_along_rows)
[ 5 7 9 ]

1 2 3
4 5 6
⇥ ⇤
<latexit sha1_base64="5lnxL8zySiaH1KTLC07IsuJUu4M=">AAACGXicbVDLSgMxFM3UV62vqks3waK4KjNtfe0KblxWsA/oDCWTuW1DM5khyYhl6G+48VfcuFDEpa78G9MHoq0XDjmccy+59/gxZ0rb9peVWVpeWV3Lruc2Nre2d/K7ew0VJZJCnUY8ki2fKOBMQF0zzaEVSyChz6HpD67GfvMOpGKRuNXDGLyQ9ATrMkq0kTp52/Whx0Tqh0RLdj9y8DEuGZRdt2KeU4MzF0Tw09DJF+yiPSm8SJwZKaBZ1Tr5DzeIaBKC0JQTpdqOHWsvJVIzymGUcxMFMaED0oO2oYKEoLx0ctkIHxklwN1IGgiNJ+rviZSESg1D33Sa/fpq3huL/3ntRHcvvJSJONEg6PSjbsKxjvA4JhwwCVTzoSGESmZ2xbRPJKHahJkzITjzJy+SRqnolIv2TaVQvZzFkUUH6BCdIAedoyq6RjVURxQ9oCf0gl6tR+vZerPep60Zazazj/6U9fkNI7ed2w==</latexit>

<latexit sha1_base64="wpkfcu6511qhvhdRK5sII0cc6rc=">AAACDnicbVDLSsNAFJ34rPUVdelmsFRclcQHtbuCG5cV7APaUCaT23boZBJmJmIJ/QI3/oobF4q4de3Ov3HaBtHWAxcO59zLvff4MWdKO86XtbS8srq2ntvIb25t7+zae/sNFSWSQp1GPJItnyjgTEBdM82hFUsgoc+h6Q+vJn7zDqRikbjVoxi8kPQF6zFKtJG6drHjQ5+J1A+Jlux+fIGPcdlUpQMi+FG7dsEpOVPgReJmpIAy1Lr2ZyeIaBKC0JQTpdquE2svJVIzymGc7yQKYkKHpA9tQwUJQXnp9J0xLholwL1ImhIaT9XfEykJlRqFvuk09w3UvDcR//Paie5deikTcaJB0NmiXsKxjvAkGxwwCVTzkSGESmZuxXRAJKHaJJg3IbjzLy+SxmnJPSs5N+eFaiWLI4cO0RE6QS4qoyq6RjVURxQ9oCf0gl6tR+vZerPeZ61LVjZzgP7A+vgG5nibWQ==</latexit>
5 7 9

46
Common Functions in Numpy

1 2 3
a=
4 5 6
<latexit sha1_base64="oUHV7uuCKEIPOD6evmzUyUZzaEY=">AAACG3icbVDLSgMxFM34rPVVdekmWBRXZaatr4VQcOOygn1AZyiZzG0bmskMSUYsQ//Djb/ixoUirgQX/o3pA9HWC4cczrmX3Hv8mDOlbfvLWlhcWl5Zzaxl1zc2t7ZzO7t1FSWSQo1GPJJNnyjgTEBNM82hGUsgoc+h4fevRn7jDqRikbjVgxi8kHQF6zBKtJHauSK5dH3oMpH6IdGS3Q8dfISLBiXXLZvnxODUBRH8NLRzebtgjwvPE2dK8mha1Xbuww0imoQgNOVEqZZjx9pLidSMchhm3URBTGifdKFlqCAhKC8d3zbEh0YJcCeSBkLjsfp7IiWhUoPQN51mv56a9Ubif14r0Z1zL2UiTjQIOvmok3CsIzwKCgdMAtV8YAihkpldMe0RSag2cWZNCM7syfOkXiw4pYJ9U85XLqZxZNA+OkDHyEFnqIKuURXVEEUP6Am9oFfr0Xq23qz3SeuCNZ3ZQ3/K+vwGfAOejQ==</latexit>

• Find minimum value in an array:


>>> all_min = np.min(a)
>>> print(all_min)
[ 1 ]

• Along cols:
>>> col_min = np.min(a, axis=1)
>>> print(col_min)
[ 1 4 ]

• Along rows:
>>> row_min = np.min(a, axis=0)
>>> print(row_min)
[ 1 2 3 ]

For finding the maximum value, you can use np.max


47
Common Functions in Numpy
• Find index of minimum value in an array:
⇥ ⇤
b= 5 3
<latexit sha1_base64="C/BpGG/qcncGCLYqgzWi4yPRUvk=">AAACEXicbVDLSsNAFJ34rPVVdelmsChdlcQq6kIouHFZwT6gCWUyuWmHTiZhZiKW0F9w46+4caGIW3fu/BunD0RbD1w4nHMv997jJ5wpbdtf1sLi0vLKam4tv76xubVd2NltqDiVFOo05rFs+UQBZwLqmmkOrUQCiXwOTb9/NfKbdyAVi8WtHiTgRaQrWMgo0UbqFEr+petDl4nMj4iW7H54io9wxZRjuyCCH7lTKNpleww8T5wpKaIpap3CpxvENI1AaMqJUm3HTrSXEakZ5TDMu6mChNA+6ULbUEEiUF42/miID40S4DCWpoTGY/X3REYipQaRbzrNfT01643E/7x2qsNzL2MiSTUIOlkUphzrGI/iwQGTQDUfGEKoZOZWTHtEEqpNiHkTgjP78jxpHJedStm+OSlWL6Zx5NA+OkAl5KAzVEXXqIbqiKIH9IRe0Kv1aD1bb9b7pHXBms7soT+wPr4Bn36cOg==</latexit>
10
>>> b = np.array([5, 3, 10])
>>> min_ind_b = np.argmin(b)
>>> print(min_ind_b)
1

• Be careful with multi-dimensional arrays!



1 2 3
c=
4
<latexit sha1_base64="U9Ot5P/pQr1g0/Nw6iQT7rQ1jPg=">AAACJnicbVDLSgMxFM34rPU16tJNsCiuykxbfCwKBTcuK9gHdErJpHfa0ExmSDJiGfo1bvwVNy4qIu78FNMHoq0HLpyccy+59/gxZ0o7zqe1srq2vrGZ2cpu7+zu7dsHh3UVJZJCjUY8kk2fKOBMQE0zzaEZSyChz6HhD24mfuMBpGKRuNfDGNoh6QkWMEq0kTp2mZY9H3pMpH5ItGSPIxef4YKpIvY8XDLEM0bfD1JnZB4XHojuT2/Hzjl5Zwq8TNw5yaE5qh177HUjmoQgNOVEqZbrxLqdEqkZ5TDKeomCmNAB6UHLUEFCUO10euYInxqli4NImhIaT9XfEykJlRqGvumcrKwWvYn4n9dKdHDVTpmIEw2Czj4KEo51hCeZ4S6TQDUfGkKoZGZXTPtEEqpNslkTgrt48jKpF/JuMe/clXKV63kcGXSMTtA5ctElqqBbVEU1RNETekFj9GY9W6/Wu/Uxa12x5jNH6A+sr2/vKqL+</latexit>
0 6
>>> c = np.array([[1,2,3],[4,0,6]])
>>> min_ind_c = np.argmin(c)
>>> print(min_ind_c)
4

• To get 2D index, use np.unravel_index()


48
Common Functions in Numpy

1 2 3
a=
4 5 6
<latexit sha1_base64="oUHV7uuCKEIPOD6evmzUyUZzaEY=">AAACG3icbVDLSgMxFM34rPVVdekmWBRXZaatr4VQcOOygn1AZyiZzG0bmskMSUYsQ//Djb/ixoUirgQX/o3pA9HWC4cczrmX3Hv8mDOlbfvLWlhcWl5Zzaxl1zc2t7ZzO7t1FSWSQo1GPJJNnyjgTEBNM82hGUsgoc+h4fevRn7jDqRikbjVgxi8kHQF6zBKtJHauSK5dH3oMpH6IdGS3Q8dfISLBiXXLZvnxODUBRH8NLRzebtgjwvPE2dK8mha1Xbuww0imoQgNOVEqZZjx9pLidSMchhm3URBTGifdKFlqCAhKC8d3zbEh0YJcCeSBkLjsfp7IiWhUoPQN51mv56a9Ubif14r0Z1zL2UiTjQIOvmok3CsIzwKCgdMAtV8YAihkpldMe0RSag2cWZNCM7syfOkXiw4pYJ9U85XLqZxZNA+OkDHyEFnqIKuURXVEEUP6Am9oFfr0Xq23qz3SeuCNZ3ZQ3/K+vwGfAOejQ==</latexit>

• Find mean value of an array:


>>> overall_mean = np.mean(a)
>>> print(overall_mean)
3.5

• Along columns:
>>> col_mean = np.mean(a, axis=1)
>>> print(col_mean)
[2. 5.]

• Along rows (this is useful for whitening your data!):


>>> row_mean = np.mean(a, axis=0)
>>> print(row_mean)
[2.5 3.5 4.5]

You can use np.std similarly to find the standard deviation and
np.var to find the variance
49
Random

• The module np.random implements pseudo-random number


generators for various distributions.

• Almost all module functions depend on the basic function


random(), which generates a random float uniformly in the semi-
open range [0.0, 1.0).

>>> x = np.random.random()
>>> print(x)
0.22402776143655379

50
Random
• How to fix the pseudo-random generator?

>>> np.random.random()
0.4353
>>> np.random.random()
0.4204

• Use seed.

>>> np.random.seed(2)
>>> np.random.random()
0.4360
>>> np.random.seed(2)
>>> np.random.random()
0.4360

51
Random: Useful Functions
• Getting random integer n such that a <= n <= b:

>>> n = random.randint(3, 10)


>>> print(n)
7

• Getting random float x such that min <= x <= max:

>>> minim = 3
>>> maxim = 10
>>> x = (maxim - minim) * random.random() + minim
print(x)
3.1815

52
Random: Useful Stuff
• How to shuffle (in-place) randomly list of indices?

>>> x = np.arange(10)
>>> print(x)
[0 1 2 3 4 5 6 7 8 9]

>>> np.random.shuffle(x)
>>> print(x)
[9 5 4 7 1 8 0 3 2 6]

• Some useful distributions (uniform, normal):

>>> x = random.uniform(low=0, high=1, size=(2, 3))


>>> x = random.randn(2, 3)

53
References
• Python Documentation:
https://docs.python.org/3/

• Official python3 tutorial:


https://docs.python.org/3/tutorial/index.html

• All built-in functions:


https://docs.python.org/3/library/functions.html

• Jupyter Notebook documentation:


https://jupyter.readthedocs.io/en/latest/

• NumPy Basics:
https://docs.scipy.org/doc/numpy/user/basics.html

• Official NumPy tutorial:


https://docs.scipy.org/doc/numpy/user/quickstart.html

54

You might also like