Learning Group

Python-NumPy

14 Dec., 2016

Chaoran Huang, UNSW Computing

Python

  • Basic data types
  • Containers

NumPy

  • Arrays
  • Data types
  • Array Math

Python

Designed by Guido van Rossum

  • High-level
  • Interpreted
  • Dynamic
  • Multi-paradigm
  • Popular version: 2.7/ 3.5

In this talk we use Python 2.7

Python

Basic Data Types

  • Booleans
  • Numeric Types
  • Sequence Types and Containers

more details in https://docs.python.org/2/library/stdtypes.html

Python

Basic Data Types - Booleans

The following values are considered FALSE:

  • False
  • None
  • zero of any numeric type
  • any empty sequence
  • any empty mapping

All other values are considered TRUE.

In [26]:
t, f = True, False
print type(t)

print t and f # Logical AND;
print t or f  # Logical OR;
print not t   # Logical NOT;
print t != f  # Logical XOR;
<type 'bool'>
False
True
False
True

Python

Basic Data Types - Numeric Types

  • int: Integers;
    • equivalent to C longs in Python 2.x, non-limited length in Python 3.x
  • long:
    • Long integers of non-limited length; exists only in Python 2.x
  • float:
    • Floating-Point numbers, equivalent to C doubles
  • complex:
    • Complex Numbers
In [27]:
x = 3
print x, type(x)

print x + 1   # Addition;
print x - 1   # Subtraction;
print x * 2   # Multiplication;
print x ** 2  # Exponentiation;
3 <type 'int'>
4
2
6
9
In [28]:
x += 1
print x  # Prints "4"
x *= 2
print x  # Prints "8"
4
8
In [29]:
y = 2.5
print type(y) # Prints "<type 'float'>"
print y, y + 1, y * 2, y ** 2 # Prints "2.5 3.5 5.0 6.25"
<type 'float'>
2.5 3.5 5.0 6.25

Python

Basic Data Types - Sequence Types and Containers

  • str:
    • String; represented as a sequence of 8-bit characters in Python 2.x, but as a sequence of Unicode characters (in the range of U+0000 - U+10FFFF) in Python 3.x
  • list
    • similar to array, resizeable and can contain elements of different types
  • set
    • an unordered collection of distinct elements
  • dictionary
    • stores (key, value) pairs
  • tuple
    • an (immutable) ordered list of values
In [30]:
hello = 'hello'   # String literals can use single quotes
world = "world"   # or double quotes; it does not matter.
print hello, len(hello)
hello 5
In [31]:
hw = hello + ' ' + world  # String concatenation
print hw  # prints "hello world"
hello world
In [32]:
hw12 = '%s %s %d' % (hello, world, 12)  # sprintf style string formatting
print hw12  # prints "hello world 12"
hello world 12
In [33]:
s = "hello"
print s.capitalize()  # Capitalize a string; prints "Hello"
print s.upper()       # Convert a string to uppercase; prints "HELLO"
print s.rjust(7)      # Right-justify a string, padding with spaces; prints "  hello"
print s.center(7)     # Center a string, padding with spaces; prints " hello "
print s.replace('l', '(ell)')  # Replace all instances of one substring with another;
                               # prints "he(ell)(ell)o"
print '  world '.strip()  # Strip leading and trailing whitespace; prints "world"
Hello
HELLO
  hello
 hello 
he(ell)(ell)o
world
In [34]:
xs = [3, 1, 2]   # Create a list
print xs, xs[2]
print xs[-1]     # Negative indices count from the end of the list; prints "2"
[3, 1, 2] 2
2
In [35]:
xs[2] = 'foo'    # Lists can contain elements of different types
print xs
[3, 1, 'foo']
In [36]:
xs.append('bar') # Add a new element to the end of the list
print xs
[3, 1, 'foo', 'bar']
In [37]:
x = xs.pop()     # Remove and return the last element of the list
print x, xs
bar [3, 1, 'foo']
In [38]:
# Slicing - concise syntax to access sublists

