NumPy - Array Creation Routines
NumPy - Array Creation Routines
Section titled “NumPy - Array Creation Routines”NumPy provides several functions to create new arrays (ndarray objects) from scratch. These are fundamental building blocks for numerical work.
numpy.empty()
Section titled “numpy.empty()”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:
| Parameter | Description |
|---|---|
shape | The desired shape of the array (integer or tuple of integers). |
dtype | The desired data type for the array elements (optional, defaults to float64). |
order | Memory layout: ‘C’ for row-major (C-style), ‘F’ for column-major (Fortran-style). Optional, defaults to ‘C’. |
Example
Section titled “Example”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 typex = 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.
numpy.zeros()
Section titled “numpy.zeros()”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.
Example 1: Simple Zeros Array
Section titled “Example 1: Simple Zeros Array”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: float64Example 2: Zeros Array with Integer Type
Section titled “Example 2: Zeros Array with Integer Type”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: int64Example 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 zerosx = 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]]numpy.ones()
Section titled “numpy.ones()”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.
Example 1: Simple Ones Array
Section titled “Example 1: Simple Ones Array”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: float64Example 2: Ones Array with Integer Type
Section titled “Example 2: Ones Array with Integer Type”import numpy as np
# 2x2 array of ones with integer typex = 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: int64Related Functions: *_like
Section titled “Related Functions: *_like”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: float32zeros_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.]]