0
0
NumPydata~15 mins

np.vectorize() for custom functions in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Using np.vectorize() for Custom Functions
📖 Scenario: Imagine you have a list of temperatures in Celsius from different cities. You want to convert all these temperatures to Fahrenheit using a custom formula.
🎯 Goal: You will create a custom function to convert Celsius to Fahrenheit, then use np.vectorize() to apply this function to a list of temperatures efficiently.
📋 What You'll Learn
Create a list of Celsius temperatures called celsius_temps with the values 0, 20, 37, 100
Create a custom function called c_to_f that converts Celsius to Fahrenheit using the formula F = C * 9/5 + 32
Use np.vectorize() to apply c_to_f to celsius_temps and store the result in fahrenheit_temps
Print the fahrenheit_temps array
💡 Why This Matters
🌍 Real World
Scientists and engineers often need to apply custom calculations to many data points quickly. Vectorizing functions helps speed up these operations.
💼 Career
Data scientists and analysts use vectorized operations to handle large datasets efficiently without writing slow loops.
Progress0 / 4 steps
1
Create the Celsius temperatures list
Create a list called celsius_temps with these exact values: 0, 20, 37, 100
NumPy
Need a hint?

Use square brackets to create a list with the given numbers separated by commas.

2
Define the Celsius to Fahrenheit function
Define a function called c_to_f that takes one parameter c and returns the Fahrenheit value using the formula c * 9/5 + 32
NumPy
Need a hint?

Use def to create the function and return the calculated Fahrenheit value.

3
Use np.vectorize() to apply the function
Import numpy as np. Then create a variable called vectorized_c_to_f by applying np.vectorize() to the c_to_f function. Use vectorized_c_to_f to convert celsius_temps and store the result in fahrenheit_temps
NumPy
Need a hint?

Remember to import numpy as np before using np.vectorize().

4
Print the Fahrenheit temperatures
Print the variable fahrenheit_temps to display the converted temperatures
NumPy
Need a hint?

Use print(fahrenheit_temps) to show the converted temperatures.