nums = range(5)    # range is a built-in function that creates a list of integers
print nums         # Prints "[0, 1, 2, 3, 4]"
print nums[2:4]    # Get a slice from index 2 to 4 (exclusive); prints "[2, 3]"
print nums[2:]     # Get a slice from index 2 to the end; prints "[2, 3, 4]"
print nums[:2]     # Get a slice from the start to index 2 (exclusive); prints "[0, 1]"
print nums[:]      # Get a slice of the whole list; prints ["0, 1, 2, 3, 4]"
print nums[:-1]    # Slice indices can be negative; prints ["0, 1, 2, 3]"
nums[2:4] = [8, 9] # Assign a new sublist to a slice
print nums         # Prints "[0, 1, 8, 8, 4]"
[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]
In [39]:
#Loop through List

animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
    print '#%d: %s' % (idx + 1, animal)
#1: cat
#2: dog
#3: monkey
In [40]:
# List comprehensions

nums = [0, 1, 2, 3, 4]
squares = []
for x in nums:
    squares.append(x ** 2)
print squares
[0, 1, 4, 9, 16]
In [41]:
nums = [0, 1, 2, 3, 4]
squares = [x ** 2 for x in nums]
print squares
[0, 1, 4, 9, 16]
In [42]:
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares
[0, 4, 16]
In [43]:
# Set

animals = {'cat', 'dog'}
print 'cat' in animals   # Check if an element is in a set; prints "True"
print 'fish' in animals  # prints "False"
animals.add('fish')      # Add an element to a set
print 'fish' in animals  # Prints "True"
print len(animals)       # Number of elements in a set; prints "3"
animals.add('cat')       # Adding an element that is already in the set does nothing
print len(animals)       # Prints "3"
animals.remove('cat')    # Remove an element from a set
print len(animals)       # Prints "2"
True
False
True
3
3
2
In [44]:
animals = {'cat', 'dog', 'fish'}
for idx, animal in enumerate(animals):
    print '#%d: %s' % (idx + 1, animal)
# Prints "#1: fish", "#2: dog", "#3: cat"
#1: fish
#2: dog
#3: cat
In [45]:
# Dictionary

d = {'cat': 'cute', 'dog': 'furry'}  # Create a new dictionary with some data
print d['cat']       # Get an entry from a dictionary; prints "cute"
print 'cat' in d     # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet'    # Set an entry in a dictionary
print d['fish']      # Prints "wet"
print d['monkey']  # KeyError: 'monkey' not a key of d
cute
True
wet
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-45-4d04a79d0baa> in <module>()
      6 d['fish'] = 'wet'    # Set an entry in a dictionary
      7 print d['fish']      # Prints "wet"
----> 8 print d['monkey']  # KeyError: 'monkey' not a key of d

KeyError: 'monkey'
In [66]:
print d.get('monkey', 'N/A')  # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A')    # Get an element with a default; prints "wet"
del d['fish']        # Remove an element from a dictionary
print d.get('fish', 'N/A') # "fish" is no longer a key; prints "N/A"
N/A
wet
N/A
In [67]:
# Loop through dictionary

d = {'person': 2, 'cat': 4, 'spider': 8}
for animal in d:
    legs = d[animal]
    print 'A %s has %d legs' % (animal, legs)
print '------'

d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.iteritems():
    print 'A %s has %d legs' % (animal, legs)
A person has 2 legs
A spider has 8 legs
A cat has 4 legs
------
A person has 2 legs
A spider has 8 legs
A cat has 4 legs
In [68]:
# Dictionary comprehensions

nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print even_num_to_square
{0: 0, 2: 4, 4: 16}
In [69]:
# Tuple

d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
t = (5, 6)       # Create a tuple
print type(t)
print d[t]       
print d[(1, 2)]
<type 'tuple'>
5
1
In [70]:
# Tuple cannot be mutated
# Thus Error Expected Here

t[0] = 1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-9e8806da6510> in <module>()
      2 # Thus Error Expected Here
      3 
----> 4 t[0] = 1

TypeError: 'tuple' object does not support item assignment

numpy NumPy is the fundamental package for scientific computing with Python. It contains among other things:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

Installing: https://docs.scipy.org/doc/numpy/user/install.html

Details see https://docs.scipy.org/doc/numpy-dev/user/index.html Tutorial for Matlab users: http://wiki.scipy.org/NumPy_for_Matlab_Users

NumPy

Arrays

  • NumPy’s main object
  • High-performance
  • Multidimensional
    • dimensions here called axes, and
    • number of dimension called rank
  • Homogeneous
    • table of elements of the same type
In [73]:
import numpy as np

a = np.array([1, 2, 3])  # Create a rank 1 array
print type(a)            # Prints "<type 'numpy.ndarray'>"
print a.shape            # Prints "(3,)"
print a[0], a[1], a[2]   # Prints "1 2 3"
a[0] = 5                 # Change an element of the array
print a                  # Prints "[5, 2, 3]"

b = np.array([[1,2,3],[4,5,6]])   # Create a rank 2 array
print b.shape                     # Prints "(2, 3)"
print b[0, 0], b[0, 1], b[1, 0]   # Prints "1 2 4"
<type 'numpy.ndarray'>
(3,)
1 2 3
[5 2 3]
(2, 3)
1 2 4
In [72]:
a = np.zeros((2,2))  # Create an array of all zeros
print a              # Prints "[[ 0.  0.]
                     #          [ 0.  0.]]"
    
b = np.ones((1,2))   # Create an array of all ones
print b              # Prints "[[ 1.  1.]]"

c = np.full((2,2), 7.) # Create a constant array
print c               # Prints "[[ 7.  7.]
                      #          [ 7.  7.]]"

d = np.eye(2)        # Create a 2x2 identity matrix
print d              # Prints "[[ 1.  0.]
                     #          [ 0.  1.]]"
    
e = np.random.random((2,2)) # Create an array filled with random values
print e                     # Might print "[[ 0.91940167  0.08143941]
                            #               [ 0.68744134  0.87236687]]"
[[ 0.  0.]
 [ 0.  0.]]
[[ 1.  1.]]
[[ 7.  7.]
 [ 7.  7.]]
[[ 1.  0.]
 [ 0.  1.]]
[[ 0.39743312  0.11084661]
 [ 0.43026082  0.90327968]]
In [74]:
# Numpy array indexing

import numpy as np

# Create the following rank 2 array with shape (3, 4)
# [[ 1  2  3  4]
#  [ 5  6  7  8]
#  [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

# Use slicing to pull out the subarray consisting of the first 2 rows
# and columns 1 and 2; b is the following array of shape (2, 2):
# [[2 3]
#  [6 7]]
b = a[:2, 1:3]

# A slice of an array is a view into the same data, so modifying it
# will modify the original array.
print a[0, 1]   # Prints "2"
b[0, 0] = 77    # b[0, 0] is the same piece of data as a[0, 1]
print a[0, 1]   # Prints "77"
2
77
In [75]:
#Integer array indexing

import numpy as np

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

# An example of integer array indexing.
# The returned array will have shape (3,) and 
print a[[0, 1, 2], [0, 1, 0]]  # Prints "[1 4 5]"

# The above example of integer array indexing is equivalent to this:
print np.array([a[0, 0], a[1, 1], a[2, 0]])  # Prints "[1 4 5]"

# When using integer array indexing, you can reuse the same
# element from the source array:
print a[[0, 0], [1, 1]]  # Prints "[2 2]"

# Equivalent to the previous integer array indexing example
print np.array([a[0, 1], a[0, 1]])  # Prints "[2 2]"
[1 4 5]
[1 4 5]
[2 2]
[2 2]
In [77]:
import numpy as np

# Create a new array from which we will select elements
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])

print a  # prints "array([[ 1,  2,  3],
         #                [ 4,  5,  6],
         #                [ 7,  8,  9],
         #                [10, 11, 12]])"

# Create an array of indices
b = np.array([0, 2, 0, 1])

# Select one element from each row of a using the indices in b
print a[np.arange(4), b]  # Prints "[ 1  6  7 11]"

# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10

