Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE 194/BIO 196: Modeling,simulating and optimizing biological systems

Similar presentations


Presentation on theme: "EE 194/BIO 196: Modeling,simulating and optimizing biological systems"— Presentation transcript:

1 EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Spring 2018 Tufts University Instructor: Joel Grodstein 2D arrays

2 Arrays so far We’ve already used arrays – but just in “1D”
HW1 = np.array([90,99,60,90]) HW1[0]=90 HW1[3]=60 One homework, four students Next up: add an extra dimension (so, “2D”) Use an egg carton again 90 99 60 90 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

3 2D arrays Think of a 2D array as a table of numbers
3 homeworks, 4 students Each row is a homework Each column is a student How do I access it? HW[0,2] is 60 HW[1,2] is 55 HW[2,1] is How do I build it? HW = np.array ([[90,99,60,90],[91,80,55,93],[89,80,99,93]]) [0] [1] [2] 90 99 60 90 91 80 55 93 89 80 99 93 [0] [1] [2] [3] 80 EE 194/Bio 196 Joel Grodstein

4 2D arrays Another way to build it: HW = np.zeros ((3,4))
[0] [1] [2] 90 99 60 90 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

5 2D arrays Another way to build it: HW = np.zeros ((3,4))
(just “:” means the whole row) [0] [1] [2] 90 99 60 90 91 80 55 93 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

6 Reading 2D arrays Read it as usual: print (HW[0,0:4]) print (HW[1,:])
[0] [1] [2] 90 99 60 90 91 80 55 93 [0] [1] [2] [3] Read it as usual: print (HW[0,0:4]) print (HW[1,:]) print (HW[2,:]) [90,99,60,90] [91,80,55,93] [0,0,0,0] EE 194/Bio 196 Joel Grodstein

7 More slice access HW[1,1:] HW[1:3,2] HW[1:3,2:] 80 55 93 90 99 60 90
[0] [1] [2] 80 55 93 90 99 60 90 91 80 55 93 55 [0] [1] [2] [3] 55 93 EE 194/Bio 196 Joel Grodstein

8 Modify one element Assume we’ve set HW[2,:]=[89,80,99,93] HW[1,2]=90
error [0] [1] [2] 90 99 60 90 91 80 55 90 93 50 89 80 99 93 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

9 Still more slice access
copy a row HW[2,:] = HW[1,:] copy two columns HW[:,2:4] = HW[:,0:2] [0] [1] [2] 90 99 60 90 91 91 80 55 93 80 55 93 89 80 99 93 [0] [1] [2] [3] [0] [1] [2] 90 90 99 91 80 89 99 60 90 91 80 55 93 89 80 99 93 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

10 Remember our problem with arrays?
a1 = np.array ([2,4,6]) a2 = a1 a2[1]=1 print (a2) print (a1) a2 = np.array ([2,4,6]) We’ll learn more strategies to beat this problem later [2, 4, 6] Now a2=[2, 4, 6] [2, 1, 6] [2, 1, 6] Now we have two different egg cartons [2, 1, 6] “Later” has now arrived [2, 4, 6] EE 194/Bio 196 Joel Grodstein

11 Copying an array a1 = np.array ([2,4,6]) a2 = np.array ([2,4,6])
print (a2) print (a1) And this works just as well with 2D arrays. We’ll use it in the Manduca HW Now we have two different egg cartons a1.copy() [2, 1, 6] [2, 4, 6] EE 194/Bio 196 Joel Grodstein

12 Looping through an array
Remember how we looped through an array? ar = np.array ([1,3,5]) for i in ar: What happens if we loop through a 2D array? ar = np.array ([[1,3,5],[11,13,15]]) What if you want to loop through every element of a 2D array? for i in ar.flat: i becomes 1, 3 and then 5 i becomes [1,3,5] and then [11,13,15] i becomes 1, 3, 5, 11, 13 and finally 15 EE 194/Bio 196 Joel Grodstein

13 Advanced indexing Python has advanced indexing capabilities that mirror Matlab HW [ HW<65] += 10 HW [2, HW[2,:]>90] = 0 [0] [1] [2] 90 99 70 60 90 91 80 65 55 93 89 80 99 93 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein

14 Advanced indexing Advanced indexing can build a 2D array from elements of a 1D array: ar_1D = np.array ([2,4,6,8]) indices = np.array ([[1,1][2,3]]) ar_2D = ar_1D [indices] It can also do the reverse: ar_1D = ar_2D [ [0,1,1], [0,1,0] ] 4 4 6 8 4 8 6 EE 194/Bio 196 Joel Grodstein

15 Group exercise Given: two arrays ar1 and ar2
Each one is 10 rows and 4 columns Build an array ar3 that has even rows from ar1, odd from ar2 Make sure that changing ar3 will not change ar1 or ar2 Lookahead: this will be similar to a genetic algorithm EE 194/Bio 196 Joel Grodstein

16 Another group exercise
Given: one 10x4 array ar1 Pick a random pair of rows (e.g., rows 0&1) Replicate it forwards (i.e., so rows 2&3 become identical to 0&1) Make sure not to try to copy, e.g., rows 7&8 to rows 9 & 10 Since there is no row #10! (This may be useful for genetic mutations) EE 194/Bio 196 Joel Grodstein

17 Follow-up activities Try the examples from this lecture yourself
Vary them, or even mis-type some to see what happens More exercises. Write a program that… Creates an NxN array with 1 on the border and 0 inside Creates an 8x8 array and fills it with a checkerboard pattern Finds the indices of the maximum and minimum values in a 2D array Creates an NxN cyclic matrix [[ ] [ ][ ] ...]. Then double every element and print it. Prints the odd columns of a 2D array EE 194/Bio 196 Joel Grodstein


Download ppt "EE 194/BIO 196: Modeling,simulating and optimizing biological systems"

Similar presentations


Ads by Google