Numerical Python v0.3
Numerical Python v0.3
Python
v0.3
Research Computing
Services
IS & T
Run
Spyder
Start the
Anaconda
Navigator
Click on
Spyder’s
Launch button
Be patient…it
takes a while to
start.
Download from the RCS
website
Open a browser and go to:
http://rcs.bu.edu/examples/python/tutorials/
Alternatives to Python
Python’s
strengths
Python is a general purpose language.
In contrast with R or Matlab which started out as specialized languages
Review
Operation Syntax Notes
x[1] ‘b’
Indexing backwards from -1 x[-1] 3.14
x[-3] ‘a’
Slicing x[start:end:incr] Slicing produces a COPY
of the original list!
x[0:2] [‘a’,’b’]
x[-1:-3:-1] [3.14,’b’]
x[:] [‘a’,’b’,3.14]
Sorting x.sort() in-place sort Depending on list
sorted(x) returns a new sorted list contents a sorting
function might be req’d
Size of a list len(x)
Each has a unique
Computer address
memory
All the bytes in a …
row
All of the memory in a computer is accessed (and connected
electronically) as a 1D array.Any byte can be read via its unique
address.
1 2 3
y[1] check the ndarray data type retrieve the value at offset
1 in the data array return 2
https://docs.scipy.org/doc/numpy/reference/arrays.html
dtyp
e
Every ndarray has a dtype, the a = np.array([1,2,3])
type of data that it holds. a.dtype
This is used to interpret the dtype('int32')
block of
data stored in the ndarray.
c = np.array([-1,4,124],
dtype='int8')
Can be assigned at creation c.dtype --> dtype('int8')
time:
A small amount of memory is used to store info about the ndarray (~few dozen bytes)
list
The numpy function array creates a new array from any data
structure with array like behavior (other ndarrays, lists, sets,
etc.)
Read the docs!
indexing twoD =
oneD.reshape([2,2])
ndarray indexing is similar
twoD array([[1, 2],
to Python lists, strings, [3, 4]])
tuples, etc.
# index from 0
oneD[0] 1
Index with integers, oneD[3] 4
starting from # -index starts from the end
zero. oneD[-1] 4
oneD[-2] 3
slicing
Syntax for each dimension (same
rules as lists): y[0:3] --> array([ 50, 100, 150])
start:end:step y[-1:-3:-1] --> array([250, 200])
start: from starting index to end
:end start from 0 to end (exclusive of x = np.arange(10,130,10).reshape(4,3)
end) # x --> array([[ 10, 20, 30],
: all elements. [ 40, 50, 60],
[ 70, 80, 90],
Slicing an ndarray does not [100, 110, 120]])
make a copy, it creates a view
to the original data. # 1-D returned!
x[:,0] --> array([ 10, 40, 70, 100])
# 2-D returned!
Slicing a Python list creates a x[2:4,1:3] --> array([[ 80,
copy. 90], [110, 120]])
y[0:3] = -1
# y --> array([ -1, -1, -1, 200,
250])
y[0:8] = -1
# NO ERROR!
# y --> array([ -1, -1, -1, -1, -1])
y[0:2] = x[3:5]
# =
x y np.array([10,20,30,40,50,60])
--> array([ 40, 50, -1, -1, -1])
ndarray addressing with an
ndarray
Ndarray’s can be used to a=np.linspace(-1,1,5)
# a --> [-1. , -0.5, 0. , 0.5, 1. ]
address/index another
ndarray. b=np.array([0,1,2])
Column-major order:
Fortran, R, Matlab, and
others
https://en.wikipedia.org/wiki/Row-_and_column-major_order
ndarray memory
# Y is C-ordered by default
layout
For row-major ordering the
Y = np.ones([2,3,4])
# For loop indexing:
rightmost index accesses total=0.0
values in adjacent memory. for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
for k in range(Y.shape[2]):
The opposite is true for column- total += Y[i,j,k]
major ordering.
# X is Fortan-ordered
X = np.ones([2,3,4], order='F')
This can have a significant # For loop indexing:
impact on performance when total=0.0
accessing ndarrays with for for i in range(X.shape[2]):
for j in range(X.shape[1]):
loops or built-in functions like for k in range(X.shape[0]):
numpy.sum() total += X[k,j,i]
https://docs.scipy.org/doc/numpy/reference/routines.linalg.html
NumPy
I/O
When reading files you can use standard Python, use lists,
allocate
ndarrays and fill them.
Docs: https://docs.scipy.org/doc/numpy/reference/routines.io.html
Numpy
docs
As numpy is a large library we can only cover the basic
usage here
genfromtxt.
Task: read temperature data into a 2D
ndarray, clean up missing data, plot the
data.
Outlin
e The numpy
library
Libraries: scipy and opencv
Open scipy_minimize.py
https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html
OpenC
V • Image Processing
The Open Source • Image file reading and writing
• Video I/O
Computer Vision Library • High-level GUI
• Video Analysis
• Camera Calibration and 3D Reconstruction
Highly optimized and mature
• 2D Features Framework
C++ library usable from C+ • Object Detection
+, Java, and Python. • Deep Neural Network module
• Machine Learning
• Clustering and Search in Multi-Dimensional Spaces
Cross platform: Windows, • Computational Photography
Linux, • Image stitching
Mac OSX, iOS, Android
OpenCV vs
SciPy
For imaging-related operations and many linear algebra functions
there is a lot of overlap between these two libraries.
scipy.ndimage.gaussian_f 85.7
Gaussian 3.7x
ilter cv2.GaussianBlur 23.2
scipy.ndimage.median_filter 1,780
Median 22.5x
cv2.medianBlur 79.2
When NumPy and SciPy aren’t fast
enough
Auto-compile your Python code with the numba and numexpr
libraries
a
The @jit decorator is # This will get compiled when it's
used to indicate which first executed
@jit
functions are compiled.
def average(x,
Options: y, z):
GPU code generation return (x
Parallelization + y + z) /
Caching of compiled code 3.0
You can use the Intel Thread Building Blocks library which may
improve performance on multithreaded Python programs:
Cytho
n
Cython is a superset of the Python language.
If you visit this link later please make sure to select the correct
tutorial – name, time, and location.
http://scv.bu.edu/survey/tutorial_evaluation.html