print a  # prints "array([[11,  2,  3],
         #                [ 4,  5, 16],
         #                [17,  8,  9],
         #                [10, 21, 12]])
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[ 1  6  7 11]
[[11  2  3]
 [ 4  5 16]
 [17  8  9]
 [10 21 12]]
In [78]:
# Boolean array indexing

import numpy as np

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

bool_idx = (a > 2)  # Find the elements of a that are bigger than 2;
                    # this returns a numpy array of Booleans of the same
                    # shape as a, where each slot of bool_idx tells
                    # whether that element of a is > 2.
            
print bool_idx      # Prints "[[False False]
                    #          [ True  True]
                    #          [ True  True]]"

# We use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the True values
# of bool_idx
print a[bool_idx]  # Prints "[3 4 5 6]"

# We can do all of the above in a single concise statement:
print a[a > 2]     # Prints "[3 4 5 6]"
[[False False]
 [ True  True]
 [ True  True]]
[3 4 5 6]
[3 4 5 6]

Numpy

Data types

  • bool_ Boolean (True or False) stored as a byte
  • int_ Default integer type (same as C long; normally either int64 or int32)
  • intc Identical to C int (normally int32 or int64)
  • intp Integer used for indexing (same as C ssize_t; normally either int32 or int64)
  • int8 Byte (-128 to 127)
  • int16 Integer (-32768 to 32767)
  • int32 Integer (-2147483648 to 2147483647)
  • int64 Integer (-9223372036854775808 to 9223372036854775807)
  • uint8 Unsigned integer (0 to 255)
  • uint16 Unsigned integer (0 to 65535)
  • uint32 Unsigned integer (0 to 4294967295)
  • uint64 Unsigned integer (0 to 18446744073709551615)
  • float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
  • float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
  • float64/float_ Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
  • complex64 Complex number, represented by two 32-bit floats (real and imaginary components)
  • complex128/complex_ Complex number, represented by two 64-bit floats (real and imaginary components)

Numpy

Array Math

In [81]:
import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise sum;
print x + y
print np.add(x, y)

print '-----'
# Elementwise difference;
print x - y
print np.subtract(x, y)
[[  6.   8.]
 [ 10.  12.]]
[[  6.   8.]
 [ 10.  12.]]
-----
[[-4. -4.]
 [-4. -4.]]
[[-4. -4.]
 [-4. -4.]]
In [82]:
import numpy as np

x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)

# Elementwise product;
print x * y
print np.multiply(x, y)

print '-----'
# Elementwise division;
print x / y
print np.divide(x, y)

print '-----'
# Elementwise square root;
print np.sqrt(x)
[[  5.  12.]
 [ 21.  32.]]
[[  5.  12.]
 [ 21.  32.]]
-----
[[ 0.2         0.33333333]
 [ 0.42857143  0.5       ]]
[[ 0.2         0.33333333]
 [ 0.42857143  0.5       ]]
-----
[[ 1.          1.41421356]
 [ 1.73205081  2.        ]]
In [84]:
import numpy as np

x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])

v = np.array([9,10])
w = np.array([11, 12])

# Inner product of vectors;
print v.dot(w)
print np.dot(v, w)

print '-----'
# Matrix / vector product;
print x.dot(v)
print np.dot(x, v)

print '-----'
# Matrix / matrix product;
print x.dot(y)
print np.dot(x, y)
219
219
-----
[29 67]
[29 67]
-----
[[19 22]
 [43 50]]
[[19 22]
 [43 50]]
In [86]:
import numpy as np

x = np.array([[1,2],[3,4]])

print np.sum(x)  # Compute sum of all elements; prints "10"
print np.sum(x, axis=0)  # Compute sum of each column; prints "[4 6]"
print np.sum(x, axis=1)  # Compute sum of each row; prints "[3 7]"
print '-----'

# Transposing
print x.T

print '-----'
# Note that taking the transpose of a rank 1 array does nothing:
v = np.array([1,2,3])
print v    # Prints "[1 2 3]"
print v.T  # Prints "[1 2 3]"
10
[4 6]
[3 7]
-----
[[1 3]
 [2 4]]
-----
[1 2 3]
[1 2 3]

Thanks