Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.ones

Creates a new NumPy array of a specified shape, filled with ones. By default it uses float64 values; dtype and memory order can be selected.

How it works

Call np.ones with either separate dimension arguments or a tuple of dimensions. It allocates and returns a new ndarray, fills every element with one, uses float64 unless dtype is specified, and can control multidimensional memory layout with order.

Syntax

numpy.ones(shape, dtype=None, order='C', *, device=None, like=None)

Parameters

NameTypeDescription
shapeint or sequence of intsDimensions of the new array, given as an integer or sequence of integers.
dtypedata-type, optionalElement data type; defaults to numpy.float64.
order{'C', 'F'}, optionalMemory layout for multidimensional arrays: C or F; defaults to C.
likearray_like, optionalOptional object implementing the array protocol; the returned array is created through that protocol.

Returns

An ndarray containing ones with the requested shape and dtype.

Key points

  • The function allocates a fresh ndarray and initializes every element to one.
  • Shape may be supplied as one integer or as a sequence describing multiple dimensions.
  • Use dtype to select the element type, including a specific NumPy scalar type.
  • The default memory order is C; F can be selected for Fortran-style layout.

Common mistakes

  • Without dtype, the returned values are floating-point rather than integers.
  • The order option affects multidimensional memory layout; choose C or F deliberately when layout matters.

Example: Create a typed matrix of ones

import numpy as np

weights = np.ones((2, 3), dtype=np.float32)
print(weights)

Creates a 2×3 floating-point array whose six elements are initialized to 1.

When to use it

Preallocate a uniform array

Initialize weights, masks, counters, or other numeric buffers before computation.

Control the element type with dtype

Create integer-valued dimensions or indicator data.

Choose C or Fortran ordering

Prepare arrays for libraries that expect a specific multidimensional memory layout.

Interview questions

QWhat does np.ones return, and what dtype does it use by default?

np.ones creates a new array with the requested shape and fills it with ones. Its default dtype is numpy.float64, so integer output requires an explicit dtype such as int.

Follow-up: How would you create a 2-by-3 integer array? Pass (2, 3) as the shape and dtype=int.

QHow is the shape argument specified?

The shape can be one integer for a one-dimensional array or a sequence of integers for multiple dimensions. For example, np.ones(5) has shape (5,), while np.ones((2, 3)) has two dimensions.

Follow-up: What shape argument creates a three-dimensional array with dimensions 2, 4, and 5? Use (2, 4, 5).

QWhat does the order parameter control?

order accepts C or F and controls how multidimensional data is stored in memory. C selects row-major storage, while F selects column-major storage.

Follow-up: When is order especially relevant? When interoperating with code that expects a particular memory layout or optimizing multidimensional array access.

  • numpy.zeros
  • numpy.empty
  • numpy.full
  • numpy.ones_like
  • numpy.asarray

Source: numpy.ones — NumPy v2.3 Manual. Checked 2026-10-15.