0
0
NumPydata~10 mins

np.ones() for one-filled arrays in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.ones() for one-filled arrays
Call np.ones(shape)
Create new array of given shape
Fill all elements with 1
Return one-filled array
np.ones() creates a new array with the shape you want, filling every spot with the number 1.
Execution Sample
NumPy
import numpy as np
arr = np.ones((3, 2))
print(arr)
This code makes a 3x2 array filled with ones and prints it.
Execution Table
StepActionInputIntermediate ResultOutput
1Call np.onesshape=(3,2)Prepare empty array of shape (3,2)Array allocated
2Fill arrayArray allocatedAll elements set to 1Array filled with ones
3Return arrayArray filled with onesNo change[[1. 1.] [1. 1.] [1. 1.]]
4Print arrayArray returnedDisplay array[[1. 1.] [1. 1.] [1. 1.]]
💡 Array created and filled with ones, then printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrundefinedempty array shape (3,2)array filled with onesarray filled with onesarray filled with ones
Key Moments - 3 Insights
Why does np.ones need the shape as a tuple?
np.ones uses the shape tuple to know how many rows and columns to create. See execution_table step 1 where shape=(3,2) leads to an array of 3 rows and 2 columns.
Does np.ones fill with integer 1 or float 1.0 by default?
By default, np.ones fills with float 1.0. This is shown in the output array where elements appear as 1.0 (see execution_table step 3 output).
Can np.ones create arrays with more than 2 dimensions?
Yes, np.ones can create arrays of any shape, including 3D or more, by passing a tuple with more numbers. The flow is the same as shown in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the shape of the array after Step 1?
A(3, 2)
B(2, 3)
C(1, 3)
D(3, 3)
💡 Hint
Check the 'Input' column in Step 1 where shape=(3,2) is specified.
At which step does the array get filled with ones?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Intermediate Result' columns in the execution_table.
If you want an integer array of ones, what should you do?
AUse np.ones with shape as int
BUse np.ones with dtype=int
CUse np.ones without arguments
DUse np.ones with float dtype
💡 Hint
Recall that default dtype is float, so to get integers, specify dtype=int.
Concept Snapshot
np.ones(shape, dtype=float) creates a new array of given shape filled with 1.0 by default.
Shape is a tuple like (rows, columns).
Use dtype=int to get integer ones.
Useful to initialize arrays with all ones quickly.
Full Transcript
np.ones() is a function in numpy that makes a new array filled with ones. You give it the shape you want as a tuple, like (3, 2) for 3 rows and 2 columns. It first creates an empty array of that shape, then fills every element with the number 1. By default, these ones are floats (1.0). You can change the type by using the dtype parameter. After creating the array, you can use it for calculations or print it to see the one-filled array.