80 likes | 165 Views
Last Week. Special characters in strings Files – what are they? Opening and closing files Reading and writing to files Comma Separated Files. Arrays. An array is similar to a list. Arrays store only one type at a time – usually numbers. Lists can store a variety of types .
E N D
Last Week • Special characters in strings • Files – what are they? • Opening and closing files • Reading and writing to files • Comma Separated Files
Arrays An array is similar to a list. Arrays store only one type at a time – usually numbers. Lists can store a variety of types. Arrays and lists are both indexed using []. Arrays are computationally very fast
Arrays Q. How do we create them? import numpy as np >>>a = np.array([1, 2, 3]) >>>b = np.array([[1, 2, 3], [4, 5, 6]]) >>>c = np.arange(12) array([0,1,2,3,4,5,6,7,8,9,10,11]) Q.What does this remind you of? A.range(n)
Arrays Q. How do we index them? >>>a = np.array([1, 2, 3]) >>>a[0] 1 >>>b = np.array([[1, 2, 3], [4, 5, 6]]) >>>b.shape (2, 3) Q. How do we index b to get the 5? A.b[1,1]
Reshaping Arrays >>>c = np.arange(12) array([0,1,2,3,4,5,6,7,8,9,10,11]) We can change the shape of an array: >>>c.reshape(2, 6)# returns the array reshaped array([[0,1,2,3,4,5], [6,7,8,9,10,11]])
Reshaping Arrays >>>c = np.arange(12) array([0,1,2,3,4,5,6,7,8,9,10,11]) We can change the size of an array: >>>c.resize(3,4) # changes the array >>>c array([[0,1,2,3], [4,5,6,7], [8,9,10,11]])
Reshaping Arrays Q. What does this array look like? >>>d.resize(2, 3, 2) # three dimensional array >>>d array([[[0,1], [2,3], [4,5]], [[6,7], [8,9], [10,11]]]) Think of this as a two story building with 6 units on each floor.
Using Arrays Q. What can we do with arrays? >>>a = np.array([1, 2, 3]) >>>b = np.array([4, 5, 6]) >>>c = a + b A.What is c? >>>c array([5, 7, 9]) Q. How is this different than lists? A. With lists, a+bis concatenation.