Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
NumPy provides functionality to generate random samples and permutations of sequences using the numpy.random.permutation()
and numpy.random.shuffle()
functions. Let's delve into these functions and their use-cases.
Start by importing the necessary library:
import numpy as np
numpy.random.permutation()
:The numpy.random.permutation()
function returns a shuffled version of the input sequence (or returns a permuted range if an integer is given).
arr = np.array([1, 2, 3, 4, 5]) print(np.random.permutation(arr)) # Example Output: [4 1 3 5 2]
For an integer input, it treats it as a range:
print(np.random.permutation(5)) # Example Output: [3 1 0 4 2]
For a multi-dimensional array, it only shuffles the first axis:
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(np.random.permutation(matrix)) # Example Output: # [[7 8 9] # [1 2 3] # [4 5 6]]
numpy.random.shuffle()
:The numpy.random.shuffle()
function shuffles the input sequence in-place:
arr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr) # Example Output: [5 2 1 4 3]
Just like permutation()
, the shuffle()
function will only shuffle along the first axis of a multi-dimensional array:
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) np.random.shuffle(matrix) print(matrix) # Example Output: # [[7 8 9] # [1 2 3] # [4 5 6]]
numpy.random.permutation()
: Returns a shuffled copy of the input sequence.numpy.random.shuffle()
: Shuffles the sequence in-place.The ability to generate random permutations or shuffle sequences is invaluable in many scenarios like simulations, cross-validation in machine learning, and games. NumPy provides efficient tools to handle these operations with ease.
NumPy's numpy.random.permutation
function allows you to generate random samples from a sequence of permutations.
import numpy as np # Create a sequence or array sequence = np.array([1, 2, 3, 4, 5]) # Generate a random permutation sample random_permutation_sample = np.random.permutation(sequence) print("Random Permutation Sample:") print(random_permutation_sample)
You can use NumPy's random.permutation
function to generate random permutation samples.
# Assuming 'sequence' is already defined # Generate a random permutation sample random_permutation_sample = np.random.permutation(sequence) print("Random Permutation Sample:") print(random_permutation_sample)
Here's an example code snippet demonstrating the use of NumPy's permutation function for random sampling.
# Assuming 'sequence' is already defined # Generate a random permutation sample random_permutation_sample = np.random.permutation(sequence) print("Random Permutation Sample:") print(random_permutation_sample)
NumPy's random.permutation
function is straightforward to use for generating random samples from permutations.
# Assuming 'sequence' is already defined # Use numpy.random.permutation for random samples random_permutation_sample = np.random.permutation(sequence) print("Random Permutation Sample:") print(random_permutation_sample)
Here's a sample code snippet showcasing the generation of random permutation samples using NumPy.
import numpy as np # Assuming 'sequence' is already defined # Generate multiple random permutation samples num_samples = 3 permutation_samples = [np.random.permutation(sequence) for _ in range(num_samples)] print("Permutation Samples:") for sample in permutation_samples: print(sample)
NumPy's random.permutation
function allows efficient random sampling from a given sequence.
# Assuming 'sequence' is already defined # Perform random sampling using numpy.random.permutation random_permutation_sample = np.random.permutation(sequence) print("Random Permutation Sample:") print(random_permutation_sample)
Generate a random permutation of a given sequence using NumPy's random.permutation
function.
# Assuming 'sequence' is already defined # Generate a random permutation of the sequence random_permutation = np.random.permutation(sequence) print("Random Permutation of Sequence:") print(random_permutation)
NumPy's numpy.random.permutation
function is used to generate a random permutation of a sequence.
# Assuming 'sequence' is already defined # Generate a random permutation using numpy.random.permutation random_permutation = np.random.permutation(sequence) print("Random Permutation:") print(random_permutation)