1 of 26

CSE 163

Numpy and Images�

Suh Young Choi�

🎶 Listening to: Delta Sleep

💬 Before Class: Tell each other (and/or me) a cool philosophy!

2 of 26

Announcements

EDA + LR 5 due tonight!

HW5 due on Thursday (last one of the quarter!)

Resubmissions for HW 2 close tomorrow night

2

3 of 26

Checking in with the Check-ins

What’s up with the project?

  • Project Code/Report and Video Presentation specs will drop later this week
  • You can visit the Past Project Gallery on the course website to get a feel for what the project can look like

Any difficulties? Talk to us!

  • It’s never too late to get help if you need it
  • If Ed and office hours aren’t working out, you can schedule a one-to-one with Suh Young
  • Just because it’s summer doesn’t mean it doesn’t get hard

3

4 of 26

This Time

  • numpy arrays
  • Broadcasting
  • Images

Last Time

  • Algorithmic Efficiency
  • Big-O

4

5 of 26

Numpy Arrays

When using numpy, the primary data structure that we work with is called a numpy.array.

We saw some syntax for creating numpy arrays

5

a = np.array([12, 6]) # [12, 6]

b = np.arange(5) # [0, 1, 2, 3, 4]

c = np.ones(3) # [1, 1, 1]

6 of 26

Reshaping

6

We looked at how numpy arrays have shape

x = np.ones((2, 3))

x: array([[1, 1, 1],

[1, 1, 1]])

Numpy provides a function called reshape that allows us to change the shape of an array without change the data inside.

y = np.arange(9)

y = y.reshape((3, 3))

y: array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

7 of 26

Reshaping

7

We also saw how that numpy arrays have shape

x = np.ones((2, 3))

x: array([[1, 1, 1],

[1, 1, 1]])

Numpy provides a function called reshape that allows us to change the shape of an array without change the data inside.

y = np.arange(9)

y = y.reshape((3, 3))

y: array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

8 of 26

Broadcasting

8

m = np.ones((2, 3))

v = np.arange(3)

m + v

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

m.shape = (2, 3)

v.shape = (3,)

Numpy lets us perform element-wise operations on arrays that have the same shape (and sometimes with different shape!)

+

9 of 26

Broadcasting

9

m = np.ones((2, 3))

v = np.arange(3)

m + v

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

m.shape = (2, 3)

v.shape = (3,)

Numpy lets us perform element-wise operations on arrays that have the same shape (and sometimes with different shape!)

+

10 of 26

Broadcasting

10

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

m.shape = (2, 3)

v.shape = (3,)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

11 of 26

Broadcasting

11

m: [[1, 1, 1],

[1, 1, 1]]

v: [0, 1, 2]

m.shape = (2, 3)

v.shape = (1, 3)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

12 of 26

Broadcasting

12

m: [[1, 1, 1],

[1, 1, 1]]

v: [[0, 1, 2]]

m.shape = (2, 3)

v.shape = (1, 3)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

13 of 26

Broadcasting

13

m: [[1, 1, 1],

[1, 1, 1]]

v: [[0, 1, 2]]

m.shape = (2, 3)

v.shape = (2, 3)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

14 of 26

Broadcasting

14

m: [[1, 1, 1],

[1, 1, 1]]

v: [[0, 1, 2],

[0, 1, 2]]

m.shape = (2, 3)

v.shape = (2, 3)

+

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If the sizes of any dimension disagree and neither is equal to 1, an error is raised.

15 of 26

Images as

Matrices

15

Grey-scale images can be represented as matrices.

Grey-scale: 255

Grey-scale: 0

data = imageio.imread(‘...’)�

data[rows, columns] = #

16 of 26

Color Images

When you overlap each color channel, it creates a picture we are used to seeing.

  • Pixels on your monitor let out specified R/G/B light

16

data[rows, columns, channels] = #

17 of 26

17

Practice: Broadcasting

18 of 26

Practice:

Broadcasting

18

x = np.arange(3).reshape((3, 1))

y = np.arange(3)

x + y

x: [[0],

[1],

[2]]

y: [0, 1, 2]

x.shape = (3, 1)

y.shape = (3,)

We started off with the following instructions

+

19 of 26

Practice:

Broadcasting

19

x: [[0],

[1],

[2]]

y: [0, 1, 2]

x.shape = (3, 1)

y.shape = (3,)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

20 of 26

Practice:

Broadcasting

20

x: [[0],

[1],

[2]]

y: [0, 1, 2]

x.shape = (3, 1)

y.shape = (1, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

21 of 26

Practice:

Broadcasting

21

x: [[0],

[1],

[2]]

y: [[0, 1, 2]]

x.shape = (3, 1)

y.shape = (1, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

22 of 26

Practice:

Broadcasting

22

x: [[0],

[1],

[2]]

y: [[0, 1, 2]]

x.shape = (3, 3)

y.shape = (1, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

23 of 26

Practice:

Broadcasting

23

x: [[0, 0, 0]],

[1, 1, 1],

[2, 2, 2]]

y: [[0, 1, 2]]

x.shape = (3, 3)

y.shape = (1, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

24 of 26

Practice:

Broadcasting

24

x: [[0, 0, 0]],

[1, 1, 1],

[2, 2, 2]]

y: [[0, 1, 2]]

x.shape = (3, 3)

y.shape = (3, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

25 of 26

Practice:

Broadcasting

25

x: [[0, 0, 0]],

[1, 1, 1],

[2, 2, 2]]

y: [[0, 1, 2],

[0, 1, 2],

[0, 1, 2]]

x.shape = (3, 3)

y.shape = (3, 3)

The Rules of Broadcasting

  1. If the two arrays differ in their number of dimensions, the shape of the one with fewer dimensions is padded on it’s left side.
  2. If the shape of two arrays does not match on any dimension, the array with shape equal to 1 in that dimension is stretched to match the other shape.
  3. If in any dimension the sizes disagree and neither is equal to 1, an error is raised.

+

26 of 26

Before Next Time

  • Complete Lesson 19
    • Remember not for points, but do go towards Checkpoint Tokens
  • EDA + LR 5 due tonight
  • Resub cycle closes tomorrow (last day to resubmit HW2)

Next Time

  • Convolutions

26