Week 2 - Exercise 5 - image2vector

Hello,

I am receiving:

NameError: name ‘np’ is not defined

when running the code below:


GRADED FUNCTION:image2vector

def image2vector(image):
“”"
Argument:
image – a numpy array of shape (length, height, depth)

Returns:
v -- a vector of shape (length*height*depth, 1)
"""

# (≈ 1 line of code)
# v =
# YOUR CODE STARTS HERE

v = image.reshape((image.shape[0] * image.shape[1] * image.shape[2], 1))


# YOUR CODE ENDS HERE

return v

This is a 3 by 3 by 2 array, typically images will be (num_px_x, num_px_y,3) where 3 represents the RGB values

t_image = np.array([[[ 0.67826139, 0.29380381],
[ 0.90714982, 0.52835647],
[ 0.4215251 , 0.45017551]],

               [[ 0.92814219,  0.96677647],
                [ 0.85304703,  0.52351845],
                [ 0.19981397,  0.27417313]],

               [[ 0.60659855,  0.00533165],
                [ 0.10820313,  0.49978937],
                [ 0.34144279,  0.94630077]]])

print ("image2vector(image) = " + str(image2vector(t_image)))

image2vector_test(image2vector)


NameError Traceback (most recent call last)
in
1 # This is a 3 by 3 by 2 array, typically images will be (num_px_x, num_px_y,3) where 3 represents the RGB values
----> 2 t_image = np.array([[[ 0.67826139, 0.29380381],
3 [ 0.90714982, 0.52835647],
4 [ 0.4215251 , 0.45017551]],
5

NameError: name ‘np’ is not defined


What am I missing???

Thank you for your help.

Greg

Most likely you can fix that by “Cell → Run All Above”. The thing to realize is that just having the code sitting there in a cell doesn’t do anything: you have to run it in order for it to take effect. There is this statement in one of the earlier cells:

import numpy as np

Now you need to run that cell in order for it to take effect. You need to do that anytime you close and reopen the notebook.

1 Like

That worked… I was only running the cell above which did not contain the import… thank you for the help!