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

Python CrashCourse

This document provides an overview and installation instructions for a Python crash course taught by Prof. Kevin Smet in Spring 2020. It discusses why Python is a popular programming language and its key features. It then provides step-by-step instructions for installing Python 3 and the Anaconda or Miniconda distributions on Windows. It recommends creating a virtual environment and using either the Spyder IDE or Jupyter notebooks for coding. Finally, it lists some online and book resources for learning more about Python.

Uploaded by

feriel djelil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python CrashCourse

This document provides an overview and installation instructions for a Python crash course taught by Prof. Kevin Smet in Spring 2020. It discusses why Python is a popular programming language and its key features. It then provides step-by-step instructions for installing Python 3 and the Anaconda or Miniconda distributions on Windows. It recommends creating a virtual environment and using either the Spyder IDE or Jupyter notebooks for coding. Finally, it lists some online and book resources for learning more about Python.

Uploaded by

feriel djelil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

Python Crash Course

Prof. Kevin Smet

Spring 2020
Why?
• Popular programming language created in 1991 by Guido van Rossum
• Open-source, powerful and versatile
• Lots of online community support
• Lots of packages for all sorts of things
• Easy, readible and short syntax
• Works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc)
• Supports procedural, an object-orientated and functional programming
• Runs on an interpreter
Python 3.x installation (1)
• Note that there are two versions of Python: 2.x and 3.x.
We’ll be using the latter.

• The easiest way to install all you need and more is to use the
python Anaconda distribution from Continuum Analytics. It comes
with lots of installed packages for scientific computing,
engineering and data science.

• An installer for various platforms can be downloaded from:


anaconda.org/anaconda/python.

• There is also a light-weight Miniconda version with just the basics


(conda.io/miniconda.html).
Python 3.x installation (2)
Preferred installation routine (Windows):
1. Download the light-weight Miniconda version from
conda.io/miniconda.html for python 3.x.

2. Run the installer and follow all steps. (Don’t select « add to
PATH », nor « preferred install »)

3. Type anaconda (or in some cases cmd will also work) in


windows search and select Anaconda Prompt to open up a
anaconda command window.
Python 3.x installation (3)
Preferred installation routine (Windows):

4. Create a python 3.x virtual environment named Why use a virtual


environment?
DIP filled with a full Anaconda distribution:
• Easy version control
• If you mess something up,
delete the environment and
start anew.

5. Activate the DIP environment:


Python 3.x installation (4)
Spyder IDE:

6a. Start the Spyder IDE:

Jupyter Notebook:

6b. Or start a jupyter notebook session:


Spyder IDE run code in editor set working directory

code editor

console
Spyder IDE

Console code execution


Spyder IDE

Editor code execution


Jupyter notebook This will open up in a brower window/tab

Click on new and select


« Python 3 »
to open up a new
jupyter notebook

Change filename here

jupyter notebook cell


Jupyter notebook Code execution from a jupyter notebook
« code cell »

A new « code cell » is automatically


created upon execution of the
previous cell

Use « markdown cell » to add text (e.g.


code explanation) to the jupyter
notebook
Python resources
Online

• https://python.org/
(documentation, tutorials, beginners guide, core distribution, ...)
• https://docs.python.org/3/tutorial/
• https://wiki.python.org/moin/BeginnersGuide/Programmers

• http://scipy.github.io/old-wiki/pages/NumPy_for_Matlab_Users.html
• http://cs231n.github.io/python-numpy-tutorial/

• See also the many video tutorials on youtube.com and udemy.com


Python resources
Books

• Learning Python by Mark Lutz (can easily be found online as a pdf)

• Python Essential Reference by David Beazley

• Python Cookbook, ed. by Martelli, Ravenscroft and Ascher


