0
0
NumPydata~10 mins

np.random.rand() and random arrays in NumPy - Interactive Code Practice

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

Complete the code to create a 1D array of 5 random numbers between 0 and 1 using numpy.

NumPy
import numpy as np
random_array = np.random.[1](5)
print(random_array)
Drag options to blanks, or click blank then click option'
Arandom
Brandint
Crand
Drandn
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.random.randint which generates integers, not floats.
Using np.random.randn which generates numbers from a normal distribution.
2fill in blank
medium

Complete the code to create a 2D array with 3 rows and 4 columns of random numbers between 0 and 1.

NumPy
import numpy as np
array_2d = np.random.[1](3, 4)
print(array_2d)
Drag options to blanks, or click blank then click option'
Arand
Brandint
Crandom_sample
Drandn
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a tuple instead of separate arguments to rand.
Using randint which generates integers.
3fill in blank
hard

Fix the error in the code to generate a 1D array of 10 random floats between 0 and 1.

NumPy
import numpy as np
random_numbers = np.random.rand([1])
print(random_numbers)
Drag options to blanks, or click blank then click option'
A[10]
B10
C(10,)
Drange(10)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a list or tuple instead of an integer.
Using range(10) which is not accepted.
4fill in blank
hard

Fill both blanks to create a dictionary where keys are numbers 1 to 5 and values are their random floats between 0 and 1.

NumPy
import numpy as np
random_dict = {i: np.random.[1]() for i in range(1, [2])}
print(random_dict)
Drag options to blanks, or click blank then click option'
Arand
Brandint
C6
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which generates integers.
Using 5 as the stop value, which excludes 5 in range.
5fill in blank
hard

Fill all three blanks to create a 3x3 numpy array of random floats and print its shape and type.

NumPy
import numpy as np
arr = np.random.[1]([2], [3])
print(arr.shape)
print(type(arr))
Drag options to blanks, or click blank then click option'
Arand
B3
Drandint
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which generates integers.
Passing dimensions as a tuple instead of separate arguments.