It means performing arithmetic operations on each pair of elements from two arrays of the same shape, producing a new array with the results.
Click to reveal answer
beginner
How do you add two numpy arrays element-wise?
You can use the + operator or the numpy.add() function to add two arrays element-wise.
Click to reveal answer
intermediate
What happens if you try element-wise arithmetic on arrays of different shapes?
Numpy tries to broadcast smaller arrays to match the shape of larger ones if possible; otherwise, it raises an error.
Click to reveal answer
beginner
Show how to multiply two numpy arrays element-wise.
Use the * operator or numpy.multiply() to multiply arrays element-wise.
Click to reveal answer
beginner
Why is element-wise arithmetic useful in data science?
It allows fast and simple operations on datasets represented as arrays, like scaling, combining features, or applying formulas to each data point.
Click to reveal answer
What operator performs element-wise addition of two numpy arrays?
A/
B+
C*
D-
✗ Incorrect
The + operator adds corresponding elements of two arrays.
If array A has shape (3, 2) and array B has shape (3, 2), what is the shape of A * B?
AError
B(2, 3)
C(3, 3)
D(3, 2)
✗ Incorrect
Element-wise multiplication keeps the same shape when arrays have the same shape.
What does numpy do if you add arrays of shapes (3, 1) and (3, 2)?
ARaises an error
BBroadcasts (3, 2) to (3, 1) and adds element-wise
CBroadcasts (3, 1) to (3, 2) and adds element-wise
DReturns a scalar
✗ Incorrect
Numpy broadcasts the smaller array to match the larger shape if compatible.
Which numpy function can be used for element-wise subtraction?
Anumpy.subtract()
Bnumpy.add()
Cnumpy.multiply()
Dnumpy.divide()
✗ Incorrect
numpy.subtract() subtracts elements of one array from another element-wise.
What is the result of element-wise division of two arrays?
AA new array with each element divided by the corresponding element
BA scalar value
CAn error always
DConcatenation of arrays
✗ Incorrect
Element-wise division divides each element of the first array by the corresponding element of the second.
Explain element-wise arithmetic in numpy and why it is useful.
Think about how operations apply to each element in arrays.
You got /3 concepts.
Describe what happens when you perform element-wise operations on arrays of different shapes.
Consider how numpy handles shape differences.
You got /3 concepts.
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.