NumPy Built-in Array Creation

Last Updated: 09 Nov 2025


NumPy provides ready-made functions to create arrays without manually typing elements. These functions help you make arrays filled with zeros, ones, specific numbers, identity matrices, or random valuescritical in real-world ML, robotics, and scientific code.


Array Filled with Zeros np.zeros(shape, dtype)

Creates an array of all zeros. Use shape= for clarity.

import numpy as np

a = np.zeros(shape=5)
b = np.zeros(shape=(2, 3))

print(a)
print(b)
c = np.zeros(shape=5, dtype=int)
print(c)

Array Filled with Ones np.ones(shape, dtype)

Creates an array of all ones.

arr = np.ones(shape=(3, 2))
print(arr)

arr = np.ones(shape=(3, 2), dtype=int)
print(arr)

Array Filled with a Constant np.full(shape, fill_value)

Creates an array filled with a specific value.

arr = np.full(shape=(3, 2), fill_value=5)
print(arr)

# Sentinel for missing data
missing = np.full(shape=(4,), fill_value=-999.0, dtype=np.float32)
print(missing)

Pro: -999 is standard in weather and sensor data.


Sequence with Steps np.arange(start, stop, step)

Like Python’s range(), but returns a NumPy array.

arr = np.arange(1, 10, 2)
print(arr)  # [1 3 5 7 9]

Use Case: Frame indices in video processing.


Evenly Spaced Values np.linspace(start, stop, num)

Creates exactly num evenly spaced values from start to stop (inclusive).

arr = np.linspace(0, 1, 5)
print(arr)  # [0.   0.25 0.5  0.75 1.  ]

Plotting & Physics: Time vectors, signal sampling.


Square Identity Matrix np.identity(n, dtype)

Creates n×n identity matrix. Always returns float64 by default.

arr = np.identity(3)
print(arr)
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

# Force integer
arr_int = np.identity(3, dtype=int)
print(arr_int)

Warning: np.identity ignores dtype=float → use int explicitly.


Flexible Identity Matrix np.eye(N, M=None, k=0, dtype)

Like identity, but allows non-square and offset diagonal.

arr = np.eye(3)
print(arr)

# Super-diagonal (k=1)
arr = np.eye(3, k=1, dtype=int)
print(arr)
# [[0 1 0]
#  [0 0 1]
#  [0 0 0]]

# Sub-diagonal (k=-1)
arr = np.eye(3, k=-1, dtype=int)
print(arr)
# [[0 0 0]
#  [1 0 0]
#  [0 1 0]]

Uninitialized Array np.empty(shape, dtype)

Creates array without initializingfaster, but values are garbage.

arr = np.empty(shape=(2, 3))
print(arr)  # Random values from memory!

Speed vs Safety:

  • np.zeros: ~10ms for 10M elements
  • np.empty: ~1ms
    Use empty in real-time systems (LiDAR, robotics).

Random Array Creation (np.random)

Uniform [0, 1) np.random.rand(d0, d1, ...)

np.random.rand(3)        # 1D
np.random.rand(2, 3)     # 2D

Normal (mean=0, std=1) np.random.randn(d0, d1, ...)

arr = np.random.randn(4)
print(arr)

Random Integers np.random.randint(low, high, size)

arr = np.random.randint(1, 10, 5)
print(arr)

Random Sampling np.random.choice(a, size, replace)

data = np.array([10, 20, 30, 40, 50])
print(np.random.choice(data, 3))  # With replacement

Modifies in place np.random.shuffle(x)

data = np.array([10, 20, 30, 40, 50])
np.random.shuffle(data)
print(data)  # Order changed!

Returns copy np.random.permutation(x)

data = np.array([10, 20, 30, 40, 50])
print(np.random.permutation(data))  # Original unchanged

Reproducible Results np.random.seed(seed)

np.random.seed(42)
arr = np.random.randint(1, 10, 5)
print(arr)  # Always [6 3 7 4 6]