📖 Scenario: Imagine you have two lists of daily temperatures from two different cities. You want to compare them by doing element-wise arithmetic operations like addition, subtraction, multiplication, and division.
🎯 Goal: You will create two NumPy arrays with exact temperature values, set a scaling factor, perform element-wise arithmetic operations between the arrays and the scaling factor, and finally print the results.
📋 What You'll Learn
Use NumPy arrays for the temperature data
Create a scaling factor variable
Perform element-wise addition, subtraction, multiplication, and division
Print the resulting arrays
💡 Why This Matters
🌍 Real World
Element-wise arithmetic is useful when comparing or combining data from different sources, like temperatures from multiple cities or sales data from different stores.
💼 Career
Data scientists often use element-wise operations to preprocess data, create new features, or analyze relationships between datasets efficiently.
Progress0 / 4 steps
1
Create two NumPy arrays with temperature data
Import NumPy as np. Create a NumPy array called city1_temps with these exact values: [20, 22, 19, 24, 21]. Create another NumPy array called city2_temps with these exact values: [18, 21, 20, 23, 22].
NumPy
Hint
Use np.array([...]) to create NumPy arrays with the exact values given.
2
Set a scaling factor
Create a variable called scale and set it to the exact value 2.
NumPy
Hint
Just assign the number 2 to a variable named scale.
3
Perform element-wise arithmetic operations
Create four new variables: added, subtracted, multiplied, and divided. Set added to the element-wise sum of city1_temps and city2_temps. Set subtracted to the element-wise difference of city1_temps minus city2_temps. Set multiplied to the element-wise product of city1_temps and scale. Set divided to the element-wise division of city2_temps by scale.
NumPy
Hint
Use +, -, *, and / operators between arrays and variables for element-wise operations.
4
Print the results
Print the variables added, subtracted, multiplied, and divided each on a separate line.
NumPy
Hint
Use four print() statements, one for each variable.
Practice
(1/5)
1. What does element-wise arithmetic mean in numpy?
easy
A. Adding arrays only if they have the same number of elements
B. Applying operations to each pair of elements in arrays individually
C. Multiplying the sum of all elements in two arrays
D. Combining arrays into a single list
Solution
Step 1: Understand element-wise operation
Element-wise arithmetic means applying the operation to each corresponding element in two arrays.
Step 2: Compare options
Only Applying operations to each pair of elements in arrays individually correctly describes this as applying operations to each pair of elements individually.
Final Answer:
Applying operations to each pair of elements in arrays individually -> Option B
Quick Check:
Element-wise = operation on each element pair [OK]
Hint: Think: operation applies to each element pair separately [OK]
Common Mistakes:
Confusing element-wise with whole-array operations
Thinking arrays must be identical length only
Assuming element-wise means concatenation
2. Which of the following is the correct syntax to add two numpy arrays a and b element-wise?
easy
A. np.sum(a, b)
B. a.add(b)
C. a + b
D. a.append(b)
Solution
Step 1: Check numpy addition methods
The + operator performs element-wise addition in numpy.
Step 2: Evaluate options
a.add(b) is invalid because numpy arrays do not have an add method. np.sum(a, b) computes sums, not element-wise addition. a.append(b) is a list method and errors on arrays.
Final Answer:
a + b -> Option C
Quick Check:
+ operator adds arrays element-wise [OK]
Hint: Use + for element-wise addition [OK]
Common Mistakes:
Using methods that don't exist on numpy arrays
Trying to add arrays with Python list methods
Confusing element-wise addition with matrix multiplication
3. What is the output of this code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x * y)
medium
A. [4 10 18]
B. [5 7 9]
C. [1 2 3 4 5 6]
D. Error: arrays must be same shape
Solution
Step 1: Understand element-wise multiplication
Multiplying two numpy arrays multiplies each element pair: 1*4=4, 2*5=10, 3*6=18.