Array arithmetic (element-wise) in Data Analysis Python - Time & Space Complexity
When we do arithmetic on arrays, like adding or multiplying each number one by one, it takes some time. We want to know how this time changes when the array gets bigger.
How does the work grow as the array size grows?
Analyze the time complexity of the following code snippet.
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([10, 20, 30, 40, 50])
result = arr1 + arr2 # element-wise addition
This code adds two arrays by adding each pair of elements at the same position.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each element from the first array to the corresponding element in the second array.
- How many times: Once for each element in the arrays, so as many times as the array length.
When the array size doubles, the number of additions also doubles because each element needs to be added.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows in a straight line with the array size.
Time Complexity: O(n)
This means the time it takes grows directly in proportion to the number of elements in the arrays.
[X] Wrong: "Adding two arrays is instant and does not depend on size."
[OK] Correct: Even though the code looks simple, the computer must add each pair of numbers one by one, so bigger arrays take more time.
Understanding how element-wise operations scale helps you explain efficiency clearly and shows you know how data size affects performance.
"What if we used two-dimensional arrays instead of one-dimensional? How would the time complexity change?"
Practice
Solution
Step 1: Understand element-wise operation
Element-wise means doing the operation on each pair of matching positions in two arrays.Step 2: Apply to addition
For addition, each number in the first array is added to the number in the same position in the second array.Final Answer:
Adding each number in one array to the corresponding number in the other array -> Option BQuick Check:
Element-wise addition = add matching elements [OK]
- Confusing element-wise with total sum
- Thinking arrays are joined instead of added
- Mixing addition with multiplication
a and b element-wise?Solution
Step 1: Recall NumPy addition syntax
NumPy supports element-wise addition using the + operator directly between arrays.Step 2: Check options
c = a + b usesa + b, which is correct. c = a.add(b) is invalid syntax. c = add(a, b) requires importing add function explicitly. c = a & b uses bitwise AND, not addition.Final Answer:
c = a + b -> Option AQuick Check:
Use + for element-wise addition in NumPy [OK]
- Using bitwise operators instead of +
- Trying to call add as a method on array
- Forgetting to import add function if used
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) z = x * y print(z)
Solution
Step 1: Understand element-wise multiplication
Multiplying two arrays with * multiplies each element in the first array by the corresponding element in the second array.Step 2: Calculate each element
1*4=4, 2*5=10, 3*6=18, so the result is [4, 10, 18].Final Answer:
[4 10 18] -> Option CQuick Check:
Element-wise multiply = multiply matching elements [OK]
- Adding instead of multiplying
- Concatenating arrays instead of multiplying
- Confusing output format with list instead of array
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5]) z = x + y print(z)
Solution
Step 1: Check array sizes
Array x has 3 elements, y has 2 elements. Element-wise operations require arrays to be the same size or broadcastable.Step 2: Understand error cause
Since sizes differ and no broadcasting applies here, addition fails with a ValueError.Final Answer:
Arrays have different sizes, so element-wise addition is not possible -> Option DQuick Check:
Array sizes must match for element-wise ops [OK]
- Assuming + works regardless of size
- Thinking np.add fixes size mismatch
- Blaming print syntax for error
store1 = np.array([10, 20, 30, 40]) store2 = np.array([5, 15, 25, 35])
You want to find the total sales per day by adding these arrays element-wise. Then, you want to apply a 10% tax on the total sales. Which code correctly does this?
Solution
Step 1: Calculate total sales element-wise
Add store1 and store2 arrays element-wise using + operator to get daily totals.Step 2: Apply 10% tax
Multiply the total sales array by 1.10 to add 10% tax to each day's total.Final Answer:
total = store1 + store2 final = total * 1.10 print(final) -> Option AQuick Check:
Add then multiply by 1.10 for tax [OK]
- Adding 0.10 instead of multiplying by 1.10
- Multiplying arrays instead of adding
- Dividing instead of multiplying for tax
Ready to prove it?
Pass the test and this pattern moves unknown
