Learn how to create NumPy arrays using Python data structures as well as NumPy’s built-in functions.
A common way of creating NumPy arrays is by passing a Python data structure such as Python List or Python Tuples as a parameter of NumPy's array()
method.
The general syntax for NumPy's array()
method is as follows:
numpy.array(object, dtype, *args)
The basic parameters that need to be passed into this function are:
You can pass a Python List as the object
parameter of the NumPy's array()
method to create a NumPy array.
Consider the example given below to understand how to create a NumPy Array from a Python List:
# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python List num_array = np.array([1, 2, 3, 4]) # Printing the content of the variable print(num_array)
[1 2 3 4]
You can also specify the data type of the array to be created as follows:
# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python List with dtype as float num_array = np.array([1, 2, 3, 4], dtype='float') # Printing the content of the variable print(num_array)
[1. 2. 3. 4.]
You can pass a Python Tuple as the object
parameter of the NumPy's array()
method to create a NumPy array.
Consider the example given below to understand how to create a NumPy Array from a Python Tuple:
# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python Tuple num_array = np.array((1, 2, 3, 4)) # Printing the content of the variable print(num_array)
[1 2 3 4]
You can also specify the data type of the array to be created as follows:
# Importing the NumPy library as np import numpy as np # Creating a NumPy array from a Python Tuple with dtype as float num_array = np.array((1, 2, 3, 4), dtype='float') # Printing the content of the variable print(num_array)
[1. 2. 3. 4.]
NumPy provides a wide variety of built-in functions/methods for creating NumPy arrays from scratch. You will learn how to create NumPy arrays using these functions in the section below.
The NumPy zeros function, zeros()
, creates a NumPy array with all elements as zero based on a given shape and data type. The syntax for the function is as follows:
numpy.zeros(shape, dtype, *args)
The basic parameters that need to be passed into this function are:
float64
).Consider the example given below to understand how the NumPy zeros function works:
# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the zeros() function arr1 = np.zeros(shape=(1, 5), dtype=np.int16) arr2 = np.zeros(shape=(5, 5)) # Default dtype is np.float64 # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)
arr1: [[0 0 0 0 0]] arr2: [[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.]]
The NumPy ones function, ones()
, creates a NumPy array with all elements as one based on a given shape and data type. The syntax for the function is as follows:
numpy.ones(shape, dtype, *args)
The basic parameters that need to be passed into this function are:
float64
).Consider the example given below to understand how the NumPy ones function works:
# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the ones() function arr1 = np.ones(shape=(1, 5), dtype=np.int16) arr2 = np.ones(shape=(5, 5)) # Default dtype is np.float64 # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)
arr1: [[1 1 1 1 1]] arr2: [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]]
The NumPy arange function, arange()
, creates a one-dimensional NumPy array with evenly spaced values within a given interval. The syntax for the function is as follows:
numpy.arange([start, ]stop, [step, ]dtype=None, *args)
The basic parameters that need to be passed into this function are:
The NumPy arange function is quite similar to the Python range function. Consider the example given below to understand how the NumPy arange function works:
# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the arange() function arr1 = np.arange(start=1, stop=5, step=1, dtype=np.int16) arr2 = np.arange(start=10, stop=20, step=2, dtype=np.float32) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)
arr1: [1 2 3 4] arr2: [10. 12. 14. 16. 18.]
The NumPy linspace function, linspace()
, creates a one-dimensional NumPy array with evenly spaced values within a given interval. The syntax for the function is as follows:
numpy.linspace(start, stop, num, *args)
The basic parameters that need to be passed into this function are:
Consider the example given below to understand how the NumPy linspace function works:
# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the linspace() function arr1 = np.linspace(start=1, stop=3, num=5) arr2 = np.linspace(start=10, stop=20, num=2) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)
arr1: [1. 1.5 2. 2.5 3. ] arr2: [10. 20.]
Note: The data type of a NumPy array created using the linspace function is always a float unless it is specified explicitly.
The NumPy rand function, rand()
, creates a NumPy array of the given shape and populates it with random samples from a uniform distribution over [0, 1) i.e. including 0 and excluding 1. The syntax for the function is as follows:
numpy.random.rand(d0, d1, d2, ..., dn)
The basic parameters that need to be passed into this function are:
Consider the example given below to understand how the NumPy rand function works:
# Importing the NumPy library as np import numpy as np # Creating NumPy arrays using the rand() function arr1 = np.random.rand(3,) arr2 = np.random.rand(2, 2) # Printing the arrays print("arr1: ", arr1) print("arr2: ", arr2)
arr1: [0.52768115 0.73880502 0.12197086] arr2: [[0.7268723 0.77066793] [0.39853139 0.28570399]]
Note that the values generated by the rand function can change each time the array is created since the values are randomly generated.
With this lesson, you have learned to create NumPy arrays from Python Lists/Python Tuples as well as from NumPy's built-in functions.