np.round(), np.floor(), np.ceil() in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run np.round(), np.floor(), and np.ceil() changes as the input size grows.
How does the number of elements affect the work these functions do?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.random.rand(n) * 100
rounded = np.round(arr)
floored = np.floor(arr)
ceiled = np.ceil(arr)
This code creates an array of size n with random numbers, then applies rounding, flooring, and ceiling operations element-wise.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the rounding, floor, or ceiling function to each element in the array.
- How many times: Once for each element, so n times.
As the array size grows, the number of operations grows in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1000 operations |
Pattern observation: Doubling the input size roughly doubles the work done.
Time Complexity: O(n)
This means the time to run these functions grows linearly with the number of elements in the array.
[X] Wrong: "These functions run in constant time no matter the array size because they are built-in."
[OK] Correct: Even built-in functions must process each element, so the total time depends on how many elements there are.
Understanding how simple element-wise operations scale helps you explain performance in data processing tasks clearly and confidently.
"What if we applied np.round() only to a fixed number of elements regardless of array size? How would the time complexity change?"
Practice
np.floor() do to a decimal number?Solution
Step 1: Understand the behavior of np.floor()
It always rounds any decimal number down to the nearest whole number, regardless of the decimal part.Step 2: Compare with other rounding functions
Unlike np.ceil() which rounds up, np.floor() always rounds down.Final Answer:
Rounds the number down to the nearest whole number -> Option AQuick Check:
np.floor(3.7) = 3 [OK]
- Confusing floor with ceil
- Thinking floor rounds to nearest integer
- Assuming floor changes only if decimal > 0.5
arr = np.array([1.2, 2.5, 3.7]) to 1 decimal place using np.round()?Solution
Step 1: Check np.round() parameter for decimals
The parameter to specify decimal places is named 'decimals' and expects an integer.Step 2: Validate each option
A: decimals=0.1 invalid (must be integer). B: np.round(arr, 0) rounds to 0 decimal places. C: np.round(arr, decimals=1) correct. D: 'places' not a valid parameter.Final Answer:
np.round(arr, decimals=1) -> Option BQuick Check:
np.round(arr, decimals=1) rounds to 1 decimal [OK]
- Using wrong parameter name like 'places'
- Passing float instead of int for decimals
- Omitting decimals parameter and expecting decimal rounding
import numpy as np arr = np.array([1.7, 2.3, 3.5]) result = np.ceil(arr) print(result)
Solution
Step 1: Understand np.ceil() behavior on each element
np.ceil() rounds each number up to the nearest whole number: 1.7 -> 2, 2.3 -> 3, 3.5 -> 4.Step 2: Apply np.ceil() to the array
The resulting array is [2., 3., 4.].Final Answer:
[2. 3. 4.] -> Option AQuick Check:
np.ceil rounds decimals up [OK]
- Confusing ceil with floor
- Expecting rounding to nearest integer
- Ignoring decimal part in output
import numpy as np arr = np.array([1.2, 2.5, 3.7]) result = np.round(arr, decimal=1) print(result)
Solution
Step 1: Identify the parameter name error
The correct parameter name for decimal places in np.round() is 'decimals', not 'decimal'.Step 2: Confirm np.round() usage
np.round() works on numpy arrays and accepts an integer for decimals parameter.Final Answer:
The parameter name should be 'decimals' not 'decimal' -> Option DQuick Check:
Use decimals=1, not decimal=1 [OK]
- Using 'decimal' instead of 'decimals'
- Thinking np.round() only works on scalars
- Passing string instead of integer for decimals
arr = np.array([1.25, 2.75, 3.5, 4.1]). You want to round each number to the nearest integer but always round .5 up. Which combination of numpy functions will achieve this?Solution
Step 1: Understand rounding .5 up behavior
Standard np.round() rounds .5 to nearest even integer, not always up.Step 2: Use floor with offset to force .5 up rounding
Adding 0.5 then applying np.floor() rounds numbers so that .5 always rounds up.Step 3: Check example
1.25 + 0.5 = 1.75 floor = 1, 2.75 + 0.5 = 3.25 floor = 3, 3.5 + 0.5 = 4.0 floor = 4, 4.1 + 0.5 = 4.6 floor = 4.Final Answer:
Use np.floor(arr + 0.5) -> Option CQuick Check:
Floor after adding 0.5 rounds .5 up [OK]
- Using np.round() which rounds .5 to nearest even
- Using ceil incorrectly with subtraction
- Ignoring .5 rounding behavior
