Given n Number of Vectors, How to Build the Cartesian Product using Numpy?
Is it possible to calculate the cartesian product of vectors in NumPy? Yes, absolutely! Let's have a look at the various methods for computing the cartesian product of vectors. Before we proceed, let us take a step back and define what we meant by cartesian product.
The cartesian product of X and Y, denoted by X x Y, is the set X x Y = {(a,b) | a belongs to X and b belongs to Y}. (a,b) are ordered pairs of items (or a pair of two numbers (or variables) written inside brackets and separated by a comma). For example if X = {7,8} and Y = {1,2,4} then the cartesian products of X and Y is X x Y = {(7,1),(7,2),(7,4),(8,1),(8,2),(8,4)}. Got it? Perhaps one more example would help, so examine the following sets: X = {q,w,r} and Y = {0,1} the cartesian of X and Y denoted by X x Y = {(q,0), (q,1),(w,0),(w,1),(r,0),(r,1)}. This should be obvious by now, right? Awesome! Let us proceed.
Note : A small disclaimer: we presume you are familiar with Python techniques such as list comprehension and are well-educated in NumPy in-built functions. If any of the concepts or terms discussed in this article are foreign to you, please review the following link first.
There are several methods to determine the cartesian product of vectors, including:
- Approach 1: itertools.product() method
- Approach 2: Utilize the meshgrid() method from the NumPy library.
- Approach 3: Combination of numpy.transpose(), numpy.tile(), and numpy.repeat()
- Approach 4: for-in method
- Approach 5: User-defined functions
Let us now thoroughly examine each of the aforementioned approaches.
Approach 1
We will use the itertools package in this method, which has several methods related to combination & permutation. The itertools.product() method can be used to compute the cartesian product of "n" iterables. The itertools.product() method accepts iterables as input arguments and returns the iterables' cartesian product. Let me illustrate this with an example.
Code :
Output :
We computed the cartesian cross-product of array1 and array2 in the preceding code using the product() function in the itertools package and saved the result in combinations.
Approach 2
In this technique, we will use an inbuilt function given by the NumPy package to calculate the cartesian product of two NumPy arrays. To compute the cartesian product of two NumPy arrays, we will utilize the meshgrid() method from the NumPy library. The matrices are passed as input parameters to the numpy.meshgrid() method, which returns the cross-product of the two matrices.
To compute the cartesian product, we may utilize the numpy.meshgrid() method in two ways. With the assistance of an example, let us explore the first method, in which we will merely use the numpy.meshgrid() function to calculate the cartesian product.
Code :
Output :
The second approach will include combining the numpy.meshgrid() and numpy.dstack() functions. The numpy.dstack() method accepts an array sequence as input & produces the array generated by stacking the arrays as output.
Code:
Output:
Using NumPy's meshgrid() function, we calculated the cartesian cross-product of the array with itself in both of the preceding programs.
Approach 3
We will also use NumPy inbuilt functions in this approach. The functions numpy.transpose(), numpy.tile(), and numpy.repeat() will be combined. To transpose our array, we use numpy.transpose(). Both the numpy.tile() and numpy.repeat() functions create an array by duplicating array content the number of times specified by the programmer as the function's input. Let me illustrate this with an example.
Code:
Output:
In the prior program, you may have observed that we calculated the cartesian cross-product of the array with itself.
Approach 4
The for-in iterator is a simpler way of accomplishing the same purpose as the two previous approaches. In Python, the for-in iterator is employed to iterate through every element in an iterable. This technique does not need the installation of any additional packages or libraries. Let us better comprehend this by using an example.
Code:
Output:
In the previous code, we computed the cartesian cross-product among two arrays utilizing a nested for-in iterator. Using the np.array() method, we stored the result in a NumPy array.
Note: The above methods (approaches 2, 3, & 4) are suitable for computing the cartesian product of two arrays.
Approach 5
We'll look at several user-defined methods to calculate the cartesian product of n-number arrays in this approach.
Let's start with our first user-defined function, which calculates the cartesian product of n number arrays using the numpy.result type() function and the list comprehension approach. The numpy.result type() method produces the data type that resulted from implementing the NumPy type promotion guidelines to the parameters, in this instance a list of arrays.
Code:
Output:
Let us now examine our second user-defined function, which computes the cartesian product of n number arrays employing NumPy inbuilt functions such as numpy.ix_(), numpy.broadcast arrays(), numpy.prod(), numpy.result type(), and numpy.empty() . If you come across any unfamiliar terms, it is recommended that you read through the official NumPy documentation to better understand the following code.
Code:
Output:
Now consider our final user-created function, which computes the cartesian product of n number arrays leveraging NumPy inbuilt functions such as numpy.asarray(), numpy.zeros(), numpy.prod(), and numpy.repeat(), as well as the list comprehension and recursion strategy. It is advised that you read through the official NumPy documentation if you encounter any terminology that is unclear in order to comprehend the code that follows.
Code:
Output:
Using the different user-defined functions, we calculated the cartesian cross-product of the array with itself in all of the preceding code examples.
In general, we may predict that employing inbuilt functions would be quicker for smaller inputs, whereas a user-defined function may be faster for larger inputs. Furthermore, approaches 2, 3, and 4 will not assist with a generalized n-dimensional product since they lack clear higher-dimensional equivalents. As a result, it is worthwhile to investigate the behavior of user-defined functions as well. We discovered that our first user-defined function, as described in approach 5, is the quickest and most optimum solution to the problem.
Conclusion
In this blog, we learned that
- To compute the cartesian product, we may utilize built-in methods such as numpy.meshgrid() or a combination of numpy.tile() and numpy.repeat().
- To compute the cartesian product of n vectors, we may also utilize user-defined functions.
- In general, implementing inbuilt functions is quicker for small inputs, although employing user-defined functions is faster for larger inputs.