What will be the output of the following code snippet?
import numpy as np np.random.seed(0) sample1 = np.random.randint(1, 10, 3) np.random.seed(0) sample2 = np.random.randint(1, 10, 3) print(sample1) print(sample2)
import numpy as np np.random.seed(0) sample1 = np.random.randint(1, 10, 3) np.random.seed(0) sample2 = np.random.randint(1, 10, 3) print(sample1) print(sample2)
Setting the seed resets the random number generator to the same starting point.
Using np.random.seed(0) before generating random numbers ensures the same sequence is produced each time. So sample1 and sample2 are identical.
Why is it important to set a random seed when generating random data in data science projects?
Think about sharing your work with others or rerunning your code later.
Setting a random seed makes the random number generation reproducible, so results can be repeated exactly, which is important for debugging and sharing.
What is the shape and content type of the output from this code?
import numpy as np np.random.seed(42) sample = np.random.normal(loc=0, scale=1, size=(2,3)) print(type(sample)) print(sample.shape)
import numpy as np np.random.seed(42) sample = np.random.normal(loc=0, scale=1, size=(2,3)) print(type(sample)) print(sample.shape)
Check the type returned by np.random.normal and the shape argument.
The function np.random.normal returns a numpy array with the shape specified by the size parameter, here (2, 3).
What error will this code produce?
import numpy as np
np.random.seed('seed')
sample = np.random.randint(0, 5, 4)
print(sample)import numpy as np np.random.seed('seed') sample = np.random.randint(0, 5, 4) print(sample)
Check the type of the argument passed to np.random.seed.
The seed must be an integer or array of integers. Passing a string causes a TypeError.
You want to split your dataset into training and testing parts randomly but reproducibly. Which code snippet correctly achieves this?
Think about reproducibility and correct use of random permutation for splitting.
Setting the seed ensures reproducibility. Using np.random.permutation shuffles indices without repeats. Option C correctly sets the seed and uses permutation.