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.
import numpy as np arr = np.ones((3, 2)) print(arr)
| Step | Action | Input | Intermediate Result | Output |
|---|---|---|---|---|
| 1 | Call np.ones | shape=(3,2) | Prepare empty array of shape (3,2) | Array allocated |
| 2 | Fill array | Array allocated | All elements set to 1 | Array filled with ones |
| 3 | Return array | Array filled with ones | No change | [[1. 1.] [1. 1.] [1. 1.]] |
| 4 | Print array | Array returned | Display array | [[1. 1.] [1. 1.] [1. 1.]] |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| arr | undefined | empty array shape (3,2) | array filled with ones | array filled with ones | array filled with ones |
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.