0
0
NumPydata~10 mins

Why random generation matters in NumPy - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to generate 5 random numbers between 0 and 1 using NumPy.

NumPy
import numpy as np
random_numbers = np.random.[1](5)
print(random_numbers)
Drag options to blanks, or click blank then click option'
Arand
Bchoice
Crandint
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.randint which generates integers, not floats.
Using np.random.choice without specifying a list.
Using np.random.random which is a valid function but not the best here.
2fill in blank
medium

Complete the code to generate 5 random integers between 1 and 10 (inclusive) using NumPy.

NumPy
import numpy as np
random_ints = np.random.[1](1, 11, 5)
print(random_ints)
Drag options to blanks, or click blank then click option'
Arandom
Brand
Crandint
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.rand which generates floats, not integers.
Using np.random.random which generates floats.
Using np.random.choice without a list.
3fill in blank
hard

Fix the error in the code to generate 3 random numbers from a normal distribution with mean 0 and standard deviation 1.

NumPy
import numpy as np
samples = np.random.normal([1], 1, 3)
print(samples)
Drag options to blanks, or click blank then click option'
A3
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting mean to 1 instead of 0.
Confusing mean with standard deviation.
Using sample size as mean.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only include words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Comparing the word string directly to 3.
Not filtering words by length.
5fill in blank
hard

Fill both blanks to create a dictionary with uppercase words as keys and their lengths as values, including only words longer than 3 letters.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word.upper(): {BLANK_2}} for word in words if {{BLANK_2}}
print(lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 3
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Not starting the dictionary with a curly brace.
Using the word itself as key instead of uppercase.
Not filtering words by length.