1 / 8

Last Week

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 .

Télécharger la présentation

Last Week

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Last Week • Special characters in strings • Files – what are they? • Opening and closing files • Reading and writing to files • Comma Separated Files

  2. 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

  3. 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)

  4. 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]

  5. 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]])

  6. 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]])

  7. 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.

  8. 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.

More Related