Consider the following code that shuffles a numpy array. What will be the output?
import numpy as np np.random.seed(0) arr = np.array([1, 2, 3, 4, 5]) np.random.shuffle(arr) print(arr.tolist())
Remember that np.random.shuffle shuffles the array in place and the seed fixes the random order.
With np.random.seed(0), the shuffle order is fixed. The array [1, 2, 3, 4, 5] becomes [4, 1, 3, 5, 2] after shuffling.
Given this code, how many elements does the array arr have after shuffling?
import numpy as np arr = np.arange(10) np.random.shuffle(arr) print(len(arr))
Shuffling changes order but not size.
Shuffling an array does not remove or add elements. The length remains the same.
What error will this code raise when trying to shuffle a tuple?
import numpy as np t = (1, 2, 3, 4) np.random.shuffle(t)
Think about whether tuples can be changed in place.
Tuples are immutable, so trying to shuffle them (which changes order in place) raises a TypeError about item assignment.
You have a 2D numpy array representing data rows. Which code correctly shuffles the rows but keeps columns intact?
import numpy as np np.random.seed(1) arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
Remember np.random.shuffle shuffles in place and returns None.
np.random.shuffle(arr) shuffles rows in place. Option A assigns None to arr, losing data. Option A returns a shuffled copy but does not assign it back. Option A tries to shuffle the transpose, which is not the intended behavior.
Choose the best explanation for the difference in behavior between np.random.shuffle and np.random.permutation when applied to multi-dimensional numpy arrays.
Think about in-place vs returning new arrays and which axis is shuffled.
np.random.shuffle shuffles the array along the first axis in place, meaning rows get reordered but columns stay intact. np.random.permutation returns a new shuffled copy of the entire array along the first axis without modifying the original.