170 likes | 187 Views
Learn how to manipulate 2D arrays for biological system optimization using Python with Joel Grodstein at Tufts University. Explore array indexing, copying, looping, and advanced techniques.
E N D
EE 194/BIO 196: Modeling,simulating and optimizing biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu 2D arrays
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
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
2D arrays • Another way to build it: • HW = np.zeros ((3,4)) • HW[0,0:4] = [90,99,60,90] [0] [1] [2] 90 99 60 90 0 0 0 0 0 0 0 0 0 0 0 0 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein
2D arrays • Another way to build it: • HW = np.zeros ((3,4)) • HW[0,0:4] = [90,99,60,90] • HW[1,:] = [91,80,55,93] • (just “:” means the whole row) [0] [1] [2] 90 99 60 90 91 80 55 93 0 0 0 0 0 0 0 0 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein
Reading 2D arrays • Read it as usual: • print (HW[0,0:4]) • print (HW[1,:]) • print (HW[2,:]) [0] [1] [2] 90 99 60 90 91 80 55 93 0 0 0 0 [0] [1] [2] [3] [90,99,60,90] [91,80,55,93] [0,0,0,0] EE 194/Bio 196 Joel Grodstein
More slice access • HW[1,1:] • HW[1:3,2] • HW[1:3,2:] [0] [1] [2] 90 99 60 90 80 55 93 91 80 55 93 0 0 0 0 55 [0] [1] [2] [3] 0 55 93 0 0 EE 194/Bio 196 Joel Grodstein
Modify one element • Assume we’ve set HW[2,:]=[89,80,99,93] • HW[1,2]=90 • HW[2,0]=50 • HW[3,0] = 80 • error [0] [1] [2] 90 99 60 90 91 80 93 90 55 89 80 99 93 50 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein
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 80 55 93 89 80 99 93 [0] [1] [2] [3] [0] [1] [2] 91 80 55 93 90 99 60 90 91 80 55 93 89 80 99 93 90 99 91 80 [0] [1] [2] [3] 89 80 EE 194/Bio 196 Joel Grodstein
Remember our problem with arrays? a1 = np.array ([2,4,6]) a2 = a1 a2[1]=1 print (a2) print (a1) a1 = np.array ([2,4,6]) a2 = np.array ([2,4,6]) a2[1]=1 print (a2) print (a1) 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
Copying an array a1 = np.array ([2,4,6]) a2 = np.array ([2,4,6]) a2[1]=1 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
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]]) for i in ar: • What if you want to loop through every element of a 2D array? ar = np.array ([[1,3,5],[11,13,15]]) 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
Advanced indexing • Python has advanced indexing capabilities that mirror Matlab • HW [ HW<65] += 10 • HW [2, HW[2,:]>90] = 0 [0] [1] [2] 70 90 99 60 90 65 91 80 55 93 89 80 99 93 0 0 [0] [1] [2] [3] EE 194/Bio 196 Joel Grodstein
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
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
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 maybe useful for genetic mutations) EE 194/Bio 196 Joel Grodstein
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 [[0 1 2 3 4] [1 2 3 4 0][2 3 4 0 1] ...]. Then double every element and print it. • Prints the odd columns of a 2D array EE 194/Bio 196 Joel Grodstein