⬡ Hub
Skip to content

NumPy: Array Creation and Basic Properties

NumPy's core feature is the ndarray object, a powerful N-dimensional array that forms the basis for numerical computing in Python. This document delves deeper into various ways to create these arrays and explore their fundamental properties.

1. Array Creation Routines

NumPy provides a multitude of functions to create arrays, catering to different needs.

From Python Lists or Tuples

The most straightforward way to create an array is from existing Python sequences.

import numpy as np

# From a list
arr_from_list = np.array([1, 2, 3, 4, 5])
print("Array from list:\n", arr_from_list)
print("Type:", type(arr_from_list))

# From a list of lists (for 2D array)
arr_from_list_of_lists = np.array([[1, 2, 3], [4, 5, 6]])
print("\nArray from list of lists:\n", arr_from_list_of_lists)

# From a tuple
arr_from_tuple = np.array((10, 20, 30))
print("\nArray from tuple:\n", arr_from_tuple)

Arrays with Initial Placeholders

These functions create arrays filled with default values, useful for pre-allocating memory.

  • np.zeros(shape, dtype): Creates an array filled with zeros.
  • np.ones(shape, dtype): Creates an array filled with ones.
  • np.empty(shape, dtype): Creates an array without initializing its entries. Its content is random and depends on the state of the memory. This can be faster if you plan to fill the array immediately.
  • np.full(shape, fill_value, dtype): Creates an array filled with a specified value.
import numpy as np

# 1D array of zeros
zeros_1d = np.zeros(5)
print("1D Zeros:\n", zeros_1d)

# 2D array of ones (3 rows, 4 columns)
ones_2d = np.ones((3, 4), dtype=int) # Specify dtype
print("\n2D Ones (int):\n", ones_2d)

# 3D empty array
empty_3d = np.empty((2, 3, 2))
print("\n3D Empty (contents are arbitrary):\n", empty_3d)

# Array filled with a specific value
full_array = np.full((2, 2), 7.0)
print("\nFull array with 7.0:\n", full_array)

Arrays with Numerical Ranges

  • np.arange(start, stop, step, dtype): Returns evenly spaced values within a given interval. Similar to Python's range(), but returns an ndarray.
  • np.linspace(start, stop, num, endpoint, retstep, dtype): Returns evenly spaced numbers over a specified interval. It generates num samples, calculated over the interval [start, stop].
import numpy as np

# Using arange
arr_range = np.arange(0, 10, 2) # Start=0, Stop=10 (exclusive), Step=2
print("Array from arange(0, 10, 2):\n", arr_range)

# Using linspace
arr_linspace = np.linspace(0, 10, 5) # 5 numbers from 0 to 10 (inclusive)
print("\nArray from linspace(0, 10, 5):\n", arr_linspace)

arr_linspace_no_endpoint = np.linspace(0, 10, 5, endpoint=False) # Exclusive end
print("\nArray from linspace(0, 10, 5, endpoint=False):\n", arr_linspace_no_endpoint)

Arrays for Identity Matrices and Diagonal

  • np.identity(n, dtype): Returns the identity matrix of size n x n.
  • np.eye(N, M, k, dtype): Returns a 2-D array with ones on the diagonal and zeros elsewhere. k is the index of the diagonal (0 for main diagonal, positive for super-diagonal, negative for sub-diagonal).
import numpy as np

# Identity matrix
identity_matrix = np.identity(3)
print("Identity Matrix (3x3):\n", identity_matrix)

# Eye matrix
eye_matrix = np.eye(4, 5, k=1) # 4 rows, 5 columns, diagonal starting at 1st super-diagonal
print("\nEye Matrix (4x5, k=1):\n", eye_matrix)

2. Basic Array Properties

Every NumPy array has several important attributes that describe its structure and data.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
print("Array:\n", arr)

# Shape: Tuple of array dimensions (rows, columns, ...)
print("\nShape (dimensions):", arr.shape)

# ndim: Number of dimensions (axes)
print("Number of dimensions (ndim):", arr.ndim)

# size: Total number of elements in the array
print("Total number of elements (size):", arr.size)

# dtype: Data type of the elements in the array
print("Data type (dtype):", arr.dtype)

# itemsize: Size of each element in bytes
print("Size of each element in bytes (itemsize):", arr.itemsize)

# nbytes: Total bytes consumed by the array data
print("Total bytes consumed by data (nbytes):", arr.nbytes)

3. Data Types (dtype)

NumPy supports a much wider range of numerical data types than Python's built-in types. Specifying the dtype is crucial for memory efficiency and ensuring correct numerical behavior.

import numpy as np

# Default integer type (usually int64 on 64-bit systems)
arr_int = np.array([1, 2, 3])
print(f"\narr_int dtype: {arr_int.dtype}")

# Default float type (usually float64)
arr_float = np.array([1.0, 2.5])
print(f"arr_float dtype: {arr_float.dtype}")

# Explicitly specify dtype
arr_int8 = np.array([1, 2, 3], dtype=np.int8)
print(f"arr_int8 dtype: {arr_int8.dtype}")

arr_float32 = np.array([1.0, 2.5], dtype=np.float32)
print(f"arr_float32 dtype: {arr_float32.dtype}")

# Convert dtype of an existing array
arr_converted = arr_float32.astype(np.int32)
print(f"arr_converted dtype: {arr_converted.dtype}, values: {arr_converted}")

Further Topics:

  • Random array creation (np.random)
  • Converting arrays to/from other formats (e.g., Python lists, Pandas Series)
  • Memory layout and strides

Understanding these array creation methods and properties is the first step towards effectively manipulating and analyzing numerical data with NumPy.