0
0
NumPydata~15 mins

Element-wise arithmetic in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Element-wise arithmetic with NumPy arrays
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use four print() statements, one for each variable.