Aan de slagGa gratis aan de slag

Vectorizing .upper()

There are many situations where you might want to use Python methods and functions on array elements in NumPy. You can always write a for loop to do this, but vectorized operations are much faster and more efficient, so consider using np.vectorize()!

You've got an array called names which contains first and last names:

names = np.array([["Izzy", "Monica", "Marvin"],
                  ["Weber", "Patel", "Hernandez"]])

You'd like to use one of the Python methods you learned on DataCamp, .upper(), to make all the letters of every name in the array uppercase. As a reminder, .upper() is a string method, meaning that it must be called on an instance of a string: str.upper().

Your task is to vectorize this Python method. numpy is loaded for you as np, and the names array is available.

Deze oefening maakt deel uit van de cursus

Introduction to NumPy

Cursus bekijken

Oefeninstructies

  • Create a vectorized function called vectorized_upper from the Python .upper() string method.
  • Apply vectorized_upper() to the names array and save the resulting array as uppercase_names.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Vectorize the .upper() string method
vectorized_upper = ____

# Apply vectorized_upper to the names array
uppercase_names = ____
print(uppercase_names)
Code bewerken en uitvoeren