NumPy. Daniël de Kok. May 4, 2017

Size: px
Start display at page:

Download "NumPy. Daniël de Kok. May 4, 2017"

Transcription

1 NumPy Daniël de Kok May 4, 2017

2 Introduction

3 Today Today s lecture is about the NumPy linear algebra library for Python. Today you will learn: How to create NumPy arrays, which store vectors, matrices, and higher order tensors.

4 Today Today s lecture is about the NumPy linear algebra library for Python. Today you will learn: How to create NumPy arrays, which store vectors, matrices, and higher order tensors. How to index into and modify NumPy arrays.

5 Today Today s lecture is about the NumPy linear algebra library for Python. Today you will learn: How to create NumPy arrays, which store vectors, matrices, and higher order tensors. How to index into and modify NumPy arrays. How to perform common linear algebra operations on NumPy arrays.

6 Today Today s lecture is about the NumPy linear algebra library for Python. Today you will learn: How to create NumPy arrays, which store vectors, matrices, and higher order tensors. How to index into and modify NumPy arrays. How to perform common linear algebra operations on NumPy arrays. How to reshape and transpose tensors.

7 Today Today s lecture is about the NumPy linear algebra library for Python. Today you will learn: How to create NumPy arrays, which store vectors, matrices, and higher order tensors. How to index into and modify NumPy arrays. How to perform common linear algebra operations on NumPy arrays. How to reshape and transpose tensors. How arrays are broadcast in NumPy.

8 What is NumPy? NumPy is a linear algebra library. Main NumPy data types: multidimensional arrays. Supports tensors arbitrary-rank tensors, including: rank-1 tensors (vectors) rank-2 tensors (matrices) Numpy uses C (or Fortran) arrays and BLAS operations.. Avoids the memory and computation overhead associated with Python lists/interpretation. Operator overloading for convenient notation.

9 Relation to Tensorflow Although we will use Tensorflow to construct computation graphs and for parameter estimation, mastering NumPy is important for three reasons: The input to a Tensorflow graph is typically provided as a NumPy array. The Tensorflow tensor API is strongly inspired by NumPy. Quick experimentation is usually easier with NumPy (Tensorflow constructs computation graphs that need to be executed).

10 Importing NumPy The NumPy package is called numpy. It is customary to import numpy as np: 1 import numpy as np From here onwards, these lecture notes assume that NumPy is imported in this manner.

11 Tensors So far, we have been dealing with: Vectors: R M Matrices: R M N

12 Tensors So far, we have been dealing with: Vectors: R M Matrices: R M N Vectors are 1st order tensors, matrices 2nd order tensors.

13 Tensors So far, we have been dealing with: Vectors: R M Matrices: R M N Vectors are 1st order tensors, matrices 2nd order tensors. Sometimes we need tensors of higher orders, e.g. a 3rd order tensor: R M N O

14 NumPy arrays

15 Constructing a new array A NumPy array can be constructed directly from a Python list using the array function: 1 np.array([1,2,3]) This returns a one-dimensional array (a vector), which has the following string representation: array([1, 2, 3])

16 Multi-dimensional lists The array function can also handle multi-dimensional Python lists. That is, list with embedded lists: 1 np.array([[1,2,3],[4,5,6]]) Each inner array in represents a row. NumPy uses a pretty printer to give the output as a formatted string: array([[1, 2, 3], [4, 5, 6]])

17 Array properties NumPy arrays have the following properties that we will frequently use: ndim The rank of the tensor. shape The shape of the tensor a tuple specifying the size of each tensor dimension. dtype The data type of tensor elements.

18 Array properties: example Here we see the values of these properties for an array that is constructed from a two-dimensional Python list: 1 m = np.array([[1,2,3],[4,5,6]]) 2 "ndim: %s, shape: %s, size: %s, dtype: %s" % \ 3 (m.ndim, m.shape, m.size, m.dtype) ndim: 2, shape: (2, 3), size: 6, dtype: int64

19 Data type inference NumPy bases the data type on the data type of the elements in the original array. If we used float literals instead, the array would have float64 as the data type: 1 np.array([1.0, 2.0, 3.0]).dtype dtype('float64')

20 Data types NumPy supports the typical data types one would expect from a linear algebra library: Data types int{8,16,32,64} uint{8,16,32,64} float{16,32,64} bool_ Description Signed integers of the given widths Unsigned integers of the given widths Half/single/double precision floating point Boolean values

21 Data type specification The data type for the elements can be specified using the dtype argument of array-creating functions. For example: 1 example = np.array([1,2,3], dtype=np.float32) 2 example.dtype dtype('float32')

22 Zero array It is common that you want to have an array consisting of only zeros. The np.zeros function creates such an array with the given data type: 1 np.zeros((3,2), dtype=np.float16) array([[ 0., 0.], [ 0., 0.], [ 0., 0.]], dtype=float16)

23 One array Similarly to zeros, there is a ones function: 1 np.ones((3,2), dtype=np.float16) array([[ 1., 1.], [ 1., 1.], [ 1., 1.]], dtype=float16)

24 One array Similarly to zeros, there is a ones function: 1 np.ones((3,2), dtype=np.float16) array([[ 1., 1.], [ 1., 1.], [ 1., 1.]], dtype=float16) More generally, np.full creates an array filled with the a given number: 1 np.full((3,2), 42, dtype=np.float16) array([[ 42., 42.], [ 42., 42.], [ 42., 42.]], dtype=float16)

25 Values from an interval np.arange creates a vector with values in the half-open interval [start, end): 1 np.arange(1, 4) array([1, 2, 3])

