![[Formales]] ![[combinations__664404932.png]] - [NumPy](https://numpy.org)<br> In: *NumPy Documentation* - [NumPy Quickstart](https://NumPy.org/devdocs/user/quickstart.html) <br> In: *NumPy Documentation* - [Numpy CheatSheet](https://www.geeksforgeeks.org/numpy-cheat-sheet/#numpy-cheat-sheet-faqs) <br> In: *Geeks for Geeks* - [NumPy Tutorial](https://www.youtube.com/watch?v=QUT1VHiLmmI&t=6s) <br> From: *FreeCodeCamp*, 1hr YouTube > [!TLDR] NumPy > Numerical Python is a fundamental package for scientific computing in Python. NumPy supports large, multi-dimensional arrays and matrices, along with an extensive collection of high-level mathematical functions to operate on these arrays. # Combinations The following code snippet generates a 2D array of combinations using np.meshgrid and np.stack, which is a great start for exploring combinations in NumPy. Combine two Coins Head = 1, Tail = 2 ```python import numpy as np a = np.stack(np.meshgrid([[1, 2]], [[1, 2]]), -1).reshape(-1, 2) a ``` # Exercises The exercises above cover a range of operations, including generating combinations, computing sums, filtering based on conditions, and manipulating grids. They provide a good foundation for understanding how to work with combinations and grids in NumPy. ## Create all possible combinations of three arrays - Define three arrays - Generate all possible combinations of these arrays ```python import numpy as np # Define three arrays array1 = np.array([1, 2]) array2 = np.array([3, 4]) array3 = np.array([5, 6]) # Generate all possible combinations of these arrays combinations = np.stack(np.meshgrid(array1, array2, array3), -1).reshape(-1, 3) print("Combinations of three arrays:\n", combinations) ``` ## Generate combinations and compute their sums - Define two arrays - Generate combinations - Compute the sum of each combination ```python import numpy as np # Define two arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5]) # Generate combinations combinations = np.stack(np.meshgrid(array1, array2), -1).reshape(-1, 2) # Compute the sum of each combination sums = np.sum(combinations, axis=1) print("Combinations:\n", combinations) print("Sums:\n", sums) ``` ## Create a 3D grid of points - Define ranges for x, y, z - Create a 3D grid of points ```python import numpy as np # Define ranges for x, y, z x = np.array([0, 1]) y = np.array([0, 1]) z = np.array([0, 1]) # Create a 3D grid of points grid = np.stack(np.meshgrid(x, y, z), -1).reshape(-1, 3) print("3D Grid of Points:\n", grid) ``` ## Filter combinations based on a condition - Generate combinations - Filter combinations where the sum is greater than 7 ```python import numpy as np # Generate combinations a = np.stack(np.meshgrid([1, 2, 3], [4, 5, 6]), -1).reshape(-1, 2) # Filter combinations where the sum is greater than 7 filtered_combinations = a[np.sum(a, axis=1) > 7] print("Filtered Combinations:\n", filtered_combinations) ``` ## Create and manipulate a 2D grid - Create a 2D grid - Multiply each element in the grid by 2 ```python import numpy as np # Create a 2D grid x = np.array([1, 2, 3]) y = np.array([4, 5]) grid = np.stack(np.meshgrid(x, y), -1).reshape(-1, 2) # Multiply each element in the grid by 2 modified_grid = grid * 2 print("Original Grid:\n", grid) print("Modified Grid:\n", modified_grid) ```