4/19/2016

27. Two-Dimensional Arrays

Topics Motivation The numpy Module Subscripting functions and 2d Arrays

Rows and Columns

12 17 49 61 38 18 82 77 83 53 12 10

Thi s i s row 1.

4/19/2016

Visualizing

12 17 49 61 38 18 82 77 83 53 12 10

Can have a 2d array of strings or objects.

But we will just deal with 2d arrays of numbers.

A 2D array has row s and columns. Thi s one has 3 row s and 4 columns. We say i t i s a "3-by-4" array (a.k.a matri x)

Rows and Columns

12 17 49 61 38 18 82 77 83 53 12 10

Thi s i s column 2.

Entries

12 17 49 61 38 18 82 77 83 53 12 10

Thi s i s the (1,2) entry.

Where Do They Come From?

Entry (i ,j) i s the di stance from ci ty i to ci ty j

1

4/19/2016

Where Do they Come From?

Entry (i ,j) i s 1 i fnode i i s connected to node j and i s 0 otherw i se

Captures the connectivity in a network

N o d es

4 and 6

Are connected

Where Do They Come From

An m-by-n array of pi xels.

Each pi xel encodes 3 numbers: a red value, a green value, a blue value

So all the i nformati on can be encoded i n three 2D arrays

2d Arrays in Python

12 17 49 61 38 18 82 77 83 53 12 10

A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]] A li st of li sts.

Accessing Entries

12 17 49 61 38 18 82 77 83 53 12 10

A[1][2]

A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]]

Accessing Entries

12 17 49 61 38 18 82 77 83 53 12 10

A[2][1]

A = [[12,17,49,61],[38,18,82,77],[83,53,12,10]]

Setting Up 2D Arrays

Here i s a functi on that returns a reference to an m-by-n array of zeros:

def zeros(m,n): v = [] for k in range(n): v.append(0.0) A = [] for k in range(m): A.append(v) return A

2

Python is Awkward

Turns out that base Python i s not very handy for 2D array mani pulati ons.

The numpy module makes up for thi s.

We w i ll learn just enough numpy so that w e can do elementary plotti ng, i mage processi ng and other thi ngs.

4/19/2016

Introduction to 2D Arrays in numpy

A few essenti als i llustrated by examples.

Setting up a 2D Array of 0's

>>> from numpy import * >>> m = 3 >>> n = 4 >>> A = zeros((m,n)) >>> A array([[ 0., 0., 0., 0.],

[ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])

Note how the row and column dimensions are passed to zeros

Accessing an Entry

>>> A = zeros((3,2)) >>> A[2,1] = 10 >>> A array([[ 0., 0.],

[ 0., 0.], [ 0., 10.]])

A nicer notation than A[2][1].

Accessing an Entry

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

[4, 5, 6]])

Using the array constructor to build a 3-by-2 array. Note all the square brackets.

Use Copy to Avoid Aliasing

>>> A = array([[1,2],[3,4]]) >>> B = A >>> A[1,1] = 10 >>> B array([[ 1, 2],

[ 3, 10]])

>>> A = array([[1,2],[3,4]]) >>> B = copy(A) >>> A[1,1] = 10 >>> B array([[1, 2],

[3, 4]])

1 2 3 4

2D arrays are objects

3

Iteration and 2D Arrays

Lots of Nested Loops

4/19/2016

Nested Loops and 2D Arrays

A = array((3,3)) for i in range(3):

for j in range(3): A[i,j] = (i+1)*(j+1)

1 23 2 4 6 3 6 9

A

3x3 times table

Nested Loops and 2D Arrays

A = array((3,3))

Allocates memory, but doesn' t put any values i n the boxes. Much more effi ci ent than the

Repeated append framew ork.

Understanding 2D Array Set-Up

for i in range(3): for j in range(3): A[i,j] = (i+1)*(j+1)

for i in range(3): A[i,0] = (i+1)*(0+1) A[i,1] = (i+1)*(1+1) A[i,2] = (i+1)*(2+1)

Equi valent!

Understanding 2D Array Set-Up

for i in range(3): A[i,0] = (i+1)*(0+1) A[i,1] = (i+1)*(1+1) A[i,2] = (i+1)*(2+1)

1 23

Row 0 i s set up w hen i =0

Understanding 2D Array Set-Up

for i in range(3): A[i,0] = (i+1)*(0+1) A[i,1] = (i+1)*(1+1) A[i,2] = (i+1)*(2+1)

1 23 2 4 6

Row 1 i s set up w hen i =1

4

4/19/2016

Understanding 2D Array Set-Up

for i in range(3): A[i,0] = (i+1)*(0+1) A[i,1] = (i+1)*(1+1) A[i,2] = (i+1)*(2+1)

1 23 2 4 6 4 6 9

Row 2 i s set up w hen i =2

Extended Example

A company has m factori es and each of w hi ch makes n products. We' ll refer to such a company as an m-by-n company.

Customers submi t purchase orders i n w hi ch they i ndi cate how many of each product they w i sh to purchase. A length-n li st of numbers that expresses thi s called a PO li st.

Cost and Inventory

The cost of maki ng a product vari es from factory to factory. Inventory vari es from factory to factory.

Three Problems

A customer submi ts a purchase order that i s to be fi lled by a si ngle factory.

Q1. How much w ould i t cost each factory to fi ll the PO? Q2. Whi ch factori es have enough i nventory to fi ll the PO? Q3. Among the factori es that can fi ll the PO, w hi ch one can do i t most cheaply?

Ingredients

To set ourselves up for the soluti on to these problems w e need to understand:

-The i dea of a Cost Array (2D) - The i dea of an Inventory Array (2D)

- The i dea of a Purchase Order Array (1D) We w i ll use numpy arrays throughout.

Cost Array

10 36 22 15 62

C ---> 12 35 20 12 66

13 37 21 16 59

The value of C[k,j] i s w hat i t costs factory k to make product j.

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download