26 Values from an interval np.arange creates a vector with values in the half-open interval [start, end): 1 np.arange(1, 4) array([1, 2, 3]) Leaving the starting index of the interval, assumes 0: 1 np.arange(4) array([0, 1, 2, 3])

27 Values from an interval (2) Finally, arange also allows you to indicate a step, similarly to the regular Python range: 1 np.arange(0, 10, 2) array([0, 2, 4, 6, 8])

28 Random array Another interesting array initialization is the random array. np.random.random returns floating point random numbers from a uniform distribution [0.0, 1.0). For example: 1 np.random.random((3,2)) array([[ , ], [ , ], [ , ]])

29 Distributions The np.random module also supports drawing random numbers from other distributions, such as normal, poisson, and zipf distributions. The following code generates a matrix of random numbers from a normal distribution with mean 1.0 and standard deviation np.random.normal(loc=1.0, scale=0.1, size=(3,2)) array([[ , ], [ , ], [ , ]])

30 Indexing An array can be indexed to retrieve or assign a cell. Indexing is performed with the subscript operator ([]). In indexing, dimensions are separated by a comma. 1 a = np.array([[1,2,3],[4,5,6]]) 2 a[1,2] 6

31 Indexing (2) If the index does not specify an offset into each dimension, the tensor at the given index is returned: 1 a = np.array([[1,2,3],[4,5,6]]) 2 a[1] array([4, 5, 6])

32 Indexing (3) Consequently, we can also get the component that we are interested in by indexing on the returned tensor: 1 a = np.array([[1,2,3],[4,5,6]]) 2 a[1][2] 6

33 Slicing As with Python lists, arrays can be sliced by using the subscript operator in conjunction with colon (:): 1 a = np.arange(0,10) 2 a[2:8] array([2, 3, 4, 5, 6, 7])

34 Strides Adding an extra colon gives the possibility to specify a stride: 1 a = np.arange(0,10) 2 a[2:8:2] array([2, 4, 6])

35 Strides (2) Sometimes, it is also useful to only specify a stride: 1 a = np.arange(0,10) 2 a[::2] array([0, 2, 4, 6, 8])

36 In-class assignment: slicing Extract the following slices from np.arange(0, 20): array([10, 11, 12, 13, 14, 15]) array([1, 4, 7, 10, 13]) array([18, 14, 10]) Extract the following slices from np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]): array([[1, 2, 3], [7, 8, 9]]) array([[1, 3], [4, 6], [7, 9], [10, 12]])

37 Array updates The indexing operator can also be used to assign a value to a cell: 1 a = np.arange(0, 6) 2 a[4] = 42 3 a array([ 0, 1, 2, 3, 42, 5])

38 Array updates (2) This also works for more complex indexing, such as indexing with strides. 1 a = np.arange(0, 6) 2 a[::2] = [42, 43, 44] 3 a array([42, 1, 43, 3, 44, 5])

39 Array updates (2) This also works for more complex indexing, such as indexing with strides. 1 a = np.arange(0, 6) 2 a[::2] = [42, 43, 44] 3 a array([42, 1, 43, 3, 44, 5]) Of course, you have to ensure that the shape of the right operand is compatible with the left operand.

40 Linear algebra on vectors

41 Warning NumPy does overloading on operators such as *, / and +. When using the standard NumPy array data type, these are elementwise. Be careful when you are familiar with Matlab, C++ linear algebra libraries, etc.

42 Dot product np.dot implements the dot product on vectors: 1 np.dot(np.array([1.,2.,3.]), np.array([4., 5., 6.])) 32.0

43 Dot product np.dot implements the dot product on vectors: 1 np.dot(np.array([1.,2.,3.]), np.array([4., 5., 6.])) 32.0 As we will see later, dot also provides matrix multiplication.

44 Erroneous dot product Erroneous dot products, such as the following: 1 np.dot(np.array([1.,2.]), np.array([4., 5., 6.])) will fail with an exception: 1 ValueError: shapes (2,) and (3,) not aligned: 2 (dim 0)!= 3 (dim 0)

45 PEP 465 PEP 465 proposes the infix for matrix multiplication. Supported by: Python 3.5 and later NumPy and later Documented as the np.matmul function.

