Skip to content

Array Creation

Array Creation

NumPy arrays are created with the np.array() function. The arguments provided to np.array() needs to be a list or iterable. An example is below. Note how the list [1,2,3] is passed into the function with square brackets at either end.

In [1]:
import numpy as np
np.array([1,2,3])

Out[1]:
array([1, 2, 3])

The data type can be passed into the np.array() function as a second optional positional arguments. Available data types include 'int64', 'float', 'complex' and '>U32' (string data type).
In [2]:
import numpy as np
np.array([1,2,3],dtype='float')

Out[2]:
array([1., 2., 3.])

The data type store in a NumPy array can be determined using the .dtype method. For instance, an array of floats returns float64.
In [3]:
import numpy as np
my_array = np.array([1,2,3],dtype='float')
my_array.dtype

Out[3]:
dtype('float64')

Arrays of Regularly Spaced Numbers

There are multiple ways to create arrays of regularly spaced numbers with NumPy

np.arange()

NumPy's np.arange() function creates a NumPy array according the arguments start, stop,step.

my_array = np.arange(start, stop, step)

The np.arange() function is useful for creating an array of regularly spaced numbers where you know the step size.

Consider creating a NumPy array of even numbers between 0 and 10. Note that just like counting in Python, counting in NumPy starts at 0 and ends at n-1.

In [4]:
np.arange(0,10+2,2)

Out[4]:
array([ 0,  2,  4,  6,  8, 10])

np.linspace()

NumPy's np.linspace() function creates a NumPy array according the arguments start, stop,number of elements.

my_array = np.linspace(start, stop, number of elements)

The np.linspace() function is useful for creating an array of regularly spaced numbers where the spacing is not known, but the number of values is. Consider creating a NumPy array of 10 numbers between 0 and 2pi.

In [5]:
np.linspace(0,2*np.pi,10)

Out[5]:
array([0.        , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,
       3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])

np.logspace()

NumPy's np.logspace() function creates a NumPy array according the arguments start, stop,number of elements, but unlike np.linspace(), np.logspace() produces a logarithmically spaced array.

my_array = np.logspace(start, stop, number of elements, base=<num>)

This function is useful for creating an array of logarithmically spaced numbers where the spacing interval is not known but the number of values is. Consider creating a NumPy array of 4 logarithmically spaced numbers between 10 and 100. The function call is np.logspace(1, 2, 4). The start is 10^1 = 10 and the stop is 10^2 = 100. Be careful about putting large numbers in for stop because the argument is the power of 10, not the stop value.

In [6]:
np.logspace(1, 2, 4)

Out[6]:
array([ 10.        ,  21.5443469 ,  46.41588834, 100.        ])

np.zeros()

NumPy's np.zeros() function creates a NumPy array containing all zeros of a specific size. np.zeros() is useful when the size of an array is known, but the values that will go into the array have not been created yet.

my_array = np.zeros((rows,cols))
In [7]:
np.zeros((5,5))
Out[7]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

np.ones()

NumPy's np.ones() function creates a NumPy array containing all 1's of a specific size. Like np.zeros(), np.ones() is useful when the size of an array is known, but the values that will go into the array have not been created yet.

my_array = np.ones((rows,cols))
In [8]:
np.ones((3,5))
Out[8]:
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

Arrays of Random Numbers

NumPy has functions to create arrays of many different types of random numbers in the np.random module. A few of the common random number types are detailed below.

Array of Random Integers

Arrays of random integers can be created with NumPy's np.random.randint() function. The general syntax is:

np.random.randint(lower limit, upper limit, number of values)

To create an array of 5 different random numbers, each random number between 1 and 10:

In [9]:
np.random.randint(0,10,5)

Out[9]:
array([6, 7, 2, 3, 0])

Array dimensions can be provided as the third argument. A 5 \times 5 array of random numbers between 1 and 10:
In [10]:
np.random.randint(0,10,[5,5])

Out[10]:
array([[7, 2, 1, 7, 7],
       [7, 5, 7, 1, 0],
       [9, 1, 1, 4, 3],
       [3, 3, 4, 0, 7],
       [0, 8, 6, 8, 0]])

Array of Random Floats

