NumPy has an ability to insert elements into an array with ease.With NumPy’s built-in functions, we can insert elements into a 2D array randomly, without having to manually specify their positions. In this case, we will explore how to insert “n” elements into a 2D array randomly.
1. By "random.randint()" and loop:
You can used random.randit to create random indices, then by using for loop, you can assigned random value by using indices. Here's an example for you given below:
2. By "random.choice()" and "unravel_index()":
You can also used random.choice() method to create random indices, then by using these indices, assign random values with the help of unravel_choice() method. Here's an example for you given below:
3. By "reshape(): function:
You can also used reshape() method to insert random values in array. Let understand the example given below:
4. By "random.permutation()" function:
You can also used random.permutation() to develop shuffled indices and then used them to insert values in array.