46 PEP 465 PEP 465 proposes the infix for matrix multiplication. Supported by: Python 3.5 and later NumPy and later Documented as the np.matmul function. 1 np.arange(1, 4, \ 2 np.arange(4, 7, dtype=np.float32) 32.0

47 In-class assignment Implement a function l2norm that takes a vector as its argument and returns its l 2 norm. Implement a function euclid_dist that takes two vectors as its arguments and returns the euclidean distance between the vectors. Implement a function cosine_sim that takes two vectors as its arguments and returns the cosine similarity between the vectors (cos Θ). Note: you can use the math.sqrt or np.sqrt function for computing the square root. You can use the regular - operator for elementwise subtraction of vector components.

48 NumPy norms: l 2 np.linalg.norm function computes norms, the ord argument is used to specify which norm: 1 np.linalg.norm(np.arange(10), ord=2)

49 NumPy norms: l 1 1 np.linalg.norm(np.arange(10), ord=1) 45.0

50 Matrices

51 Reshaping A raw data array in memory can represent different matrices. For example, assume that we have the following array in row-major order: This array could represent a couple of different matrices: 1 [ [ ] ] ,, 3 4,

52 Reshaping (2) Reshaping changes the dimensions the data without changing its components.

53 Reshaping (2) Reshaping changes the dimensions the data without changing its components. For example: 1 np.arange(1,7).reshape((2,3)) array([[1, 2, 3], [4, 5, 6]])

54 Reshaping (3) A tensor can be reshaped into another tensor, if both tensors have the same number of components.

55 Reshaping (3) A tensor can be reshaped into another tensor, if both tensors have the same number of components. Two tensors have the same number of components if the multiplication of the dimension sizes are the same. For example, a vector with shape (8) can be reshaped into: (2,4) (4,2) (2,2,2) (1,8) But not into: (2,5) (2,2,2,2) (7)

56 Reshaping and copying reshape tries to return a view of the original array when possible: no copying necessary. Sometimes it is necessary to make a copy, e.g.: consider reshaping a transposed view of a tensor.

57 Inference of dimensions One of the dimensions of the shape can be set to have size -1. NumPy will then infer the dimension. 1 np.arange(1,7).reshape((2,-1)) array([[1, 2, 3], [4, 5, 6]])

58 Transposition The transpose() method of an array gives a transposed view (if possible) of a tensor. The T property of an array returns self when the number of array dimensions is less than 2 or the result of transpose otherwise. 1 np.arange(1,7).reshape((2,3)).t array([[1, 4], [2, 5], [3, 6]])

59 Tensor transposition How does transposition in a higher order tensor ndim > 2 work?

60 Tensor transposition How does transposition in a higher order tensor ndim > 2 work? The dimensions are reversed: A i,j,k becomes A k,j,i Example: 1 str(np.arange(0, 24).reshape((2, 3, 4)).T.shape) (4, 3, 2)

61 Broadcasting Consider the following elementwise sum: 1 a = np.array([[1, 1, 1], [2, 2, 2]]) 2 b = np.array([1, 2, 3]) 3 4 a + b array([[2, 3, 4], [3, 4, 5]])

62 Broadcasting Consider the following elementwise sum: 1 a = np.array([[1, 1, 1], [2, 2, 2]]) 2 b = np.array([1, 2, 3]) 3 4 a + b array([[2, 3, 4], [3, 4, 5]]) The shapes of the arrays are (2, 3) and (3). Why does this work?

63 Broadcasting Consider the following elementwise sum: 1 a = np.array([[1, 1, 1], [2, 2, 2]]) 2 b = np.array([1, 2, 3]) 3 4 a + b array([[2, 3, 4], [3, 4, 5]]) The shapes of the arrays are (2, 3) and (3). Why does this work? NumPy applies broadcasting.

64 Broadcasting (2) Broadcasting: when arrays have different sizes, the smaller array is broadcast accross the larger array so that they have equal sizes.

65 Broadcasting (2) Broadcasting: when arrays have different sizes, the smaller array is broadcast accross the larger array so that they have equal sizes. Constraints: NumPy compares the dimension sizes elementwise in reverse order. Two dimensions are compatible when: They have the same size. When one of the dimensions has size 1.

66 Broadcasting (2) Broadcasting: when arrays have different sizes, the smaller array is broadcast accross the larger array so that they have equal sizes. Constraints: NumPy compares the dimension sizes elementwise in reverse order. Two dimensions are compatible when: They have the same size. When one of the dimensions has size 1. The shape does not necessarily need the same dimensions. Consider our previous example with shapes (2, 3) and (3). (3) can be seen as (1, 3), making broadcasting possible.

67 In-class assignment: broadcasting Try to sum a tensor of shape (2, 1, 3) and a tensor of shape (2, 3, 1) elementwise. Explain the result.

68 Matrix multiplication The dot function operator that we have seen before can also be used for matrix multiplication: 1 a = np.arange(0, 6).reshape((2, 3)) 2 b = np.arange(0, 6).reshape((3, 2)) 3 4 b array([[10, 13], [28, 40]])

69 Matrix multiplication (2) If we use a regular vector, it is automatically converted to a matrix: 1 a = np.arange(0, 4).reshape((2, 2)) 2 b = np.array([1,2]) 3 4 b array([2, 8])

70 Matrix multiplication (2) If we use a regular vector, it is automatically converted to a matrix: 1 a = np.arange(0, 4).reshape((2, 2)) 2 b = np.array([1,2]) 3 4 b array([2, 8]) Beware: b!= a

71 Matrix multiplication (3) 1 a = np.arange(0, 4).reshape((2, 2)) 2 b = np.array([1,2]) 3 4 a array([4, 7])

NumPy Primer. An introduction to numeric computing in Python

NumPy Primer. An introduction to numeric computing in Python NumPy Primer An introduction to numeric computing in Python What is NumPy? Numpy, SciPy and Matplotlib: MATLAB-like functionality for Python Numpy: Typed multi-dimensional arrays Fast numerical computation

More information

NumPy quick reference

NumPy quick reference John W. Shipman 2016-05-30 12:28 Abstract A guide to the more common functions of NumPy, a numerical computation module for the Python programming language. This publication is available in Web form1 and

More information

PYTHON NUMPY TUTORIAL CIS 581

PYTHON NUMPY TUTORIAL CIS 581 PYTHON NUMPY TUTORIAL CIS 581 VARIABLES AND SPYDER WORKSPACE Spyder is a Python IDE that s a part of the Anaconda distribution. Spyder has a Python console useful to run commands quickly and variables

More information

Introduction to NumPy

Introduction to NumPy Lab 3 Introduction to NumPy Lab Objective: NumPy is a powerful Python package for manipulating data with multi-dimensional vectors. Its versatility and speed makes Python an ideal language for applied

More information

Problem Based Learning 2018

Problem Based Learning 2018 Problem Based Learning 2018 Introduction to Machine Learning with Python L. Richter Department of Computer Science Technische Universität München Monday, Jun 25th L. Richter PBL 18 1 / 21 Overview 1 2

More information

LECTURE 22. Numerical and Scientific Packages

LECTURE 22. Numerical and Scientific Packages LECTURE 22 Numerical and Scientific Packages NUMERIC AND SCIENTIFIC APPLICATIONS As you might expect, there are a number of third-party packages available for numerical and scientific computing that extend

More information

CSC Advanced Scientific Computing, Fall Numpy

CSC Advanced Scientific Computing, Fall Numpy CSC 223 - Advanced Scientific Computing, Fall 2017 Numpy Numpy Numpy (Numerical Python) provides an interface, called an array, to operate on dense data buffers. Numpy arrays are at the core of most Python

More information

Scientific Programming. Lecture A08 Numpy

Scientific Programming. Lecture A08 Numpy Scientific Programming Lecture A08 Alberto Montresor Università di Trento 2018/10/25 Acknowledgments: Stefano Teso, Documentation http://disi.unitn.it/~teso/courses/sciprog/python_appendices.html https://docs.scipy.org/doc/numpy-1.13.0/reference/

More information

LECTURE 19. Numerical and Scientific Packages

LECTURE 19. Numerical and Scientific Packages LECTURE 19 Numerical and Scientific Packages NUMERICAL AND SCIENTIFIC APPLICATIONS As you might expect, there are a number of third-party packages available for numerical and scientific computing that

More information

Numpy fast array interface

Numpy fast array interface NUMPY Numpy fast array interface Standard Python is not well suitable for numerical computations lists are very flexible but also slow to process in numerical computations Numpy adds a new array data type

More information

Exercise: Introduction to NumPy arrays

Exercise: Introduction to NumPy arrays Exercise: Introduction to NumPy arrays Aim: Introduce basic NumPy array creation and indexing Issues covered: Importing NumPy Creating an array from a list Creating arrays of zeros or ones Understanding

More information

NumPy and SciPy. Lab Objective: Create and manipulate NumPy arrays and learn features available in NumPy and SciPy.

NumPy and SciPy. Lab Objective: Create and manipulate NumPy arrays and learn features available in NumPy and SciPy. Lab 2 NumPy and SciPy Lab Objective: Create and manipulate NumPy arrays and learn features available in NumPy and SciPy. Introduction NumPy and SciPy 1 are the two Python libraries most used for scientific

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Autumn 2016-17 Lecture 11: NumPy & SciPy Introduction, Plotting and Data Analysis 1 Today s Plan Introduction to NumPy & SciPy Plotting Data Analysis 2 NumPy and SciPy

More information

Python Numpy (1) Intro to multi-dimensional array & numerical linear algebra. Harry Lee January 29, 2018 CEE 696

Python Numpy (1) Intro to multi-dimensional array & numerical linear algebra. Harry Lee January 29, 2018 CEE 696 Python Numpy (1) Intro to multi-dimensional array & numerical linear algebra Harry Lee January 29, 2018 CEE 696 Table of contents 1. Introduction 2. Linear Algebra 1 Introduction From the last lecture

More information

Derek Bridge School of Computer Science and Information Technology University College Cork

Derek Bridge School of Computer Science and Information Technology University College Cork CS4618: rtificial Intelligence I Vectors and Matrices Derek Bridge School of Computer Science and Information Technology University College Cork Initialization In [1]: %load_ext autoreload %autoreload

More information

Chapter 5 : Informatics Practices. Class XII ( As per CBSE Board) Numpy - Array. New Syllabus Visit : python.mykvs.in for regular updates

Chapter 5 : Informatics Practices. Class XII ( As per CBSE Board) Numpy - Array. New Syllabus Visit : python.mykvs.in for regular updates Chapter 5 : Informatics Practices Class XII ( As per CBSE Board) Numpy - Array New Syllabus 2019-20 NumPy stands for Numerical Python.It is the core library for scientific computing in Python. It consist

More information

NumPy is suited to many applications Image processing Signal processing Linear algebra A plethora of others

NumPy is suited to many applications Image processing Signal processing Linear algebra A plethora of others Introduction to NumPy What is NumPy NumPy is a Python C extension library for array-oriented computing Efficient In-memory Contiguous (or Strided) Homogeneous (but types can be algebraic) NumPy is suited

More information

Handling arrays in Python (numpy)

Handling arrays in Python (numpy) Handling arrays in Python (numpy) Thanks to all contributors: Alison Pamment, Sam Pepler, Ag Stephens, Stephen Pascoe, Anabelle Guillory, Graham Parton, Esther Conway, Wendy Garland, Alan Iwi and Matt

More information

IAP Python - Lecture 4

IAP Python - Lecture 4 IAP Python - Lecture 4 Andrew Farrell MIT SIPB January 13, 2011 NumPy, SciPy, and matplotlib are a collection of modules that together are trying to create the functionality of MATLAB in Python. Andrew

More information

The SciPy Stack. Jay Summet

The SciPy Stack. Jay Summet The SciPy Stack Jay Summet May 1, 2014 Outline Numpy - Arrays, Linear Algebra, Vector Ops MatPlotLib - Data Plotting SciPy - Optimization, Scientific functions TITLE OF PRESENTATION 2 What is Numpy? 3rd

More information

Implement NN using NumPy

Implement NN using NumPy Implement NN using NumPy Hantao Zhang Deep Learning with Python Reading: https://www.tutorialspoint.com/numpy/ Recommendation for Using Python Install anaconda on your PC. If you already have installed

More information

MLCV 182: Practical session 1 Ron Shapira Weber Computer Science, Ben-Gurion University

MLCV 182: Practical session 1 Ron Shapira Weber Computer Science, Ben-Gurion University MLCV 182: Practical session 1 Ron Shapira Weber Computer Science, Ben-Gurion University Getting Started There are two different versions of Python being supported at the moment, 2.7 and 3.6. For compatibility

More information

CME 193: Introduction to Scientific Python Lecture 5: Object Oriented Programming

CME 193: Introduction to Scientific Python Lecture 5: Object Oriented Programming CME 193: Introduction to Scientific Python Lecture 5: Object Oriented Programming Nolan Skochdopole stanford.edu/class/cme193 5: Object Oriented Programming 5-1 Contents Classes Numpy Exercises 5: Object

More information

Why NumPy / SciPy? NumPy / SciPy / Matplotlib. A Tour of NumPy. Initializing a NumPy array

Why NumPy / SciPy? NumPy / SciPy / Matplotlib. A Tour of NumPy. Initializing a NumPy array NumPy / SciPy / Matplotlib NumPy is an extension to Python adding support for arrays and matrices, along with a large library of high-level mathematical functions to operate on them. SciPy is a library

More information

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2 COMP1730/COMP6730 Programming for Scientists Sequence types, part 2 Lecture outline * Lists * Mutable objects & references Sequence data types (recap) * A sequence contains n 0 values (its length), each

More information

NumPy. Arno Proeme, ARCHER CSE Team Attributed to Jussi Enkovaara & Martti Louhivuori, CSC Helsinki

NumPy. Arno Proeme, ARCHER CSE Team Attributed to Jussi Enkovaara & Martti Louhivuori, CSC Helsinki NumPy Arno Proeme, ARCHER CSE Team aproeme@epcc.ed.ac.uk Attributed to Jussi Enkovaara & Martti Louhivuori, CSC Helsinki Reusing this material This work is licensed under a Creative Commons Attribution-

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Arrays Dr. David Koop Class Example class Rectangle: def init (self, x, y, w, h): self.x = x self.y = y self.w = w self.h = h def set_corner(self, x, y): self.x =

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Arrays and Series Dr. David Koop Exception Example def divide(mylist, x,y): newlist = [] try: z = x // y below, mid, above = \ mylist[:z], mylist[z], mylist[z+1:]

More information

Short Introduction to Python Machine Learning Course Laboratory

Short Introduction to Python Machine Learning Course Laboratory Pattern Recognition and Applications Lab Short Introduction to Python Machine Learning Course Laboratory Battista Biggio battista.biggio@diee.unica.it Luca Didaci didaci@diee.unica.it Dept. Of Electrical

More information

HW0 v3. October 2, CSE 252A Computer Vision I Fall Assignment 0

HW0 v3. October 2, CSE 252A Computer Vision I Fall Assignment 0 HW0 v3 October 2, 2018 1 CSE 252A Computer Vision I Fall 2018 - Assignment 0 1.0.1 Instructor: David Kriegman 1.0.2 Assignment Published On: Tuesday, October 2, 2018 1.0.3 Due On: Tuesday, October 9, 2018

More information

CME 193: Introduction to Scientific Python Lecture 6: Numpy, Scipy, Matplotlib

CME 193: Introduction to Scientific Python Lecture 6: Numpy, Scipy, Matplotlib CME 193: Introduction to Scientific Python Lecture 6: Numpy, Scipy, Matplotlib Nolan Skochdopole stanford.edu/class/cme193 6: Numpy, Scipy, Matplotlib 6-1 Contents Homeworks and Project Numpy Scipy Matplotlib

More information

Matrices 4: use of MATLAB

Matrices 4: use of MATLAB Matrices 4: use of MATLAB Anthony Rossiter http://controleducation.group.shef.ac.uk/indexwebbook.html http://www.shef.ac.uk/acse Department of Automatic Control and Systems Engineering Introduction The

More information

NumPy User Guide. Release dev7335. Written by the NumPy community

NumPy User Guide. Release dev7335. Written by the NumPy community NumPy User Guide Release 1.4.0.dev7335 Written by the NumPy community August 28, 2009 CONTENTS 1 Building and installing NumPy 3 1.1 Binary installers............................................. 3 1.2

More information

CME 193: Introduction to Scientific Python Lecture 5: Numpy, Scipy, Matplotlib

CME 193: Introduction to Scientific Python Lecture 5: Numpy, Scipy, Matplotlib CME 193: Introduction to Scientific Python Lecture 5: Numpy, Scipy, Matplotlib Sven Schmit stanford.edu/~schmit/cme193 5: Numpy, Scipy, Matplotlib 5-1 Contents Second part of course Numpy Scipy Matplotlib

More information

Part VI. Scientific Computing in Python. Alfredo Parra : Scripting with Python Compact Max-PlanckMarch 6-10,

Part VI. Scientific Computing in Python. Alfredo Parra : Scripting with Python Compact Max-PlanckMarch 6-10, Part VI Scientific Computing in Python Compact Course @ Max-PlanckMarch 6-10, 2017 63 Doing maths in Python Standard sequence types (list, tuple,... ) Can be used as arrays Can contain different types

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2018 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2018 1 / 32 Outline 1 Matrix operations Importance Dense and sparse

More information

Phys Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3

Phys Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3 Phys 60441 Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3 Tim O Brien Room 3.214 Alan Turing Building tim.obrien@manchester.ac.uk Tuples Lists and strings are examples of sequences.

More information

NumPy. Computational Physics. NumPy

NumPy. Computational Physics. NumPy NumPy Computational Physics NumPy Outline Some Leftovers Get people on line! Write a function / Write a script NumPy NumPy Arrays; dexing; Iterating Creating Arrays Basic Operations Copying Linear Algebra

More information

The NumPy Array: A Structure for Efficient Numerical Computation

The NumPy Array: A Structure for Efficient Numerical Computation The NumPy Array: A Structure for Efficient Numerical Computation Presented at the G-Node Autumn School on Advanced Scientific Programming in Python, held in Kiel, Germany Stéfan van der Walt UC Berkeley

More information

Python VSIP API: A first draft

Python VSIP API: A first draft Python VSIP API: A first draft Stefan Seefeld HPEC WG meeting, December 9, 2014 Goals Use cases: Promote VSIP standard to a wider audience (SciPy users) Add more hardware acceleration to SciPy Allow VSIP

More information

Python for Scientists

Python for Scientists High level programming language with an emphasis on easy to read and easy to write code Includes an extensive standard library We use version 3 History: Exists since 1991 Python 3: December 2008 General

More information

Matrix Multiplication

Matrix Multiplication Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2013 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2013 1 / 32 Outline 1 Matrix operations Importance Dense and sparse

More information

Intelligente Datenanalyse Intelligent Data Analysis

Intelligente Datenanalyse Intelligent Data Analysis Universität Potsdam Institut für Informatik Lehrstuhl Maschinelles Lernen Intelligent Data Analysis Tobias Scheffer, Gerrit Gruben, Nuno Marquez Plan for this lecture Introduction to Python Main goal is

More information

NumPy and SciPy. Shawn T. Brown Director of Public Health Applications Pittsburgh Supercomputing Center Pittsburgh Supercomputing Center

NumPy and SciPy. Shawn T. Brown Director of Public Health Applications Pittsburgh Supercomputing Center Pittsburgh Supercomputing Center NumPy and SciPy Shawn T. Brown Director of Public Health Applications Pittsburgh Supercomputing Center 2012 Pittsburgh Supercomputing Center What are NumPy and SciPy NumPy and SciPy are open-source add-on

More information

Pandas and Friends. Austin Godber Mail: Source:

Pandas and Friends. Austin Godber Mail: Source: Austin Godber Mail: godber@uberhip.com Twitter: @godber Source: http://github.com/desertpy/presentations What does it do? Pandas is a Python data analysis tool built on top of NumPy that provides a suite

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Classes & Arrays Dr. David Koop Sets Sets are like dictionaries but without any values: s = {'MA', 'RI', 'CT', 'NH'}; t = {'MA', 'NY', 'NH'} {} is an empty dictionary,

More information

NumPy User Guide. Release dev7072. Written by the NumPy community

NumPy User Guide. Release dev7072. Written by the NumPy community NumPy User Guide Release 1.4.0.dev7072 Written by the NumPy community June 24, 2009 CONTENTS 1 Building and installing NumPy 3 1.1 Binary installers............................................. 3 1.2

More information

Effective Programming Practices for Economists. 10. Some scientific tools for Python

Effective Programming Practices for Economists. 10. Some scientific tools for Python Effective Programming Practices for Economists 10. Some scientific tools for Python Hans-Martin von Gaudecker Department of Economics, Universität Bonn A NumPy primer The main NumPy object is the homogeneous

More information

Python Crash Course Numpy, Scipy, Matplotlib

Python Crash Course Numpy, Scipy, Matplotlib Python Crash Course Numpy, Scipy, Matplotlib That is what learning is. You suddenly understand something you ve understood all your life, but in a new way. Doris Lessing Steffen Brinkmann Max-Planck-Institut

More information

AMath 483/583 Lecture 8

AMath 483/583 Lecture 8 AMath 483/583 Lecture 8 This lecture: Fortran subroutines and functions Arrays Dynamic memory Reading: class notes: Fortran Arrays class notes: Fortran Subroutines and Functions class notes: gfortran flags

More information

(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB

(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB (DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB TROY P. KLING Contents 1. Importing Libraries 1 2. Introduction to numpy 2 3. Introduction to matplotlib 5 4. Image Processing 8 5. The Mandelbrot Set

More information

Python (version 3.6) for R Users: Stat Modules I

Python (version 3.6) for R Users: Stat Modules I Python (version 3.6) for R Users: Stat Modules I CMU MSP 36601, Fall 2017, Howard Seltman 1. Use the numpy module to get vector, matrix, and array functionality as well as linear algebra. The official

More information

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer Prerequisites The 2D Arrays and Matrices Primer assumes knowledge of the MATLAB IDE, MATLAB help, arithmetic operations,

More information

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors. 1 LECTURE 3 OUTLINES Variable names in MATLAB Examples Matrices, Vectors and Scalar Scalar Vectors Entering a vector Colon operator ( : ) Mathematical operations on vectors examples 2 VARIABLE NAMES IN

More information

Session 04: Introduction to Numpy

Session 04: Introduction to Numpy Session 04: Introduction to Numpy October 9th, 2017 Wouter Klijn Overview Introduction Hello world Arrays Creating Interacting Copying Differences with Matlab Matrixes vs Array Why Why not Matlib module

More information

python 01 September 16, 2016

python 01 September 16, 2016 python 01 September 16, 2016 1 Introduction to Python adapted from Steve Phelps lectures - (http://sphelps.net) 2 Python is interpreted Python is an interpreted language (Java and C are not). In [1]: 7

More information

Lecture 15: High Dimensional Data Analysis, Numpy Overview

Lecture 15: High Dimensional Data Analysis, Numpy Overview Lecture 15: High Dimensional Data Analysis, Numpy Overview Chris Tralie, Duke University 3/3/2016 Announcements Mini Assignment 3 Out Tomorrow, due next Friday 3/11 11:55PM Rank Top 3 Final Project Choices

More information

Python Advance Course via Astronomy street. Sérgio Sousa (CAUP) ExoEarths Team (http://www.astro.up.pt/exoearths/)

Python Advance Course via Astronomy street. Sérgio Sousa (CAUP) ExoEarths Team (http://www.astro.up.pt/exoearths/) Python Advance Course via Astronomy street Sérgio Sousa (CAUP) ExoEarths Team (http://www.astro.up.pt/exoearths/) Advance Course Outline: Python Advance Course via Astronomy street Lesson 1: Python basics

More information

Table of Contents. Preface... xxi

Table of Contents. Preface... xxi Table of Contents Preface... xxi Chapter 1: Introduction to Python... 1 Python... 2 Features of Python... 3 Execution of a Python Program... 7 Viewing the Byte Code... 9 Flavors of Python... 10 Python

More information

Introduction to Python and NumPy I

Introduction to Python and NumPy I Introduction to Python and NumPy I This tutorial is continued in part two: Introduction to Python and NumPy II Table of contents Overview Launching Canopy Getting started in Python Getting help Python

More information

Introduction to Artificial Neural Networks and Deep Learning

Introduction to Artificial Neural Networks and Deep Learning Introduction to Artificial Neural Networks and Deep Learning A Practical Guide with Applications in Python Sebastian Raschka This book is for sale at http://leanpub.com/ann-and-deeplearning This version

More information

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous

More information

Adina Howe Instructor

Adina Howe Instructor INTRO TO PYTHON FOR FINANCE Arrays Adina Howe Instructor Installing packages pip3 install package_name_here pip3 install numpy Importing packages import numpy NumPy and Arrays import numpy my_array = numpy.array([0,

More information

python numpy tensorflow tutorial

python numpy tensorflow tutorial python numpy tensorflow tutorial September 11, 2016 1 What is Python? From Wikipedia: - Python is a widely used high-level, general-purpose, interpreted, dynamic programming language. - Design philosophy

More information

Computational Physics

Computational Physics Computational Physics Objects : Lists & Arrays Prof. Paul Eugenio Department of Physics Florida State University Jan 24, 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ Announcements Read chapter 3

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab Andreas C. Kapourani (Credit: Steve Renals & Iain Murray) 9 January 08 Introduction MATLAB is a programming language that grew out of the need to process matrices. It is used extensively

More information

MATLAB. Devon Cormack and James Staley

MATLAB. Devon Cormack and James Staley MATLAB Devon Cormack and James Staley MATrix LABoratory Originally developed in 1970s as a FORTRAN wrapper, later rewritten in C Designed for the purpose of high-level numerical computation, visualization,

More information

Python for Data Analysis. Prof.Sushila Aghav-Palwe Assistant Professor MIT

Python for Data Analysis. Prof.Sushila Aghav-Palwe Assistant Professor MIT Python for Data Analysis Prof.Sushila Aghav-Palwe Assistant Professor MIT Four steps to apply data analytics: 1. Define your Objective What are you trying to achieve? What could the result look like? 2.

More information

2.1 Indefinite Loops. while <condition>: <body> rabbits = 3 while rabbits > 0: print rabbits rabbits -= 1

2.1 Indefinite Loops. while <condition>: <body> rabbits = 3 while rabbits > 0: print rabbits rabbits -= 1 2.1 Indefinite Loops The final kind of control flow is Python s indefinite loop, the while loop. It functions much like the for loop in that it repeatedly executes some body of statements. The difference

More information

CS224n: Natural Language Processing with Deep Learning 1

CS224n: Natural Language Processing with Deep Learning 1 CS224n: Natural Language Processing with Deep Learning 1 Lecture Notes: TensorFlow 2 Winter 2017 1 Course Instructors: Christopher Manning, Richard Socher 2 Authors: Zhedi Liu, Jon Gauthier, Bharath Ramsundar,

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

cosmos_python_ Python as calculator May 31, 2018

cosmos_python_ Python as calculator May 31, 2018 cosmos_python_2018 May 31, 2018 1 Python as calculator Note: To convert ipynb to pdf file, use command: ipython nbconvert cosmos_python_2015.ipynb --to latex --post pdf In [3]: 1 + 3 Out[3]: 4 In [4]:

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Data Frames Dr. David Koop 2D Indexing [W. McKinney, Python for Data Analysis] 2 Boolean Indexing names == 'Bob' gives back booleans that represent the elementwise

More information

NumPy User Guide. Release Written by the NumPy community

NumPy User Guide. Release Written by the NumPy community NumPy User Guide Release 1.11.0 Written by the NumPy community May 29, 2016 CONTENTS 1 Setting up 3 1.1 What is NumPy?............................................. 3 1.2 Installing NumPy.............................................

More information

Fundamentals of the J Programming Language

Fundamentals of the J Programming Language 2 Fundamentals of the J Programming Language In this chapter, we present the basic concepts of J. We introduce some of J s built-in functions and show how they can be applied to data objects. The pricinpals

More information

Numerical Calculations

Numerical Calculations Fundamentals of Programming (Python) Numerical Calculations Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Scipy Lecture Notes at http://www.scipy-lectures.org/ Outline

More information

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization DSC 201: Data Analysis & Visualization Data Frames Dr. David Koop pandas Contains high-level data structures and manipulation tools designed to make data analysis fast and easy in Python Built on top of

More information

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors. Physics 326G Winter 2008 Class 2 In this class you will learn how to define and work with arrays or vectors. Matlab is designed to work with arrays. An array is a list of numbers (or other things) arranged

More information

Week Two. Arrays, packages, and writing programs

Week Two. Arrays, packages, and writing programs Week Two Arrays, packages, and writing programs Review UNIX is the OS/environment in which we work We store files in directories, and we can use commands in the terminal to navigate around, make and delete

More information

2.7 Numerical Linear Algebra Software

2.7 Numerical Linear Algebra Software 2.7 Numerical Linear Algebra Software In this section we will discuss three software packages for linear algebra operations: (i) (ii) (iii) Matlab, Basic Linear Algebra Subroutines (BLAS) and LAPACK. There

More information

INTRODUCTION TO DATA SCIENCE

INTRODUCTION TO DATA SCIENCE INTRODUCTION TO DATA SCIENCE JOHN P DICKERSON PREM SAGGAR Today! Lecture #3 9/5/2018 CMSC320 Mondays & Wednesdays 2pm 3:15pm ANNOUNCEMENTS Register on Piazza: piazza.com/umd/fall2018/cmsc320 219 have registered

More information

LAB 2 VECTORS AND MATRICES

LAB 2 VECTORS AND MATRICES EN001-4: Intro to Computational Design Tufts University, Department of Computer Science Prof. Soha Hassoun LAB 2 VECTORS AND MATRICES 1.1 Background Overview of data types Programming languages distinguish

More information

Lezione 6. Installing NumPy. Contents

Lezione 6. Installing NumPy. Contents Lezione 6 Bioinformatica Mauro Ceccanti e Alberto Paoluzzi Dip. Informatica e Automazione Università Roma Tre Dip. Medicina Clinica Università La Sapienza Lab 01: Contents As with a lot of open-source

More information

Introduction to Scientific Computing with Python, part two.

Introduction to Scientific Computing with Python, part two. Introduction to Scientific Computing with Python, part two. M. Emmett Department of Mathematics University of North Carolina at Chapel Hill June 20 2012 The Zen of Python zen of python... fire up python

More information

Tentative NumPy Tutorial

Tentative NumPy Tutorial Page 1 of 30 Tentative NumPy Tutorial Please do not hesitate to click the edit button. You will need to create a User Account first. Contents 1. Prerequisites 2. The Basics 1. An example 2. Array Creation

More information

The NumPy array: a structure for efficient numerical computation

The NumPy array: a structure for efficient numerical computation The NumPy array: a structure for efficient numerical computation Stefan Van Der Walt, S. Chris Colbert, Gaël Varoquaux To cite this version: Stefan Van Der Walt, S. Chris Colbert, Gaël Varoquaux. The NumPy

More information

Python Working with files. May 4, 2017

Python Working with files. May 4, 2017 Python Working with files May 4, 2017 So far, everything we have done in Python was using in-memory operations. After closing the Python interpreter or after the script was done, all our input and output

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

The MatriCs. Reloaded. A linear algebra-specific language for the budding C enthusiast. Short name: MaC Extension:.neo

The MatriCs. Reloaded. A linear algebra-specific language for the budding C enthusiast. Short name: MaC Extension:.neo The MatriCs Reloaded A linear algebra-specific language for the budding C enthusiast Short name: MaC Extension:.neo Talal Asem Toukan [tat2132] - Manager Emmanuel Koumandakis [ek2808] - System Architect

More information

Paraphrase Identification; Numpy; Scikit-Learn

Paraphrase Identification; Numpy; Scikit-Learn Paraphrase Identification; Numpy; Scikit-Learn Benjamin Roth Centrum für Informations- und Sprachverarbeitung Ludwig-Maximilian-Universität München beroth@cis.uni-muenchen.de Benjamin Roth (CIS) Paraphrase

More information

SciPy. scipy [www.scipy.org and links on course web page] scipy arrays

SciPy. scipy [www.scipy.org and links on course web page] scipy arrays SciPy scipy [www.scipy.org and links on course web page] - scipy is a collection of many useful numerical algorithms (numpy is the array core) - Python wrappers around compiled libraries and subroutines

More information

MO101: Python for Engineering Vladimir Paun ENSTA ParisTech

MO101: Python for Engineering Vladimir Paun ENSTA ParisTech I MO101: Python for Engineering Vladimir Paun ENSTA ParisTech License CC BY-NC-SA 2.0 http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Introduction to Python Introduction About Python Python itself

More information

INF : NumPy and SciPy

INF : NumPy and SciPy INF5830 2015: NumPy and SciPy Python with extension packages have become one of the preferred tools for data science and machine learning. The packages NumPy and SciPy are backbones of this approach. We

More information

Introduction to Matlab/Octave

Introduction to Matlab/Octave Introduction to Matlab/Octave February 28, 2014 This document is designed as a quick introduction for those of you who have never used the Matlab/Octave language, as well as those of you who have used

More information

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015

Numerical Methods. Centre for Mathematical Sciences Lund University. Spring 2015 Numerical Methods Claus Führer Alexandros Sopasakis Centre for Mathematical Sciences Lund University Spring 2015 Preface These notes serve as a skeleton for the course. They document together with the

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

CMAT Language - Language Reference Manual COMS 4115

CMAT Language - Language Reference Manual COMS 4115 CMAT Language - Language Reference Manual COMS 4115 Language Guru: Michael Berkowitz (meb2235) Project Manager: Frank Cabada (fc2452) System Architect: Marissa Ojeda (mgo2111) Tester: Daniel Rojas (dhr2119)

More information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Introduction to MATLAB. CS534 Fall 2016

Introduction to MATLAB. CS534 Fall 2016 Introduction to MATLAB CS534 Fall 2016 What you'll be learning today MATLAB basics (debugging, IDE) Operators Matrix indexing Image I/O Image display, plotting A lot of demos... Matrices What is a matrix?

More information