NumPy provides multiple efficient methods for creating arrays, each suited to different use cases and data sources. This article covers the most commonly used techniques for creating NumPy arrays, along with when and why to use each method.
Create Numpy Arrays Using Lists or Tuples
The simplest way to create a NumPy array is by passing a Python list or tuple to the numpy.array() function. This method creates a one-dimensional array.
import numpy as np
l = [1, 2, 3, 4, 5]
a = np.array(l)
print("Simple NumPy Array:",a)
Output
Simple NumPy Array: [1 2 3 4 5]
Initialize a Python NumPy Array Using Special Functions
NumPy provides several built-in functions to generate arrays with specific properties.
- np.zeros(): Creates an array filled with zeros.
- np.ones(): Creates an array filled with ones.
- np.full(): Creates an array filled with a specified value.
- np.arange(): Creates an array with values that are evenly spaced within a given range.
- np.linspace(): Creates an array with values that are evenly spaced over a specified interval.
import numpy as np
a0 = np.zeros((2, 3))
a1 = np.ones((3, 3))
af = np.full((2, 2), 7)
ar = np.arange(0, 10, 2) # start, stop, step
la = np.linspace(0, 1, 5) # start, stop, num
print("Zero Array:","\n",a0)
print("Ones Array:","\n",a1)
print("Constant Array:","\n",af)
print("Range Array:","\n",ar)
print("Linspace Array:","\n",la)
Output
Zero Array: [[0. 0. 0.] [0. 0. 0.]] Ones Array: [[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]] Constant Array: [[7 7] [7 7]] Range Array: [0 2 4 6 8] Linspace Array: [0. 0.25 0.5 0.75 1. ]
Create Python Numpy Arrays Using Random Number Generation
NumPy provides functions to create arrays filled with random numbers.
- np.random.rand(): Creates an array of specified shape and fills it with random values sampled from a uniform distribution over [0, 1).
- np.random.randn(): Creates an array of specified shape and fills it with random values sampled from a standard normal distribution.
- np.random.randint(): Creates an array of specified shape and fills it with random integers within a given range.
import numpy as np
ar = np.random.rand(2, 3)
an = np.random.randn(2, 2)
ai = np.random.randint(1, 10, size=(2, 3))
print(ar)
print(an)
print(ai)
Output
[[0.20421896 0.03530146 0.24261146] [0.88545223 0.64030701 0.1138876 ]] [[-0.32144036 -1.62570762] [-0.80204074 -1.00453878]] [[2 7 6] [8 3 9]]
Create Python Numpy Arrays Using Matrix Creation Routines
NumPy provides functions to create specific types of matrices.
- np.eye(): Creates an identity matrix of specified size.
- np.diag(): Constructs a diagonal array.
- np.zeros_like(): Creates an array of zeros with the same shape and type as a given array.
- np.ones_like(): Creates an array of ones with the same shape and type as a given array.
import numpy as np
im = np.eye(3)
da = np.diag([1, 2, 3])
a0 = np.zeros_like(da)
a1 = np.ones_like(da)
print(im)
print(da)
print(a0)
print(a1)
Output
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] [[1 0 0] [0 2 0] [0 0 3]] [[0 0 0] [0 0 0] [0 0 0]] [[1 1 1] [1 1 1] [1 1 1]]