In this post, we discuss single- and multidimensional arrays and matrices in Python. Since Python does not offer in-built support for arrays, we use NumPy, Python’s library for matrix and array computations. The NumPy array is one of the most versatile data structures in Python and it is the foundation of most Python-based data science and machine learning applications.
What is an Array in Python?
A Python array is a collection of elements of the same data type. Each element in the array has a unique index and is stored at a contiguous location in memory.
How to Create a NumPy Array?
To create an array, you first have to install and import the NumPy module. If you haven’t installed it yet, check out the official installation guide. Next, we import NumPy and create our first array containing the numbers 1-3. When we check the data type, Python tells us that this is a NumPy array.
import numpy as np
a = np.array([1, 2, 3])
type(a) # numpy.ndarray
Python Array vs List
Contrary to an array, a list does not constrain you to one data type. For example, you can store a string, an integer, and a boolean in a list like this:
l = ["string", 1, True]
print(l) # ['string', 1, True]
If you try to to the same with NumPy array, Python will attempt to represent all elements in the same data type. If we create an array with a string, an integer, and a boolean, all elements will be printed as strings and the data type is displayed as Unicode:
NumPy has a sort function that operates like the inbuilt sort function which we used to sort lists. It can sort array elements either numerically, or alphabetically. By default, it sorts in ascending order.
Concatenation in NumPy is done using the concatenate function. If you are dealing with two one-dimensional NumPy arrays, you can concatenate them by simply passing two arrays to the concatenate function. If you are dealing with multidimensional arrays, you have to specify the axis along which to concatenate.
You can concatenate a one-dimensional array as follows.
Once you have higher-dimensional arrays, simply concatenating them is no longer enough. If you have experience with matrix addition in mathematics, you know that you can add matrices vertically or horizontally. With multidimensional arrays, you have the same problem. You need to decide along which axis to add the arrays.
But both matrices need to have the same size along the dimension that you add. You can only add a 2×3 matrix to a 2×2 matrix along the first dimension, because along the second dimension the first matrix has a size of 3, while the second has a size of 2. In the NumPy concatenate function you can pass an axis argument. An axis argument of 0 tells Python to concatenate the matrices vertically.
When Concatenating multidimensional arrays, you always have to be aware of how your dimensions fit together.
How does NumPy Delete Work?
The NumPy delete function takes the array and the index for the element you want to delete as arguments. For a multidimensional array you also need to specify the axis along which you want to delete elements. It returns a new array without the deleted elements.
For a one-dimensional array, deletion is fairly straightforward. We delete the second entry which has the index “1”:
a = np.array([1, 2, 3])
np.delete(a, 1) #array([1, 3])
For a multidimensional array, you have to add a third entry specifying the axis along which to delete. We delete the first entry with index 0. and we specify 0 for the axis, which corresponds to the horizontal axis.
This will throw an error because the matrix is not invertible. Remember, that a matrix multiplied by its inverse needs to result in the identity matrix. Such an inverse may not exist depending on the original matrix. Let’s use another matrix that does have an inverse:
a = np.array([[4, 7],
Now, the matrix is invertible and we get a result. If we calculate the dot product between the two matrices, we get the identity matrix.
Note that the upper-right value is not exactly zero due to Python’s floating point approximation.
How Do You Create an Identity Matrix in Python NumPy?
An identity matrix can be constructed with NumPy’s identity function. Since an identity matrix is a symmetric matrix with ones on the leading diagonal, and zeros everywhere else, NumPy only needs to know the number of dimensions to construct the matrix.
How Do You Calculate a Dot Product in NumPy
To calculate the dot product, you can use the np.dot function in NumPy. The function takes the two matrices or vectors as arguments that you want to dot.
When you calculate dot products in linear algebra, you need to ensure that the horizontal axis of the first matrix has the same size as the vertical axis of the second matrix. If we try to change the order of the matrices in the previous calculation, we will get an error because the dimensions do not match.