• (online at http://code.activestate.com/recipes/langs/python/)

• http://wiki.python.org/moin/PythonBooks
docs.python.org/3/library/stdtypes.html

Python basics: built-in datatypes


Numeric types
• Integers (‘int’)
• base 10 (e.g 10)
• binary (e.g. 0b10)
• Octal (e.g. 0o10)
• Hexadecimal (e.g. 0x10)
• Floating point (‘float’)
• 64 bit, IEEE 754 standard
• Max is approximately 1.8 ⨉ 10308 (larger values -> inf)
• Closest to zero is approximately 5.0 ⨉ 10-324 (smaller -> 0)
• E.g. 3.14
• Complex numbers (‘complex’)
• <real part>+<imaginary part>j
• E.g. 2+3j
docs.python.org/3/library/stdtypes.html

Python basics: built-in datatypes


Sequences
• Strings (‘str’) (immutable*)
• = sequence of Unicode characters
• String literals may be delimited using either single (‘) or double quotes (“) and even
triple quotes (“””)
• Escape characters: use backslash, e.g. ‘\n’ for new line
• E.g. “Hello World!”
• ‘bytes’ (immutable*)
• a sequence of integers in the range of 0-255
• ‘byte array’: like ‘bytes’, but mutable*

*(im)mutable: content of object can (not) be changed after creation


docs.python.org/3/library/stdtypes.html

Python basics: built-in datatypes


Sequences
• list
• A mutable collection of items Variables in Python are
• E.g. [], [0], [‘a’, ’b’, ’c’] or [‘a’, 1, ‘b’, 2.0, ’abcd’, [‘1’,2]] really just references to
objects in memory.
• tuple
• An immutable collection of items Therefore take note of the
• E.g. (), (0,), (a’, ’b’, ’c’) or (‘a’, 1, ‘b’, 2.0, ’abcd’, (‘1’,2)) following!!:
>>> a = [1,2,3]
Boolean (‘bool’) >>> b = a
• True, False >>> b[0] = ‘abc’
>>> print(a)
Null object ['abc', 2, 3]
• None (actual object, not a null pointer or null reference!)
docs.python.org/3/library/stdtypes.html

Python basics: built-in datatypes


Mappings
• dict (mutable)
• A Python dictionary, also called hashmap or associative array,
• E.g. {‘a’: 1, ‘b’: 2.0, ’c’: ’d’}, dict(a = 1, b = 2.0, c = ‘d’)
• In {x:y}, x is the key and y is the value (no duplicate keys allowed, key must be immutable)

Sets
• set:
• an unordered collection of unique objects
• E.g. set(‘abc’), {‘a’, ’b’, ’c’}
• frozen set:
• like set, but immutable
• E.g. frozenset(‘abc’)
Python basics: indexing
Indexing:
>>> list1 = [‘a’, ‘b’, ‘c’]
>>> list1[0] [0] is the first item.
‘a’
>>> list1[1] [1] is the second item
‘b’
>>> list1[3] Out of range values
IndexError: list index out of range raise an exception
>>> list1[-1]
Negative values
‘c’
go backwards from
>>> list1[-2] the last element.
‘b’
Indexing applies also to tuples & strings
Python basics: slicing
Slicing:
>>> list1[:0] Slice from first to first element (non-inclusive)
[]

>>> list1[:1] Slice from first to second element (non-inclusive).


[‘a’] (note that this is a list, not just the 1st element
as in list1[0] !!!)
>>> list1[:2]
[‘a’, ‘b’] Slice from first to third element (non-inclusive)

>>> list1[-2:] Slice from second last to last element


[‘b’, ‘c’]

>>> list1[-1:] Slice from last to last element


[‘b’]

>>> list1[0:2] Slice from first to third (non-inclusive) element


[‘a’, ‘b’]
Same for tuples & strings
docs.python.org/3/tutorial/datastructures.html

Python basics: useful list functions


>>> list1. append('d') Append element to the end of the list1
['a', 'b', 'c', 'd']

>>> list1.extend(list1[:2]) Extend list1 with all elements in second list


['a', 'b', 'c', 'd', 'a', 'b']

>>> list1.insert(0,’e’) Insert element ‘e’ at position 0


['e', 'a', 'b', 'c', 'd', 'a', 'b']

>>> list1.pop(1) Returns and removes second element from list1, which now has the
‘a’ following elements: ['e', 'b', 'c', 'd', 'a', 'b']; (to just remove: del list1[1]).
>>> list1.reverse()
['b', 'a', 'd', 'c', 'b', 'e'] Reverse elements in list1

>>> list1.sort() Sort elements in list1


['a', 'b', 'b', 'c', 'd', 'e']
docs.python.org/3/tutorial/datastructures.html

Python basics: useful dictionary functions


>>> dict1 = {'a': 1, 'b': 2, 'c': 3} Append element to the end of the list1
>>> dict1[‘b’] Get value for a given key
2

>>> 'a' in dict1, 'd' in dict1 Test if dict1 has keys ‘a’ and ‘d’. Note that specification on a single line
(True, False) returns answer as tuple.

>>> dict1['d'] Non-existent key raises KeyError.


KeyError: 'd'

>>> dict1.get('d', 'unknown') Avoid KeyError and output some default value (e.g. ‘unknown’) by
‘unknown’ using the get() method.

>>> dict1.get('a', 'unknown')


1
docs.python.org/3/tutorial/datastructures.html

Python basics: useful dictionary functions


>>> dict1.keys() Get an iterator of all keys
dict_keys(['a', 'b', 'c'])
>>> list(dict1.keys()) Get a list of all keys
['a', 'b', 'c']

>>> list(dict1.values()) Get a list of all values.


[1, 2, 3]

>>> dict1.update({'d':4, 'e':5}) Update dict1 with new keys and values.
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

>>> dict1.items Get an iterator with items, i.e. (key,value)-pairs, in dict1.


dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])

>>> del dict1['a'] Delete (key,value) pair with key == ‘a’


