BetterDocs
Home
Docs

Creation | np.ones()

Method:

np.ones(shape, dtype=np.float64, order='C', *, like=None)

Creates a NumPy array of the entered shape filled with ones.

Returns:

np.ndarray

Parameters:

shape: (list, tuple)-

Specifies the dimensions of the array to be filled with ones.

import numpy as np
# (row, col) or [row, col] to be fed as shape
ones_array = np.ones(shape=[2, 3])
print(ones_array)
'''
Output:
[[1. 1. 1.]
 [1. 1. 1.]]
'''

dtype: np.float64, Optional-

The desired data-type for the array.

import numpy as np
# (row, col) or [row, col] to be fed as shape
ones_array = np.ones([2, 3], dtype=np.int8)
print(ones_array.dtype) # int8

Values: +

By passing the dtype, you are implicitly converting the values to the passed dtype.

order: {'C', 'F'}, Optional-

Specifies the memory layout order for multi-dimensional arrays.

order = C (default) +

order = F +

Refer to np.array()  for better explanation & examples for order.

.ravel()  is a NumPy function used to flatten a multi-dimensional array into a 1D array.

like: None, Optional-

The like parameter in np.ones() was introduced to make array creation more flexible, particularly for compatibility with libraries that extend NumPy, like Dask, CuPy, or other libraries that create arrays compatible with np.ndarray but optimized for different backends (e.g., parallel processing or GPU-based arrays).


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy