np.ones() for one-filled arrays in NumPy - Time & Space Complexity
We want to understand how the time to create an array filled with ones changes as the array size grows.
How does the work needed grow when we ask for bigger arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000
ones_array = np.ones(n)
This code creates a one-dimensional array of length n filled with ones.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filling each element of the array with the value 1.
- How many times: Once for each element, so
ntimes.
As the array size n grows, the time to fill the array grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows directly with the size of the array.
Time Complexity: O(n)
This means the time to create the array grows in a straight line with the number of elements.
[X] Wrong: "Creating an array with np.ones() takes the same time no matter the size."
[OK] Correct: The function must fill each element, so bigger arrays take more time.
Understanding how array creation scales helps you reason about data preparation steps in real projects.
What if we changed np.ones(n) to np.ones((n, n))? How would the time complexity change?
Practice
np.ones() function do in NumPy?Solution
Step 1: Understand the function purpose
np.ones()is designed to create arrays filled with the number one.Step 2: Compare with other array creation functions
Functions likenp.zeros()create arrays filled with zeros, butnp.ones()specifically fills with ones.Final Answer:
Creates an array filled with ones -> Option AQuick Check:
np.ones()= array of ones [OK]
- Confusing np.ones() with np.zeros()
- Thinking it creates random values
- Assuming it creates an empty array
np.ones()?Solution
Step 1: Check the shape argument format
The shape must be given as a tuple, so (2, 3) is correct.Step 2: Check the dtype argument syntax
The correct keyword isdtype, and the value can beintor'int'.Final Answer:
np.ones((2, 3), dtype=int) -> Option AQuick Check:
Shape as tuple and dtype keyword correct [OK]
- Using list instead of tuple for shape
- Using 'type' instead of 'dtype'
- Passing shape as separate arguments
import numpy as np arr = np.ones((2, 2), dtype=float) print(arr)
Solution
Step 1: Understand the shape and dtype
The shape is (2, 2), so the array has 2 rows and 2 columns. The dtype is float, so elements are floats.Step 2: Predict the printed output
Each element is 1.0, printed as 1. with a decimal point. So the output is a 2x2 array of 1.0 values.Final Answer:
[[1. 1.] [1. 1.]] -> Option BQuick Check:
Shape (2,2) and float type means 1.0 values [OK]
- Confusing integer and float output formats
- Misreading shape as (2,3)
- Expecting zeros instead of ones
import numpy as np arr = np.ones(3, 4) print(arr)
Solution
Step 1: Check the shape argument
The code passes two separate arguments 3 and 4, butnp.ones()expects a single shape argument as a tuple.Step 2: Correct the shape argument
Usenp.ones((3, 4))to create a 3 by 4 array of ones.Final Answer:
Shape must be a tuple, so use np.ones((3, 4)) -> Option DQuick Check:
Shape argument must be tuple [OK]
- Passing shape as separate arguments
- Forgetting parentheses around shape tuple
- Thinking dtype is mandatory
np.ones(). Which code snippet correctly does this?Solution
Step 1: Confirm the shape argument format
The shape must be a tuple (2, 3, 4) to create a 3D array.Step 2: Use correct dtype keyword and value
The dtype keyword must bedtypeand the valueboolto create a boolean array.Final Answer:
np.ones((2, 3, 4), dtype=bool) -> Option CQuick Check:
Tuple shape and dtype=bool correct [OK]
- Using list instead of tuple for shape
- Using 'type' instead of 'dtype'
- Passing shape as separate arguments
Ready to prove it?
Pass the test and this pattern moves unknown
