Skip to content

NumPy - Array Creation Routines

NumPy provides several functions to create new arrays (ndarray objects) from scratch. These are fundamental building blocks for numerical work.

Creates an array of a given shape and data type without initializing its entries. The values will be whatever happens to be in that memory location at the time.

numpy.empty(shape, dtype=float, order='C')

Parameters:

ParameterDescription
shapeThe desired shape of the array (integer or tuple of integers).
dtypeThe desired data type for the array elements (optional, defaults to float64).
orderMemory layout: ‘C’ for row-major (C-style), ‘F’ for column-major (Fortran-style). Optional, defaults to ‘C’.

Creating an empty 3x2 integer array. Note the arbitrary initial values.

import numpy as np
# Create an empty array with shape (3, 2) and integer type
x = np.empty([3, 2], dtype=int)
print(f"Empty array (values are uninitialized):\n{x}")

Output (values will vary):

Empty array (values are uninitialized):
[[46575369556216 140681880190976]
[ 0 0]
[ 0 0]]

Caution: Use np.empty only when you plan to fill the array immediately afterwards. Relying on the uninitialized values is unsafe.

Creates an array of a given shape and data type, filled entirely with zeros.

numpy.zeros(shape, dtype=float, order='C')

Parameters are the same as np.empty.

import numpy as np
# Array of five zeros. Default dtype is float64.
x = np.zeros(5)
print(f"Zeros array (float default): {x}")
print(f"dtype: {x.dtype}")

Output:

Zeros array (float default): [0. 0. 0. 0. 0.]
dtype: float64
import numpy as np
# Array of five zeros with integer type.
x = np.zeros(5, dtype=int) # Using Python's built-in int is fine here
# Or specify a NumPy type: x = np.zeros(5, dtype=np.int32)
print(f"Zeros array (integer): {x}")
print(f"dtype: {x.dtype}")

Output (dtype might be int64 or int32 depending on system):

Zeros array (integer): [0 0 0 0 0]
dtype: int64

Example 3: Zeros Array with Custom Structured dtype

Section titled “Example 3: Zeros Array with Custom Structured dtype”

NumPy allows creating arrays with structured data types, like C structs.

import numpy as np
# Define a structured dtype with two integer fields 'x' and 'y'
dt = np.dtype([('x', 'i4'), ('y', 'i4')]) # i4 is a 32-bit integer
# Create a 2x2 array using this dtype, initialized to zeros
x = np.zeros((2, 2), dtype=dt)
print(f"Zeros array with structured dtype:\n{x}")
print(f"\nAccessing field 'x':\n{x['x']}")

Output:

Zeros array with structured dtype:
[[(0, 0) (0, 0)]
[(0, 0) (0, 0)]]
Accessing field 'x':
[[0 0]
[0 0]]

Creates an array of a given shape and data type, filled entirely with ones.

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

Parameters are the same as np.zeros. dtype=None defaults to float64.

import numpy as np
# Array of five ones. Default dtype is float64.
x = np.ones(5)
print(f"Ones array (float default): {x}")
print(f"dtype: {x.dtype}")

Output:

Ones array (float default): [1. 1. 1. 1. 1.]
dtype: float64
import numpy as np
# 2x2 array of ones with integer type
x = np.ones([2, 2], dtype=int)
print(f"Ones array (integer):\n{x}")
print(f"dtype: {x.dtype}")

Output:

Ones array (integer):
[[1 1]
[1 1]]
dtype: int64

NumPy also provides empty_like(), zeros_like(), and ones_like(). These create a new array with the same shape and data type as a given input array, but filled with uninitialized values, zeros, or ones, respectively.

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
zeros_arr = np.zeros_like(a)
ones_arr = np.ones_like(a)
print(f"Original array shape: {a.shape}, dtype: {a.dtype}")
print(f"zeros_like array shape: {zeros_arr.shape}, dtype: {zeros_arr.dtype}\n{zeros_arr}")
print(f"ones_like array shape: {ones_arr.shape}, dtype: {ones_arr.dtype}\n{ones_arr}")

Output:

Original array shape: (2, 3), dtype: float32
zeros_like array shape: (2, 3), dtype: float32
[[0. 0. 0.]
[0. 0. 0.]]
ones_like array shape: (2, 3), dtype: float32
[[1. 1. 1.]
[1. 1. 1.]]