NumPy Array Initialization Functions
NumPy provides a collection of built-in functions that make it easy to create arrays of any shape or structure. These functions allow you to quickly generate arrays filled with specific values, such as zeros, ones, or identity matrices — without manually defining every element.
Whether you're initializing arrays for mathematical operations, setting up input data for a model, or creating placeholder structures, these tools can help you do it efficiently and consistently.
- np.zeros(): Create arrays filled with zeros.
- np.ones(): Create arrays filled with ones.
- np.full(): Create arrays filled with a specific value.
- np.empty(): Create uninitialized arrays (faster, but values may be garbage).
- np.eye(): Create 2D identity matrices.
- np.identity(): Another way to create identity matrices.
Creating Arrays Filled with Zeros
The np.zeros() function allows you to create an array filled entirely with zeros. You simply pass in the desired shape of the array as a tuple. This is useful for initializing arrays where you plan to later fill in values or use the array as a placeholder.
import numpy as np
# Create a 1D array of 5 zeros
arr1 = np.zeros(5)
print("1D:", arr1)
# Create a 2D array (3 rows, 4 columns) of zeros
arr2 = np.zeros((3, 4))
print("2D:", arr2)
import numpy as np
# Create a 1D array of 5 zeros
arr1 = np.zeros(5)
print("1D:", arr1)
# Create a 2D array (3 rows, 4 columns) of zeros
arr2 = np.zeros((3, 4))
print("2D:", arr2)
How It Works:
- np.zeros(5) creates a 1D array with 5 zeros.
- np.zeros((3, 4)) creates a 2D array with 3 rows and 4 columns.
- The shape must be provided as a single integer (for 1D) or a tuple (for multi-dimensional arrays).
Output
1D: [0. 0. 0. 0. 0.]
2D: [[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
1D: [0. 0. 0. 0. 0.]
2D: [[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
By default, np.zeros() creates arrays with the float data type. You can specify a different data type using the dtype argument.
arr_int = np.zeros((2, 3), dtype=int)
print(arr_int)
arr_int = np.zeros((2, 3), dtype=int)
print(arr_int)
Output
[[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]]
💡 Tip: Use np.zeros_like() to create a zero-filled array with the same shape and type as an existing array.
Creating Arrays Filled with Ones
The np.ones() function creates an array where every element is set to one. Like np.zeros(), you specify the shape of the array as either an integer (for 1D) or a tuple (for multi-dimensional arrays).
import numpy as np
# Create a 1D array of 4 ones
arr1 = np.ones(4)
print("1D:", arr1)
# Create a 2D array (2 rows, 3 columns) of ones
arr2 = np.ones((2, 3))
print("2D:", arr2)
import numpy as np
# Create a 1D array of 4 ones
arr1 = np.ones(4)
print("1D:", arr1)
# Create a 2D array (2 rows, 3 columns) of ones
arr2 = np.ones((2, 3))
print("2D:", arr2)
How It Works:
- np.ones(4) creates a 1D array with 4 elements, all set to one.
- np.ones((2, 3)) creates a 2D array with 2 rows and 3 columns.
- You can control the number of dimensions using a shape tuple.
Output
1D: [1. 1. 1. 1.]
2D: [[1. 1. 1.]
[1. 1. 1.]]
1D: [1. 1. 1. 1.]
2D: [[1. 1. 1.]
[1. 1. 1.]]
Like np.zeros(), the default data type is float. If you need integers or another type, use the dtype parameter.
arr_int = np.ones((3, 2), dtype=int)
print(arr_int)
arr_int = np.ones((3, 2), dtype=int)
print(arr_int)
Output
[[1 1]
[1 1]
[1 1]]
[[1 1]
[1 1]
[1 1]]
💡 Tip: Use np.ones_like() to create a ones-filled array with the same shape and type as an existing array.
Creating Arrays Filled with a Specific Value
The np.full() function creates an array of a given shape, where every element is set to a specific constant value you choose. This is useful when you need to initialize an array with a repeated value other than zero or one.
import numpy as np
# Create a 1D array filled with the value 7
arr1 = np.full(5, 7)
# Create a 2D array (3 rows, 2 columns) filled with 3.14
arr2 = np.full((3, 2), 3.14)
print("1D:", arr1)
print("2D:", arr2)
import numpy as np
# Create a 1D array filled with the value 7
arr1 = np.full(5, 7)
# Create a 2D array (3 rows, 2 columns) filled with 3.14
arr2 = np.full((3, 2), 3.14)
print("1D:", arr1)
print("2D:", arr2)
How It Works:
- np.full(5, 7) creates a 1D array with 5 elements, all set to 7.
- np.full((3, 2), 3.14) creates a 3×2 array with all elements set to 3.14.
- The first argument is the shape, and the second is the value to fill with.
Output
1D: [7 7 7 7 7]
2D: [[3.14 3.14]
[3.14 3.14]
[3.14 3.14]]
1D: [7 7 7 7 7]
2D: [[3.14 3.14]
[3.14 3.14]
[3.14 3.14]]
💡 Tip: np.full_like() lets you fill a new array with a specific value, matching the shape and type of an existing array.
Creating Uninitialized Arrays
The np.empty() function creates a new array of the specified shape and data type, but does not initialize its values. This means the array will contain whatever data happens to already exist at that location in memory — often referred to as "garbage values."
Because it skips value initialization, np.empty() can be slightly faster than functions like np.zeros() or np.ones(). It's useful in performance-sensitive scenarios where you'll immediately overwrite the contents.
import numpy as np
# Create an uninitialized 1D array of length 4
arr1 = np.empty(4)
# Create an uninitialized 2D array (2 rows, 3 columns)
arr2 = np.empty((2, 3))
print("1D:", arr1)
print("2D:", arr2)
import numpy as np
# Create an uninitialized 1D array of length 4
arr1 = np.empty(4)
# Create an uninitialized 2D array (2 rows, 3 columns)
arr2 = np.empty((2, 3))
print("1D:", arr1)
print("2D:", arr2)
Important Notes:
- np.empty() does not fill the array with zeros or any fixed value.
- The contents will be random and based on memory state — don’t use without immediately assigning new values.
- Use this function only when performance matters and you will overwrite the values right after creation.
Sample Output
1D: [1.48219694e-323 0.00000000e+000 6.94330657e-310 6.94330837e-310]
2D: [[0.00000000e+000 0.00000000e+000 6.94330837e-310]
[6.94330837e-310 0.00000000e+000 0.00000000e+000]]
1D: [1.48219694e-323 0.00000000e+000 6.94330657e-310 6.94330837e-310]
2D: [[0.00000000e+000 0.00000000e+000 6.94330837e-310]
[6.94330837e-310 0.00000000e+000 0.00000000e+000]]
⚠️ Warning: Always initialize or overwrite the array before using it in calculations.
Creating Identity Matrices with np.eye()
The np.eye() function creates a identity matrix — a square matrix with ones on the main diagonal and zeros elsewhere. Identity matrices are important in linear algebra, as they act like the number 1 for matrix multiplication.
import numpy as np
# Create a 3x3 identity matrix
identity_3x3 = np.eye(3)
# Create a 4x4 identity matrix with float datatype
identity_4x4 = np.eye(4, dtype=float)
print(identity_3x3)
print(identity_4x4)
import numpy as np
# Create a 3x3 identity matrix
identity_3x3 = np.eye(3)
# Create a 4x4 identity matrix with float datatype
identity_4x4 = np.eye(4, dtype=float)
print(identity_3x3)
print(identity_4x4)
How It Works:
- np.eye(3) creates a 3×3 matrix with ones on the diagonal and zeros elsewhere.
- The argument specifies the number of rows and columns (square matrix).
- You can specify the data type with the dtype parameter.
- Optional parameters include M for number of columns (to create rectangular matrices) and k for shifting the diagonal.
Output
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
you can create a rectangular identity matrix, specify the number of columns using the M parameter, like np.eye(3, 5).
arr = np.eye(3, 4)
print(arr)
arr = np.eye(3, 4)
print(arr)
Output
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]]
Creating Identity Matrices with np.identity()
The np.identity() function creates a square identity matrix — a 2D array with ones on the main diagonal and zeros elsewhere. It’s a simpler alternative to np.eye() when you only need a square identity matrix.
import numpy as np
# Create a 4x4 identity matrix
identity_matrix = np.identity(4)
print(identity_matrix)
import numpy as np
# Create a 4x4 identity matrix
identity_matrix = np.identity(4)
print(identity_matrix)
How It Works:
- np.identity(4) creates a 4×4 identity matrix.
- Only accepts one argument — the number of rows and columns (square matrix).
- Defaults to float data type, but you can specify dtype if needed.
Output
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
💡 Tip: Use np.identity() when you need only square identity matrix without extra options.
Frequently Asked Questions
What does np.zeros() do in NumPy?
What does np.zeros() do in NumPy?
np.zeros() creates a new array of the specified shape and fills it with zeros.
How is np.ones() different from np.zeros()?
How is np.ones() different from np.zeros()?
np.ones() creates an array filled with ones instead of zeros. Both functions accept the same shape and dtype arguments.
What is the purpose of np.full()?
What is the purpose of np.full()?
np.full() creates a NumPy array of a given shape and fills it with a specified constant value.
What does np.empty() return?
What does np.empty() return?
np.empty() returns a new array of the given shape without initializing its values. The contents are arbitrary.
When should I use np.eye() vs np.identity()?
When should I use np.eye() vs np.identity()?
Both np.eye() and np.identity() create identity matrices, but np.eye() allows specifying off-diagonals, making it more flexible for some use cases.
What's Next?
In the next section, we’ll dive into different ways to generate NumPy arrays using np.arange
, np.linspace
, and np.logspace
. These functions help you create arrays with evenly spaced values — whether linear or logarithmic — making array creation flexible and powerful.