🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.ones()

AI & DATA SCIENCE // np-ones

np.ones() creates a new array of a given shape, filled entirely with the value 1.

Syntax

np.ones(shape, dtype=float)

Deep Dive Course

np.ones() works identically to np.zeros(), but fills every element with 1 instead of 0. It's commonly used to initialize weights or bias terms in simple numerical models, to build a mask that starts as 'everything included' before selectively zeroing entries out, or combined with scalar multiplication to build an array pre-filled with a constant other than 0 or 1, though np.full() is the more direct way to do that.

1Understanding np.ones()

np.ones() works identically to np.zeros(), but fills every element with 1 instead of 0. It's commonly used to initialize weights or bias terms in simple numerical models, to build a mask that starts as 'everything included' before selectively zeroing entries out, or combined with scalar multiplication to build an array pre-filled with a constant other than 0 or 1, though np.full() is the more direct way to do that.

💡

Multiplying np.ones(shape) by a scalar is a common but slightly indirect way to fill an array with a constant value — np.full(shape, value) expresses the same intent more directly and without the extra multiplication.

editor.html
import numpy as np

arr = np.ones(4)
print(arr)
localhost:3000

2Practical Example

Here is a real-world application of np.ones() showing how it is used in production NumPy code.

editor.html
import numpy as np

mask = np.ones((3, 3), dtype=bool)
mask[1, 1] = False
print(mask)
localhost:3000

3Best Practices

Follow these guidelines when working with np.ones():

1. Use np.ones() to initialize arrays that need an all-true or all-included starting state, like a mask before selectively disabling entries

2. Prefer np.full(shape, value) over np.ones(shape) times value when the fill value isn't literally 1

3. Specify dtype explicitly when the array represents something other than floating-point weights, like a boolean or integer mask

⚠️

Tip: Multiplying np.ones(shape) by a scalar is a common but slightly indirect way to fill an array with a constant value — np.full(shape, value) expresses the same intent more directly and without the extra multiplication.

editor.html
import numpy as np

arr = np.ones(4)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.ones(4)
print(arr)
Example 02Advanced Example
import numpy as np

mask = np.ones((3, 3), dtype=bool)
mask[1, 1] = False
print(mask)

Best Practices

  • Use np.ones() to initialize arrays that need an all-true or all-included starting state, like a mask before selectively disabling entries
  • Prefer np.full(shape, value) over np.ones(shape) times value when the fill value isn't literally 1
  • Specify dtype explicitly when the array represents something other than floating-point weights, like a boolean or integer mask

Interview Question

Why might np.full(shape, 7) be preferred over np.ones(shape) times 7 for filling an array with the value 7?

Hint: Think about how many array operations each approach actually performs.

np.ones(shape) times 7 first allocates and fills an entire array with 1s, then performs a second full pass over the array multiplying every element by 7, essentially doing twice the work and creating an intermediate array that's immediately discarded. np.full(shape, 7) directly allocates the array and fills it with 7 in a single step, making it both more efficient and more directly expressive of the actual intent.

Exercises

MediumPractice using np.ones() in a real scenario.
View Solution
import numpy as np

arr = np.ones(4)
print(arr)

Frequently Asked Questions

Why might np.full(shape, 7) be preferred over np.ones(shape) times 7 for filling an array with the value 7?

np.ones(shape) times 7 first allocates and fills an entire array with 1s, then performs a second full pass over the array multiplying every element by 7, essentially doing twice the work and creating an intermediate array that's immediately discarded. np.full(shape, 7) directly allocates the array and fills it with 7 in a single step, making it both more efficient and more directly expressive of the actual intent.

Related Functions

np-zerosnp-fullnp-empty