0
0
NumPydata~20 mins

Why random generation matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Generation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy random seed effect

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)
NumPy
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)
A
[6 1 4]
[6 1 4]
B
[6 1 4]
[2 8 8]
C
[1 6 4]
[1 6 4]
D
[2 8 8]
[2 8 8]
Attempts:
2 left
💡 Hint

Setting the seed resets the random number generator to the same starting point.

🧠 Conceptual
intermediate
1:30remaining
Why set a random seed in data science?

Why is it important to set a random seed when generating random data in data science projects?

ATo ensure the random numbers are different every time the code runs
BTo make the random number generation reproducible for debugging and sharing results
CTo speed up the random number generation process
DTo increase the randomness quality of the generated numbers
Attempts:
2 left
💡 Hint

Think about sharing your work with others or rerunning your code later.

data_output
advanced
2:00remaining
Effect of different seeds on random samples

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)
NumPy
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)
A
<class 'numpy.ndarray'>
(2, 3)
B
<class 'list'>
(2, 3)
C
<class 'numpy.ndarray'>
(3, 2)
D
<class 'list'>
(3, 2)
Attempts:
2 left
💡 Hint

Check the type returned by np.random.normal and the shape argument.

🔧 Debug
advanced
1:30remaining
Identify the error in random number generation

What error will this code produce?

import numpy as np
np.random.seed('seed')
sample = np.random.randint(0, 5, 4)
print(sample)
NumPy
import numpy as np
np.random.seed('seed')
sample = np.random.randint(0, 5, 4)
print(sample)
ASyntaxError: invalid syntax
BValueError: seed must be between 0 and 2**32 - 1
CNo error, prints 4 random integers between 0 and 4
DTypeError: 'str' object cannot be interpreted as an integer
Attempts:
2 left
💡 Hint

Check the type of the argument passed to np.random.seed.

🚀 Application
expert
2:30remaining
Using random generation for train-test split reproducibility

You want to split your dataset into training and testing parts randomly but reproducibly. Which code snippet correctly achieves this?

A
import numpy as np
indices = np.random.permutation(10)
train_idx = indices[:7]
test_idx = indices[7:]
B
import numpy as np
np.random.seed('123')
indices = np.random.permutation(10)
train_idx = indices[:7]
test_idx = indices[7:]
C
import numpy as np
np.random.seed(123)
indices = np.random.permutation(10)
train_idx = indices[:7]
test_idx = indices[7:]
D
import numpy as np
np.random.seed(123)
indices = np.random.randint(0, 10, 10)
train_idx = indices[:7]
test_idx = indices[7:]
Attempts:
2 left
💡 Hint

Think about reproducibility and correct use of random permutation for splitting.