0
0
NumPydata~5 mins

Setting random seed for reproducibility in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does setting a random seed in numpy do?
Setting a random seed makes sure that the random numbers generated are the same every time you run the code. This helps in getting reproducible results.
Click to reveal answer
beginner
How do you set a random seed in numpy?
You use np.random.seed(your_number). Replace your_number with any integer to fix the sequence of random numbers.
Click to reveal answer
beginner
Why is reproducibility important in data science?
Reproducibility means you or others can run the same code and get the same results. This is important for checking work, sharing findings, and debugging.
Click to reveal answer
beginner
What happens if you don't set a random seed before generating random numbers?
If you don't set a seed, numpy will generate different random numbers each time you run the code, which can make results vary and be hard to reproduce.
Click to reveal answer
beginner
Example: How to generate 5 random numbers between 0 and 1 with a fixed seed?
```python
import numpy as np
np.random.seed(42)
rands = np.random.rand(5)
print(rands)
```
This will always print the same 5 random numbers.
Click to reveal answer
What function sets the random seed in numpy?
Anumpy.set_seed()
Bnumpy.random.set()
Cnumpy.random.seed()
Dnumpy.seed.random()
Why should you set a random seed in your code?
ATo avoid using random numbers
BTo make random numbers different every time
CTo speed up random number generation
DTo make random numbers reproducible
What type of value do you pass to numpy.random.seed()?
AAn integer
BA float
CA string
DA boolean
If you run numpy.random.rand(3) twice without setting a seed, what happens?
AYou get different 3 numbers each time
BYou get zeros
CYou get an error
DYou get the same 3 numbers both times
Which of these is a benefit of reproducibility in data science?
ACode runs faster
BResults can be trusted and checked
CRandom numbers are more random
DYou don't need to write comments
Explain how to set a random seed in numpy and why it is useful.
Think about how to get the same random numbers every time.
You got /4 concepts.
    Describe what happens if you do not set a random seed before generating random numbers in numpy.
    Consider the effect on results when running code multiple times.
    You got /4 concepts.