Arrays of random floating point numbers can be created with NumPy's np.random.rand() function. The general syntax is:

np.random.rand(number of values)

To create an array of 5 different random numbers between 0 and 1:

In [11]:
np.random.rand(5)

Out[11]:
array([0.87496687, 0.53720722, 0.46532805, 0.34379277, 0.40271602])

Random Array Choice from a List

np.random.choice(list of choices, number of choices)

To choose three numbers at random from a list of [1,5,9,11] use:

In [12]:
lst = [1,5,9,11]
np.random.choice(lst,3)

Out[12]:
array([11,  1, 11])

Random Array with a Normal Distribution

np.random.randn() returns an array of random numbers with a normal distribution, assuming a mean of 0 and variance of 1.

np.random.randn(number of values)
In [13]:
np.random.randn(10)
Out[13]:
array([-0.8587019 ,  0.51022209,  1.52989165, -0.62763827,  0.42359871,
       -0.12794222, -0.05960913,  0.1018347 , -0.13226924,  1.21221629])

To specify a mean mu and a standard deviation sigma, the function can be wrapped with:

In [14]:
mu = 70
sigma = 6.6

sigma * np.random.randn(10) + mu

Out[14]:
array([83.08982467, 63.75617079, 54.80340275, 65.64987114, 60.86298488,
       71.15108776, 72.68180092, 67.74486107, 62.88712717, 71.53153691])

Matplotlib's plt.hist() function can be used to quickly plot a normal distribution created with NumPy's np.random.randn() function.
In [15]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

mu = 70 sigma = 6.6

sample = sigma * np.random.randn(1000) + mu plt.hist(sample) plt.show()

2-D Arrays

np.meshgrid()

NumPy's np.meshgrid() function takes in two positional arguments which are 1D NumPy arrays. The two input arrays do not have to contain the same number of elements. The outputs of the np.meshgrid() function are two 2D arrays. One of the 2D arrays has the same values in each row; the other 2D array has the same values in each column.

np.meshgrid(array1,array2)
In [16]:
x = np.arange(0,6)
y = np.arange(0,11,2)
X, Y = np.meshgrid(x,y)
print(X)
print(Y)
[[0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]
 [0 1 2 3 4 5]]
[[ 0  0  0  0  0  0]
 [ 2  2  2  2  2  2]
 [ 4  4  4  4  4  4]
 [ 6  6  6  6  6  6]
 [ 8  8  8  8  8  8]
 [10 10 10 10 10 10]]

np.mgrid[]

NumPy's np.mgrid[] function is similar to np.meshgrid(), but has a "MATLAB-like" syntax and behavior.

Use square brackets [ ] after the np.mgrid function name. Separate the two "lists" passed as input arguments with a comma and use the start:stop:step indexing method. The outputs of the np.mgrid[] function are two 2D arrays. The first 2D array have the same values in each row; the second 2D array has the same values in each column.

np.mgrid[start:stop:step, start:stop:step]
In [17]:
X, Y = np.mgrid[0:5,0:11:2]
print(X)
print(Y)
[[0 0 0 0 0 0]
 [1 1 1 1 1 1]
 [2 2 2 2 2 2]
 [3 3 3 3 3 3]
 [4 4 4 4 4 4]]
[[ 0  2  4  6  8 10]
 [ 0  2  4  6  8 10]
 [ 0  2  4  6  8 10]
 [ 0  2  4  6  8 10]
 [ 0  2  4  6  8 10]]

Section Summary

Below is a list of NumPy function descriptions used in this section.

Function Description
np.array([list,of,numbers]) Array from a list
np.arange(start,stop,step) Array with know step
np.linspace(start, stop, num) Array with known num
np.logspace(start,stop,num) Logorithmically spaced array
np.zeros((rows,cols)) Array of zeros
np.ones((rows,cols)) Array of ones
np.random.randint(start, stop, num) Random integers
np.random.rand(num) Random float 0 to 1
np.random.choice(list,num) Randome choice from a list
np.random.randn(num) Random normal distribution
np.meshgrid(array1,array2) Two 2D arrays from two 1D arrays
np.mgrid[start:stop:step, start:stop:step] MATLAB meshgrid