import numpy as npReview: Random and Fancy Indexing
Module 3: NumPy
Session 3.5: Random Number Generation & Fancy Indexing
I want my code below to print the message “Welcome” with a 50% chance. Which condition should I use?
p = np.random.uniform(0, 100)
# Option A
condition = p >= 50
# Option B
condition = p >= np.random.uniform(50)
# Option C
condition = p >= np.random.seed(50)
if condition:
print("Welcome")The code below is designed to assign random dice rolls (between 1 and 6) to each player from the list players. Fix the code so that each player receives a random integer between 1 and 6 (inclusive), as would be expected from rolling a die.
players = ["A", "B", "C"]
results = np.random.uniform(1, 6, size=3)
dict_results = {}
for i in range(len(players)):
dict_results[players[i]] = results[i]
print(dict_results)# Solution
players = ["A", "B", "C"]
results = np.random.randint(1, 7, size=3)
dict_results = {}
for i in range(len(players)):
dict_results[players[i]] = results[i]
print(dict_results)The code below is supposed to replace the first and last elements of the array arr with the value 100. However, it doesn’t work as intended. Explain the error, and fix the code to correctly replace the first and last elements.
arr = np.array([1, 2, 3, 4, 5])
# Attempt to replace the first and last elements
arr[0, -1] = 100 # This raises an error
print(arr)# Solution 1: Fancy indexing
arr = np.array([1, 2, 3, 4, 5])
# Attempt to replace the first and last elements
arr[[0, -1]] = 100
print(arr)# Solution 2: One element at a time
arr = np.array([1, 2, 3, 4, 5])
# Attempt to replace the first and last elements
arr[0] = 100
arr[-1] = 100
print(arr)What will be the output of the following code?
arr = np.array([1, 2, 3, 4, 5])
print(arr[[0, 0, 1]])The code below is designed to extract the third row and the second column from a 3x3 matrix matrix. However, it produces an error. Fix the code to correctly extract the row and column.
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Attempt to extract the third row and second column
third_row = matrix[3, :] # This causes an indexing error
second_column = matrix[:, 2]
print("Third row:", third_row)
print("Second column:", second_column)The code below attempts to extract the diagonal elements of the 4x4 matrix matrix. However, it produces incorrect results. Fix the code so that it correctly extracts the diagonal.
matrix = np.array([[10, 20, 30, 40],
[50, 60, 70, 80],
[90, 100, 110, 120],
[130, 140, 150, 160]])
# Attempt to extract the diagonal elements
diagonal = matrix[:, :] # This selects the whole matrix, not the diagonal
print("Diagonal elements:", diagonal)The code below is supposed to reverse the elements of arr using slicing, but it doesn’t work as expected. Fix the code so that the elements of the array are reversed.
arr = np.array([1, 2, 3, 4, 5])
# Attempt to reverse the array
arr[::-1, :] # This raises an error
print(arr)The code below is designed to extract a 2x2 subarray from the top-left corner of the 3x3 matrix matrix. However, the code is incorrect. Fix the indexing to extract the correct subarray.
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Attempt to extract the top-left 2x2 subarray
subarray = matrix[0:2, 0:3] # This includes an extra column
print(subarray)