Creating NumPy Arrays
November 24, 2020 2020-12-03 21:18Creating NumPy Arrays
In the previous chapter, we got familiar with NumPy and NumPy arrays. In this chapter, we will be learning about the different ways by which we can create NumPy Arrays. Following are the most common ways for creating NumPy arrays:
- From other Python Data Structures (e.g., Lists, Tuples)
- Using NumPy’s Built-In Functions (e.g., arange, ones, zeros, etc.)
Creating NumPy arrays from Python data structures
NumPy arrays can be created by using other Python Data Structures by passing them into the numpy.array function.
# Importing Numpy import numpy as np # Defining two different data structures lis = [1, 2, 3, 4] tup = (5, 6, 7, 8) # Creating numpy arrays arr_lis = np.array(lis) arr_tup = np.array(tup) # Printing the arrays print("NumPy array from List: ", arr_lis) print("NumPy array from Tuple: ", arr_tup)
Array from List: [1 2 3 4] Array from Tuple: [5 6 7 8]
Creating NumPy arrays using built-in functions
NumPy provides a wide variety of function for creating NumPy arrays from scratch. Some of those functions are illustrated below:
– numpy.zeros()
This function will create a NumPy array of the given shape, with all elements of the array as zeros. The basic parameters that need to be passed into this function are:
- shape: Shape of the desired array (integer or tuple of integers).
- dtype: Datatype of the desired array (default is
numpy.float64
).
# Importing Numpy import numpy as np # Creating numpy arrays 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.]]
– numpy.ones()
This function is responsible for creating NumPy arrays of the given shape, with all elements of the array as ones. The basic parameters that need to be passed into this function are:
- shape: Shape of the desired array (integer or tuple of integers).
- dtype: Datatype of the desired array (default is
numpy.float64
).
# Importing Numpy import numpy as np # Creating numpy arrays 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.]]
– numpy.arange()
This function is used to create a one dimensional NumPy array having evenly spaced values within a given interval. The basic parameters that need to be passed into this function are:
- start: The starting value of the interval. It should be an integer and the default value is 0. This value is included in the array created.
- stop: The ending value of the interval. This value is not generally included in the created array.
- step: The distance between two adjacent values in the array to be created. The default step size is 1.
- dtype: Datatype of the desired array.
import numpy as np # Creating numpy arrays 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.]
– numpy.linspace()
This function returns an evenly spaced NumPy array over a specified interval. The basic parameters that need to be passed into this function are:
- start: The starting value of the interval. This value is included in the array created.
- stop: The ending value of the interval. This value is also generally included in the created array.
- num: The number of evenly spaced samples to be generated within the given interval. The default value is 50.
import numpy as np # Creating numpy arrays 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: Only the basic parameters of numpy.linspace() is discussed above. If you want to know more about other parameters that you can pass into the function, do check this official documentation of NumPy.
– numpy.random.rand()
This function will generate a NumPy array of the desired shape whose elements are random samples from a uniform distribution over [0, 1)
. Only the shape of the desired array needs to be passed on to this function.
import numpy as np # Creating numpy arrays 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]]
With this chapter, you have learned to create NumPy arrays from standard python data structures and from NumPy’s Built-In functions. In the next chapter, you will learn to perform various ‘Operations on NumPy Arrays‘.