Shuffling NumPy array along a given axis

Shuffling NumPy array along a given axis

You can shuffle a NumPy array along a given axis using the numpy.random.shuffle() function, which randomly permutes the elements along the specified axis. Here's how to shuffle a NumPy array along a specific axis:

import numpy as np # Create a 2D NumPy array (for demonstration) array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Specify the axis along which to shuffle (axis=0 for rows, axis=1 for columns) axis_to_shuffle = 0 # Shuffle the array along the specified axis if axis_to_shuffle == 0: np.random.shuffle(array) elif axis_to_shuffle == 1: np.random.shuffle(array.T) # Display the shuffled array print(array) 

In this example, we created a 2D NumPy array and specified axis_to_shuffle as 0 to shuffle the rows. If you want to shuffle the columns, change axis_to_shuffle to 1.

Keep in mind that numpy.random.shuffle() directly modifies the input array in place. If you want to create a shuffled copy of the original array without modifying the original, you should make a copy of the array before shuffling, like this:

shuffled_array = np.copy(array) if axis_to_shuffle == 0: np.random.shuffle(shuffled_array) elif axis_to_shuffle == 1: np.random.shuffle(shuffled_array.T) 

This way, array remains unchanged, and shuffled_array contains the shuffled elements along the specified axis.

Examples

  1. Shuffle NumPy array along the first axis

    • Description: Demonstrates how to shuffle a NumPy array along the first axis (rows in a 2D array).
    • Code:
      import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) np.random.shuffle(arr) # Shuffles along the first axis (rows) print("Shuffled along first axis:", arr) 
  2. Shuffle NumPy array along the second axis

    • Description: Shuffle a 2D array along the second axis (columns).
    • Code:
      import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Transpose to shuffle columns, then transpose back arr_transposed = np.transpose(arr) np.random.shuffle(arr_transposed) shuffled_arr = np.transpose(arr_transposed) print("Shuffled along second axis:", shuffled_arr) 
  3. Shuffle 3D NumPy array along the first axis

    • Description: Shuffle a 3D array along its first axis.
    • Code:
      import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]) np.random.shuffle(arr) # Shuffle along the first axis print("Shuffled along first axis:", arr) 
  4. Shuffle 3D NumPy array along the third axis

    • Description: Shuffle a 3D array along the third axis.
    • Code:
      import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) arr_shuffled = arr.copy() for i in range(arr.shape[0]): # Shuffle each slice along the third axis np.random.shuffle(arr_shuffled[i]) print("Shuffled along third axis:", arr_shuffled) 
  5. Shuffle 3D NumPy array along a specific axis

    • Description: General approach to shuffle a 3D array along any given axis.
    • Code:
      import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) axis = 1 # Axis to shuffle along arr_transposed = np.moveaxis(arr, axis, 0) # Move chosen axis to the front np.random.shuffle(arr_transposed) # Shuffle along the moved axis arr_shuffled = np.moveaxis(arr_transposed, 0, axis) # Move axis back to its original position print("Shuffled along specific axis:", arr_shuffled) 
  6. Shuffle NumPy array with reproducibility along a given axis

    • Description: Set a random seed to shuffle with reproducibility along a specified axis.
    • Code:
      import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) np.random.seed(42) # Set random seed for reproducibility np.random.shuffle(arr) # Shuffle along the first axis print("Reproducible shuffle along first axis:", arr) 
  7. Shuffle NumPy array using a random permutation along a given axis

    • Description: Use numpy.random.permutation to shuffle array elements along a specified axis.
    • Code:
      import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) permuted_indices = np.random.permutation(arr.shape[0]) # Random permutation for indices shuffled_arr = arr[permuted_indices] print("Shuffled using random permutation:", shuffled_arr) 
  8. Shuffle along different axes in a multidimensional array

    • Description: Shuffle along different axes for a multidimensional array.
    • Code:
      import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]]) # Shuffle along the second axis (middle dimension) arr_transposed = np.moveaxis(arr, 1, 0) # Move second axis to the front np.random.shuffle(arr_transposed) # Shuffle the new front axis arr_shuffled = np.moveaxis(arr_transposed, 0, 1) # Move axis back print("Shuffled along different axis:", arr_shuffled) 
  9. Shuffle NumPy array along the last axis

    • Description: Shuffle a 2D array along its last axis.
    • Code:
      import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) arr_transposed = np.transpose(arr) # Transpose to move last axis to the first np.random.shuffle(arr_transposed) # Shuffle along the new first axis arr_shuffled = np.transpose(arr_transposed) # Transpose back print("Shuffled along last axis:", arr_shuffled) 
  10. Shuffle a specific slice in a multidimensional array


More Tags

findby linear-algebra git-rm networkx plugins keycloak-services copy-local selectedtext single-quotes src

More Python Questions

More Livestock Calculators

More Chemical thermodynamics Calculators

More Dog Calculators

More Various Measurements Units Calculators