Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why math functions matter
📖 Scenario: Imagine you are a data analyst working with a small set of numbers representing daily temperatures in Celsius. You want to understand these numbers better by using math functions to find their square roots, squares, and absolute values. This helps you see the data in new ways and prepare it for further analysis.
🎯 Goal: You will create a list of temperatures, set up a configuration variable for a threshold, apply math functions from the numpy library to transform the data, and finally print the results.
📋 What You'll Learn
Create a list of temperatures with exact values
Create a threshold variable to compare values
Use numpy math functions: square root, square, and absolute value
Print the transformed data
💡 Why This Matters
🌍 Real World
Math functions help data scientists transform and understand data better. For example, square roots can normalize data, squares can emphasize larger values, and absolute values remove negative signs to focus on magnitude.
💼 Career
Knowing how to use math functions with numpy is essential for data cleaning, feature engineering, and preparing data for machine learning models.
Progress0 / 4 steps
1
Create the temperature data list
Create a list called temperatures with these exact values: 16, -9, 25, -4, 0.
NumPy
Hint
Use square brackets to create a list and separate numbers with commas.
2
Set a threshold value
Create a variable called threshold and set it to 10.
NumPy
Hint
Just assign the number 10 to the variable named threshold.
3
Apply numpy math functions
Import numpy as np. Then create three new lists: sqrt_temps with the square roots of the absolute values of temperatures, square_temps with the squares of temperatures, and abs_temps with the absolute values of temperatures.
NumPy
Hint
Use np.abs() to get absolute values, np.sqrt() for square roots, and np.square() for squares. Convert results to lists.
4
Print the transformed temperature lists
Print the lists sqrt_temps, square_temps, and abs_temps each on a separate line.
NumPy
Hint
Use three separate print statements, one for each list.
Practice
(1/5)
1. Why do we use math functions like np.sum() or np.mean() in data science?
easy
A. To quickly calculate important values from data arrays
B. To create new arrays from scratch
C. To change the data type of arrays
D. To sort the data alphabetically
Solution
Step 1: Understand the purpose of math functions
Math functions like np.sum() and np.mean() help us find total or average values quickly.
Step 2: Recognize their use in data analysis
These functions work on arrays to give useful summary numbers fast, which is key in data science.
Final Answer:
To quickly calculate important values from data arrays -> Option A
Quick Check:
Math functions = fast calculations [OK]
Hint: Math functions summarize data fast, like sum or average [OK]
Common Mistakes:
Thinking math functions create new arrays
Confusing math functions with sorting
Believing math functions change data types
2. Which of the following is the correct way to use the NumPy function to find the maximum value in an array arr?
easy
A. np.max(arr)
B. arr.max()
C. max(arr)
D. np.maximum(arr)
Solution
Step 1: Identify the correct NumPy function syntax
The function to find the max value in NumPy is np.max(), which takes the array as argument.
Step 2: Check other options for correctness
arr.max() works but is a method, not a function call; max(arr) is Python built-in, not NumPy; np.maximum(arr) requires two arrays, so incorrect here.
Final Answer:
np.max(arr) -> Option A
Quick Check:
Use np.max(array) for max value [OK]
Hint: Use np.max(array) to get max value quickly [OK]
Common Mistakes:
Using np.maximum with one array instead of two
Confusing Python max() with NumPy max()
Using method arr.max() when function np.max() is asked
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.sqrt(arr)
print(result)
medium
A. [1 2 3 4]
B. [1. 1.41421356 1.73205081 2.]
C. [1. 2. 3. 4.]
D. Error: sqrt() not defined for arrays
Solution
Step 1: Understand np.sqrt() on arrays
NumPy's np.sqrt() calculates the square root of each element in the array individually.
The function np.mean requires parentheses around the argument, like np.mean(arr).
Step 2: Identify the error in the code
The code uses np.mean arr without parentheses, causing a syntax error.
Final Answer:
Missing parentheses after np.mean -> Option D
Quick Check:
Functions need parentheses: np.mean(arr) [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
Forgetting parentheses on function calls
Thinking np.mean can't handle arrays
Misreading print syntax as error
5. You have an array of temperatures in Celsius: temps = np.array([0, 20, 37, 100]). You want to convert them to Fahrenheit using the formula F = C * 9/5 + 32. Which NumPy code correctly applies this math function to all elements?
hard
A. fahrenheit = temps * 9 / (5 + 32)
B. fahrenheit = np.add(temps, 32) * 9 / 5
C. fahrenheit = np.multiply(temps, 9/5) + 32
D. fahrenheit = temps + 32 * 9 / 5
Solution
Step 1: Understand the formula and vectorized operations
The formula is F = C * 9/5 + 32. NumPy allows element-wise multiplication and addition.
Step 2: Check each option for correct order and operations