{'b': 2, 'c': 3, 'd': 4, 'e': 5}
Python basics: control flow
Conditional branching with: if, elif and else
• if statement: Executes a group of statements only if a certain condition is
true. Otherwise, the statements are skipped.
• Syntax:
if condition:
statements
• Example:
score = 12.0
if score > 10.0:
print("Your have passed!”)

Note the indentation used to group statements.


Python basics: control flow
Conditional branching with: if, elif and else
• if/else statement: Executes one block of statements if a certain condition is
True, and a second block of statements if it is False.
• Syntax:
if condition:
statements
else:
statements
• Example:
score = 12.0
if score > 10.0:
print("Your have passed!”)
else:
print("Your failed!”)
Python basics: control flow
Conditional branching with: if, elif and else
• Multiple conditions can be chained with elif ("else if"):
• Syntax:
if condition:
statements
elif condition:
statements
else:
statements
Python basics: control flow
Relational operators:

Logical operators:
Python basics: control flow
For loop
• Repeats a set of statements over a group of values.
• Syntax:

for nameOfVariable in groupOfValues:


statements

• Example:
for a in range(1, 5): Output:
print(a**2) 1 4 9 16

Note the indentation used to group statements.


Python basics: control flow
While loop
• Executes a group of statements as long as a condition is True.
• good for indefinite loops (repeat an unknown number of times)
• Syntax:
while condition:
statements
• Example:
a = 2
while a < 1000:
print(a) Output:
a **= 2 2 4 16 256

Note the indentation used to group statements.


Python basics: control flow
break / continue / pass
• break: terminates the loop containing it. Control of the program flows
to the statement immediately after the body of the loop. If break
statement is inside a nested loop (loop inside another loop), break will
terminate the innermost loop.

• continue: skips the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next
iteration.

• pass: is a null statement. Nothing happens when pass is executed,


but is required in loop (or functions) that are not implemented yet, as
they cannot have an empty body.
Python basics: functions
• Group of statements that perform a specific task.
• Makes code more manageable and organized.
• Avoids repetitions and makes code reusable.

Mark beginning (def) and end (:)


• Syntax: of function header.

def function_name(arguments): List of arguments to pass


"""docstring""" values to the function.
statement(s) Optional documentation string
about what the function does.

One or more statements that make up function body.


Optional return statement to returna function value.
Python basics: functions
• No input arguments:
Note that you need to supply all two
def print_hello_world(): arguments when calling the function to avoid
print("Hello World!") raising an error:
>>> print_msg_student1('Sarah')
TypeError: print_msg_student1() missing 1
• Two positional input arguments: required positional argument: ‘msg’
def print_msg_student1(name, msg):
print(msg + " " + name + "!")
This time no error is raised:
• One positional argument and one keyword argument: >>> print_msg_student2('Sarah')
Hello Sarah!
def print_msg_student2(name, msg = 'Hello'):
print(msg + " " + name + "!") Note that keyword arguments
must always follow positional
arguments!
Python basics: functions
• Two keyword arguments:
def print_msg_student3(name = 'Sarah', msg = 'Hello'):
print(msg + " " + name + "!")

Note both keywords have default values that do not need to be specified when calling the function:
>>> print_msg_student3()
Hello Sarah!

Note that the order of the arguments does not matter. They are called by name:
>>> print_msg_student3(msg = 'Goodbye', name = 'Elly')
Goodbye Elly!
Python basics: functions
• Unknown number of positional arguments and one keyword argument:
def print_msg_student4(*names, msg = 'Hello'):
for name in names:
print(msg + " " + name + "!")

*names can be any amount of names


>>> print_msg_student4('Sarah','Elly','Maartje')
Hello Sarah!
Hello Elly!
Hello Maartje!

>>> print_msg_student4('Sarah','Elly','Maartje', msg = 'Goodbye')


Goodbye Sarah!
Goodbye Elly!
Goodbye Maartje!
Python basics: functions
• Return statement:

def get_even_numbers(numbers):
even = []
for number in numbers:
if number % 2 == 0:
even.append(number)
return even

Output of function is stored in variable even:


>>> even = get_even_numbers([0,1,2,3,4,5])
>>> print(even)
[0,2,4]
Python basics: functions
• Return statement:
def get_even_and_odd_numbers(numbers):
even = []
odd = []
for number in numbers:
if number % 2 == 0:
even.append(number)
else:
odd.append(number)
return even, odd # tuple packing

Output is stored in tuple even_odd:


>>> even_odd = get_even_and_odd_numbers([0,1,2,3,4,5])
>>> print(even_odd)
([0, 2, 4], [1, 3, 5])
Python basics: functions
• Return statement and tuple unpacking:

Tuple can be unpacked:


>>> even1, odd1 = even_odd # tuple unpacking
>>> print(even1)
[0,2,4]
>>> print(odd1)
[1,3,5]

Output is stored in directly unpacked in variables even, odd:


>>> even2, odd2 = get_even_and_odd_numbers([0,1,2,3,4,5]) # function call + tuple unpacking
>>> print(even2)
[0,2,4]
>>> print(odd2)
[1,3,5]
Python basics: functions
• Recursion:
def factorial(n):
if n = 1:
return 1
else:
return n * factorial(n-1)

Output of function is stored in variable fac:


>>> fac = factorial(10)
>>> print(fac)
3628800
Python basics: functions
• lambda function:
• Lambda functions or anonymous functions are functions that are defined without a name
using the keyword lambda.

• Syntax:

lambda arguments: expression

• Example:
>>> pow = lambda x,n: x**n
>>> pow(3,2)
9
Python basics: functions
• Scope:
Global variables are variables declared outside of the function or in the global scope. They can
be accessed inside or outside of the function.

Example:
>>> x = 10
>>> def foo():
print(2*x)
>>> foo()
20
>>> print(3*x)
Note that calling foo() does not have any effect on the
30
global x.
Python basics: functions
• Scope:
Global variables can be changed from inside a function by using the global keyword.

Example:

>>> x = 10
>>> def bar():
global x
x = x*2
print(x)
>>> print(x)
10
>>> bar()
20
>>> print(x)
20 Note that calling bar() does have an effect on the global x.
Python basics: functions
• Scope:
Local variables are variables declared inside the function’s body or in the local scope. They
cannot be accessed from outside of the function (or local scope).

Example:
>>> def foobar():
a = 2
>>> foobar()
>>> print(a)
NameError: name 'a' is not defined

>>> a = 3
>>> foobar()
>>> print(a)
3 Note that calling foobar() did not have any effect on the global a.
Python basics: classes
• Syntax:
class name:
"documentation"
statements
-or-
class name(base1, base2, ...): Class definition with inheritance
... from classes base1, base2, …

• Most, statements are method definitions:


def name(self, arg1, arg2, ...):
...
• But, may also be class variable assignments
Python basics: classes
class ComplexNumber: Class definition
def __init__(self,r = 0,i = 0): __init__: Constructor. Gets executed
self.real = r upon creation of class instance.
self.imag = i
def getData(self): getData: Class method
print("{0}+{1}j".format(self.real,self.imag))

>>> c1 = ComplexNumber(2,3) Create a new ComplexNumber object


>>> c1.getData() Call getData method
2 + 3j

>>> c2 = ComplexNumber(5) Create another ComplexNumber object


>>> c2.attr = 10 and create a new attribute 'attr'
>>> print((c2.real, c2.imag, c2.attr))
(5, 0, 10)
>>> c1.attr c1 doesn’t have attribute 'attr'
AttributeError: 'ComplexNumber' object has no attribute 'attr'
Python basics: modules
• At startup, only a limited number of functions are available (built-in
functions): e.g. print, len, range, dict, …

• Modules refer to a file (.py) containing Python statements and


definitions (e.g. function, variable and class definitions) that extend the
functionality of python.

• Modules are used to break down large programs into small manageable
and organized files; and allow for code reusability.

• They can be imported using import

• Each module creates it own isolated global namespace (a mapping of


all defined names to their corresponding objects). Functions/classes
belonging to a module create local namespaces.
Python basics: modules
Example:
A simple module can be created by saving the following code in a file called example.py:

def add(a,b):
""" Add a and b. """
return a + b

def subtract(a,b):
""" Subtract a and b. """
return a – b

The module can be loaded as follows:


Note that you have to add the name of the module in
>>> import example front of the function/method.
>>> example.add(3,4.0)
7.0
Python basics: modules
The module can be loaded under a different name as follows:

>>> import example as basicmath Note that you have to add the new name of the
>>> basicmath.add(3,4.0) module in front of the function/method.
7.0

To load only specific definitions into the global namespace:


>>> from example import add
>>> add(3,4.0)
7.0
>>> subtract(3,4.0)
name 'subtract' is not defined Note that while add() is now defined, subtract() is not.

To load all (exported) definitions into the global namespace:

>>> from example import *


Python basics: modules
Reload a module:
• Modules are only imported once.
• Suppose the my_module contains the following code:
print("This code got executed!")

• Multiple import statements will have the following result:


>>> import my_module
This code got executed!
>>> import my_module
>>> import my_module
• When the module is changed, you will need to reload the module.
>>> import imp
>>> imp.reload(my_module)
This code got executed!
<module 'my_module' from '.\\my_module.py'>
Python basics: packages

• A package is a collection of modules in directory


• It must have an __init__.py file
• Packages may contain subpackages
• Import syntax is as follows:

>>> from A.B.C import foo; print(foo())


>>> from A.B import C; print(C.foo())
>>> import A.B.C; print(A.B.C.foo())
>>> import A.B.C as C; print(C.foo())
Python basics: file I/O

• Useful methods:
• open(filename, mode = ‘r’): open a file. Mode specifies what we want to do with
the file. E.g. ‘r’ (read), ‘w’ (write) or ‘a’ (append).
• read(n): read at most n characters. Read until end of file if n is negative or None.
• readline(n=-1): read and return one line from file. Return at most n bytes if specified.
• readlines(n=-1): read and return a list of lines from file. Return at most n
bytes/characters if specified.
• seek(offset, from = SEEK_SET): change position in file to offset bytes, in
reference to from.
• tell(): returns current file position.
• write(s): write string s to file and return number of characters written.
• writelines(lines): write a list of lines to the file.
Python basics: file I/O
Read lines one by one from input file and write them to an output file:

f_in = open(“in_file.txt")
f_out = open(“out_file.txt", "w")
for line in f_in:
f_out.write(line)

f_in.close() 'r' : open for reading (default)


'w': open for writing, truncating the file first
f_out.close() 'x' : create a new file and open it for writing
'a‘: open for writing, appending to the end of the file if it exists
A file needs to be properly closed. This will 'b‘: binary mode
free up the resources that were tied with the 't' : text mode (default)
file and is done using the close() method. '+‘: open a disk file for updating (reading and writing)
Python basics: file I/O
• The best way to work with files is using the with statement. The file doesn’t have to be
explicitly closed and will be closed internally, even when an error occurs.
with open(“file.txt") as f:
# perform file operations here

• The methods mentioned above are built-in. However, other specialized methods for
reading / writing images, arrays, dataframes, etc. from / to various file formats (e.g. csv,
txt, xlsx, …) exist as part of a variety of modules (pandas, numpy, scikit-image, …):

• Pandas: read_csv(), DataFrame.to_csv(),


• Numpy: loadtxt(), savetxt(), …

• Input from the consoles using input():

var = input("Input something: ")


Python basics: Numpy (NUMerical PYthon)
• Fundamental package for scientific computing with Python.
• Together with Scipy and Matplotlib, it provides a MATLAB-like functionality
in python.
• Numpy features:
• Typed multidimentional arrays (matrices): ndarray
• Fast numerical computations (matrix math)
• E.g.: triple loop multiplication of 1000x1000 matrix
• Python takes 10min, Numpy takes 0.03secs! www.numpy.org
• High-level math functions
• E.g.: linear algebra, Fourier transform,
and random number capabilities
• Tools for integrating C/C++ and Fortran code
Python basics: Numpy (NUMerical PYthon)
Overview

1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
5. Broadcasting
Python basics: Numpy (NUMerical PYthon)
Arrays:

Structured lists of numbers.

• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays:

Structured lists of numbers. 𝑝𝑥


𝑝𝑦
• Vectors
• Matrices 𝑝𝑧
• Images
• Tensors
𝑎11 ⋯ 𝑎1𝑛
• ConvNets ⋮ ⋱ ⋮
𝑎𝑚1 ⋯ 𝑎𝑚𝑛
Python basics: Numpy (NUMerical PYthon)
Arrays:

Structured lists of numbers.

• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays:

Structured lists of numbers.

• Vectors
• Matrices
• Images
• Tensors
• ConvNets
Python basics: Numpy (NUMerical PYthon)
Arrays: basic properties
A NumPy array is a homogeneous collection of “items” of the same “data-type” (dtype)

>>> import numpy as np


>>> a = np.array([[1,2,3],[4,5,6]],dtype=np.float32)
>>> print(a.ndim, a.shape, a.dtype)

1. Arrays can have any number of dimensions, including zero (a scalar).


2. Arrays are (homogenously) typed: np.uint8, np.int64, np.float32, np.float64
3. Arrays are dense. Each element of the array exists and has the same type.
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

>>> np.ones((2,5))
• np.ones, np.zeros array([[1., 1., 1., 1., 1.],
• np.arange [1., 1., 1., 1., 1.]])
• np.concatenate
• np.astype >>> np.zeros((3,5))
array([[0., 0., 0., 0., 0.],
• np.zeros_like,
[0., 0., 0., 0., 0.],
np.ones_like [0., 0., 0., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

>>> np.arange(5)
• np.ones, np.zeros array([0, 1, 2, 3, 4])
• np.arange >>> np.arrange(2,5)
• np.concatenate array([2, 3, 4])
• np.astype >>> np.arange(2,10, 2)
array([2, 4, 6, 8])
• np.zeros_like,
np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

>>> A = np.ones((2,4))
• np.ones, np.zeros >>> B = np.zeros((3,4))
• np.arange >>> C = np.concatenate([A,B])
• np.concatenate array([[1., 1., 1., 1.],
• np.astype [1., 1., 1., 1.],
[0., 0., 0., 0.],
• np.zeros_like, [0., 0., 0., 0.],
np.ones_like [0., 0., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

• np.ones, np.zeros
>>> A = np.ones((4,1))
• np.arange
>>> B = np.zeros((4,2))
• np.concatenate >>> C = np.concatenate([A,B], axis = 1)
• np.astype array([[1., 0., 0.],
• np.zeros_like, [1., 0., 0.],
[1., 0., 0.],
np.ones_like [1., 0., 0.]])
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: creation
>>> A = np.array([[1.2, 1.0, 1.5],
[-1.2, 1.0, 260.6]])
• np.ones, np.zeros >>> B = A.astype(np.int8)
• np.arange >>> B
array([[ 1, 1, 1],
• np.concatenate [-1, 1, 4]], dtype = int8)
• np.astype
• np.zeros_like, Note that np.int8 [-128, 127] repeats:
E.g. try
np.ones_like >>> np.arange(0,261, 1).astype(np.int8)
• np.random.random array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

121, 122, 123, 124, 125, 126, 127, -128, -127, -126,
… = 127, 128
-3, -2, -1, 0, 1, 2, 3, 4], dtype=int8)
= 255,256,257 = 260
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

>>> type(A)
• np.ones, np.zeros numpy.ndarray
• np.arange >>> A.dtype
• np.concatenate dtype(‘float64')
• np.astype >>> B.dtype
dtype('int8')
• np.zeros_like,
np.ones_like type gives type of object
• np.random.random dtype gives type of elements of ndarray
Python basics: Numpy (NUMerical PYthon)
Arrays: creation >>> A = np.ones((2,3))
>>> B = np.zeros_like(A)
>>> B.shape
• np.ones, np.zeros
(2,3)
• np.arange >>> B
• np.concatenate array([[0., 0., 0.],
• np.astype [0., 0., 0.]])
>>> B.size
• np.zeros_like, 6
np.ones_like >>> B.ndim
• np.random.random 2

B.shape or np.shape(B) returns the shape of the array B


B.size or np.size(B) returns the number of elements in array B
B.ndim or np.ndim(B)returns the number of dimensions of array B
Python basics: Numpy (NUMerical PYthon)
Arrays: creation

• np.ones, np.zeros
• np.arange >>> np.random.random((4,3))
• np.concatenate array([[0.27466025, 0.61004034, 0.94851734],
• np.astype [0.96894833, 0.00439395, 0.15156152],
[0.70234794, 0.76490032, 0.84585757],
• np.zeros_like,
[0.20728297, 0.50805883, 0.45459315]])
np.ones_like
• np.random.random
Python basics: Numpy (NUMerical PYthon)
Arrays: (re-)shaping

>>> a = np.arange(6)
>>> a = a.reshape(3,2)
>>> a = a.reshape(2,-1)
>>> a = a.ravel()

1. Total number of elements cannot change.


2. Use -1 to infer axis shape
3. Row-major by default (MATLAB is column-major)
Python basics: Numpy (NUMerical PYthon)
Transposition

>>> a = np.arange(10).reshape(5,2)
>>> a = a.transpose((1,0))
>>> a.shape
(2,5)
>>> a = a.T
>>> a.shape
(5,2)

1. np.transpose permutes axes.


2. a.T transposes the first two axes.
Python basics: Numpy (NUMerical PYthon)
Return values: copies of views
• Numpy functions return either views or
copies (The numpy documentation says
which functions return views or copies).
>>> a = np.array([0,1,2,3,4])
• np.copy, np.view make explicit copies
>>> b = a #b refers to same object as a
and views.
>>> c = a.copy() # c refers to new object
• Views share data with the original array, >>> c[0:1] = 10.6
like references in Java/C++. Altering >>> print(a)
entries of a view, changes the same
array([0, 1, 2, 3, 4])
entries in the original.
>>> b[0:1] = 10.6
>>> print(a)
array([10, 1, 2, 3, 4])
Beware of type conversion! >>> a.dtype
e.g. when assigning a float (10.6) to a int32 array. dtype('int32')
Python basics: Numpy (NUMerical PYthon)
Return values: copies of views
• Slicing (works much like in standard
python) returns views
>>> a = np.array([0,1,2,3,4])
• Fancy indexing (using position indexing or >>> b = a[0:2] # slicing -> view
Boolean masks) returns copies
>>> c = a[[0,1]]# fancy indexing -> copy
(if on right-hand side of assignment, ‘=‘) >>> c[0:2] = 100
>>> print(a)
array([0, 1, 2, 3, 4])
>>> b[0:2] = 100
>>> print(a)
array([100, 100, 2, 3, 4])
>>> b[[0,1]] = 1000
>>> print(a)
array([1000, 1000, 2, 3, 4])
Python basics: Numpy (NUMerical PYthon)
Array indexing and slicing

>>> x[0,0] # top-left element


>>> x[0,-1] # first row, last column
>>> x[0,:] # first row (all entries)
>>> x[:,0] # first column (all entries)

column
row

1. Much like in python.


2. Zero-indexing.
3. Multi-dimensional indices are comma-separated (i.e. a tuple).
Python basics: Numpy (NUMerical PYthon)
>>> a = np.array([[0,1,2,3,4,5]]).T
Array slicing + 10*np.array([[0,1,2,3,4,5]])

>>> a[0,3:5]
array([3, 4])
0 1 2 3 4 5
10 11 12 13 14 15 >>> a[:,2]
array([2,12,22,32,42,52])
20 21 22 23 24 25
30 31 32 33 34 35 >>> a[4:,4:]
array([[44, 45],
40 41 42 43 44 45 [54, 55]])
50 51 52 53 54 55
>>> a[2::2,::2] #with strides of 2
array([[20, 22, 24],
[40, 42, 44]])
Python basics: Numpy (NUMerical PYthon)
>>> a = np.array([[0,1,2,3,4,5]]).T
Fancy array indexing + 10*np.array([[0,1,2,3,4,5]])
using lists/tuples and boolean masks
>>> a[(0,1,2,3,4),(1,2,3,4,5)]
array([ 1, 12, 23, 34, 45])
0 1 2 3 4 5
10 11 12 13 14 15 >>> a[3:,[0, 2, 5]]
array([[30, 32, 35],
20 21 22 23 24 25 [40, 42, 45]])
30 31 32 33 34 35 [50, 52, 55]])

40 41 42 43 44 45 >>> mask = array([1,0,1,0,0,1],


50 51 52 53 54 55 dtype=bool)
>>> a[mask,2]
array([2,22,52])
Python basics: Numpy (NUMerical PYthon)
Array broadcasting:

>>> a = a + 1 # add one to every element of array

When operating on multiple arrays, broadcasting rules are used to convert them to the same
shape.

Each dimension must match, from right-to-left


1. Dimensions of size 1 will broadcast (as if the value was repeated).
2. Otherwise, the dimension must have the same shape.
3. Extra dimensions of size 1 are added to the left as needed.
Python basics: Numpy (NUMerical PYthon)
Array broadcasting: 4x3 4x3
0 1 2 0 0 0 0 1 2 0 0 0
0 1 2 10 10 10 0 1 2 10 10 10
+ = + =
0 1 2 20 20 20 0 1 2 20 20 20
0 1 2 30 30 30 0 1 2 30 30 30

4x3 3
0 0 0 0 1 2 0 0 0 0 1 2
0 1 2
10 10 10 10 10 10 0 1 2
+ = + = 10 11 12
20 20 20 20 20 20 0 1 2
20 21 22
30 30 30 30 30 30 0 1 2
stretch 30 31 32

4x1 3
0 0 1 2 0 0 0 0 1 2
10 10 10 10 0 1 2
+ = + =
20 20 20 20 0 1 2
30 30 30 30 0 1 2

stretch stretch
Python basics: Numpy (NUMerical PYthon)
Array broadcasting:
The trailing axes of both arrays must either be 1 or have the same size for broadcasting to occur.
Otherwise, a “ValueError: operands could not be broadcast together
with shapes (4,3) (4,)” exception is thrown.

mismatch!
4x3 4

0 0 0 0 1 2 3
10 10 10
+ =
20 20 20
30 30 30
Python basics: Numpy (NUMerical PYthon)
Array broadcasting:
>>> a = np.array((0,10,20,30))
>>> a.shape 0 0 1 2 0 1 2
(4,)
10 10 11 12
>>> b = np.array((0,1,2)) + =
20
>>> b.shape 20 21 22

(3,) 30 30 31 32
>>> y = a + b
ValueError: operands could not be
broadcast together with shapes (4,) (2,)
>>> y = a[:, None] + b

Create extra dimension


>>> a[:,None].shape (4,1) is broadcastable with (3,): extra dimension of size 1
(4,1) is added to the left of b (see rule 3 above).
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
Comparison and logical operators:
• Arithmetic operations are element-wise equal (==)
greater_equal(>=)
• Logical operator return a bool array not_equal (!=)
• In place operations modify the array less (<)
greater (>)
Basic binary operators: less_equal (<=)
a + b → add(a,b)
a - b → subtract(a,b) logical_and
a * b → multiply(a,b) logical_not
a / b → divide(a,b) logical_or
a // b → floor(divide(a,b)) logical_xor
a ** b → power(a,b)
a % b → remainder(a,b)
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array

>>> a = np.array([1,2,3])
>>> b = np.array([10,100,1000])
>>> a * b
array([ 10, 200, 3000])
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
• Arithmetic operations are element-wise
• Logical operator return a bool array
• In place operations modify the array
>>> a = np.array([[0.28659319, 0.74356839, 0.06362368],
[0.77662682, 0.82634601, 0.33838486]])
>>> b = a < 0.5
>>> b
array([[False, True, False],
[ True, True, False]])
>>> b.dtype
dtype('bool')
Python basics: Numpy (NUMerical PYthon)
Mathematical operators:
• Arithmetic operations are element-wise >>> a = np.arange(4).reshape(2,-1)
• Logical operator return a bool array >>> a
array([[0, 1],
• In place operations modify the array [2, 3]])
>>> b = a + 1
>>> b
array([[1, 2],
[3, 4]])
>>> a *= b
>>> a
array([[ 0, 2],
[ 6, 12]])
Python basics: Numpy (NUMerical PYthon)
Math, upcasting:
Just as in Python and Java, the result of >>> a = np.array([0,1,3],dtype=np.uint64)
a math operator is cast to the more >>> b = np.array([0,1,3],dtype=np.uint16)
general or precise datatype.
>>> c = a + b
• uint64 + uint16 => uint64 >>> c.dtype
dtype(‘uint64')
• float32 / int32 => float32
• float64 * int8 => float64 >>> a = np.array([0,1,3],dtype=np.float32)
• … >>> b = np.array([0,1,3],dtype=np.int32)
>>> c = a + b
Warning: upcasting does not prevent >>> c.dtype
overflow/underflow. You must dtype('float32')
manually cast first.
Python basics: Numpy (NUMerical PYthon)
Math, universal functions:
• Also called ufuncs >>> a = np.array([0,1,3],dtype=np.float64)
• Element-wise >>> b = np.sqrt(a)
>>> b
• Examples: array([0. , 1. , 1.73205081])
❑np.exp
❑np.sqrt
❑np.sin
❑np.cos
❑np.isnan
Python basics: Numpy (NUMerical PYthon)
Arrays axes:

>>> a.sum() # sum all entries


>>> a.sum(axis=0) # sum over rows
>>> a.sum(axis=1) # sum over columns
>>> a.sum(axis=1, keepdims=True) # sum over columns, keep ndim

1. Use the axis parameter to control which axis numpy operates on


2. Typically, the axis specified will disappear, keepdims keeps all dimensions
Python basics: Numpy (NUMerical PYthon)
Saving and loading arrays:

>>> np.savez('data.npz', a = a)
>>> data = np.load('data.npz')
>>> a = data['a']

1. NPZ files can hold multiple arrays.


2. np.savez_compressed similar.
Python basics: Numpy (NUMerical PYthon)
Basic array attributes:
• a.dtype – Numerical type of array elements. float32, uint8, etc.
• a.shape – Shape of the array. (m,n,o,...)
• a.size – Number of elements in entire array.
• a.itemsize – Number of bytes used by a single element in the array.
• a.nbytes – Number of bytes used by entire array (data only).
• a.ndim – Number of dimensions in the array.

Shape operations:
• a.flat() – An iterator to step through array as if it is 1D.
• a.flatten() – Returns a 1D copy of a multi-dimensional array.
• a.ravel() – Same as flatten(), but returns a ‘view’ if possible.
• a.resize(new_size) – Change the size/shape of an array in-place.
• a.swapaxes(axis1, axis2) – Swap the order of two axes in an array.
a.transpose(*axes) – Swap the order of any number of array axes.
• a.T – Shorthand for a.transpose()
• a.squeeze() – Remove any length=1 dimensions from an array.
Python basics: Numpy (NUMerical PYthon)
Fill and copy:
• a.copy() – Return a copy of the array.
• a.fill(value) – Fill array with a scalar value.

Conversion / coersion:
• a.tolist() – Convert array into nested lists of values.
• a.tostring() – raw copy of array memory into a python string.
• a.astype(dtype) – Return array coerced to given dtype.

Complex numbers:
• a.real – Return the real part of the array.
• a.imag – Return the imaginary part of the array.
• a.conjugate() – Return the complex conjugate of the array.
• a.conj() – Return the complex conjugate of an array.(same as conjugate)
Python basics: Numpy (NUMerical PYthon)
Saving:
• a.dump(file) – Store a binary array data out to the given file.
• a.dumps() – returns the binary pickle of the array as a string.
• a.tofile(fid, sep="", format="%s") Formatted ascii output to file.

Search / sort:
• a.nonzero() – Return indices for all non-zero elements in a.
• a.sort(axis=-1) – Inplace sort of array elements along axis.
• a.argsort(axis=-1) – Return indices for element sort order along axis.
• a.searchsorted(b) – Return index where elements from b would go in a.

Elementary math operations:


• a.clip(low, high) – Limit values in array to the specified range.
• a.round(decimals=0) – Round to the specified number of digits.
• a.cumsum(axis=None) – Cumulative sum of elements along axis.
• a.cumprod(axis=None) – Cumulative product of elements along axis.
Python basics: Numpy (NUMerical PYthon)
Reduction methods:
All the following methods “reduce” the size of the array by 1 dimension by carrying out an operation
along the specified axis. If axis is None, the operation is carried out across the entire array.

• a.sum(axis=None) – Sum up values along axis.


• a.prod(axis=None) – Find the product of all values along axis.
• a.min(axis=None)– Find the minimum value along axis.
• a.max(axis=None) – Find the maximum value along axis.
• a.argmin(axis=None) – Find the index of the minimum value along axis.
• a.argmax(axis=None) – Find the index of the maximum value along axis.
• a.ptp(axis=None) – Calculate a.max(axis) – a.min(axis)
• a.mean(axis=None) – Find the mean (average) value along axis.
• a.std(axis=None) – Find the standard deviation along axis.
• a.var(axis=None) – Find the variance along axis.

• a.any(axis=None) – True if any value along axis is non-zero. (or)


• a.all(axis=None) – True if all values along axis are non-zero. (and)
Python basics: Numpy (NUMerical PYthon)
Trigonometric functions: Other math functions:
• sin(x) sinh(x) • exp(x)
• cos(x) cosh(x) • log(x)
• arccos(x) arccosh(x) • log10(x)
• arctan(x) arctanh(x)
• arcsin(x) arcsinh(x) • sqrt(x)
• arctan2(x,y) • absolute(x)
• conjugate(x)
• negative(x)
• ceil(x)
• floor(x)
• fabs(x)
• hypot(x,y)
• fmod(x,y)
• maximum(x,y)
• minimum(x,y)
Python basics: Matplotlib (plotting)
• Requires NumPy extension. Provides powerful plotting commands.
• http://matplotlib.sourceforge.net
• Matplotlib has a large community, tons of plot types, and is well integrated into
ipython. It is the de-facto standard for ‘command line’ plotting from ipython.
• Import with:
>>> import matplotlib.pyplot as plt
Python basics: Matplotlib (plotting)
• Line plots
Plot against indices:
>>> x = np.arange(50)*2*pi/50
>>> y = np.sin(x)
>>> plt.plot(y)
>>> plt.xlabel(‘index’)

Multiple lines:
>>> plt.plot(x,y,x2,y2)
>>> plt.xlabel(‘radians’)
Python basics: Matplotlib (plotting)
• Line plots
Line formatting:
# red, dot-dash, triangles
>>> plt.plot(x,np.sin(x),'r-^')

Line formatting for multiple lines:


>>> plt.plot(x,y1,'b-o', x,y2, 'r-^')

# set axis limits


>>> plt.axis([0,7,-2,2])
Python basics: Matplotlib (plotting)
• Scatter plots
Simple scatter plot:
>>> x = np.arange(50)*2*pi/50
>>> y = np.sin(x)
>>> plt.scatter(x,y)

Colormapped scatter plot:


>>> x = np.random.rand(200)
>>> y = np.random.rand(200)
>>> size = np.random.rand(200)*30
>>> color = np.random.rand(200)
>>> plt.scatter(x, y, size, color)
>>> plt.colorbar()
Python basics: Matplotlib (plotting)
• Bar plots
Simple bar plot:
>>> plt.bar(x,sin(x),width=x[1]-x[0])

Horizontal bar plot:


>>> plt.barh(x,sin(x),
... height=x[1]-x[0],
... orientation='horizontal')
Python basics: Matplotlib (plotting)
• Histogram plots
Simple histogram plot with default of 10 bins:
>>> plt.hist(np.random.randn(1000))

Simple histogram plot with 30 bins :


>>> plt.hist(randn(1000), 30)
Python basics: Matplotlib (plotting)
>>> def f(t):
... s1 = np.cos(2*pi*t)

• Multiple plots using subplot ... e1 = np.exp(-t)


... return np.multiply(s1,e1)

>>> t1 = np.arange(0.0, 5.0, 0.1)


>>> plt.subplot(211) >>> t2 = np.arange(0.0, 5.0, 0.02)
>>> l = plot(t1, f(t1), 'bo', t2, f(t2),'k--') >>> t3 = np.arange(0.0, 2.0, 0.01)
>>> plt.setp(l, 'markerfacecolor', 'g')
>>> plt.grid(True)
>>> plt.title('A tale of 2 subplots')
>>> plt.ylabel('Damped oscillation')

>>> plt.subplot(212)
>>> plt.plot(t3, cos(2*pi*t3), 'r.')
>>> plt.grid(True)
>>> plt.xlabel('time (s)')
>>> plt.ylabel('Undamped')

>>> plt.show()

You might also like