Question: You are given a 2-D NumPy array (i.e., matrix). Your task is to examine each element in the array and replace it with a new

You are given a 2-D NumPy array (i.e., matrix). Your task is to examine each element in the array and replace it with a new value according to these rules: Assume T is a given threshold value, which is a floating point number. If an element in the array is bigger than the threshold T, then replace that element with a positive 1. If the array element is smaller than the threshold T, then replace it with a negative 1. It the element is exactly equal to T, then put a 0 (zero) in that position Example: If the threshold value is 3 and M = np.array([[1,2,4,2], [-3,2,1,4), (3, 1, 3, 8]]) Original [[ 1 2 4 2] [-3 2 1 4] [ 3 1 38]] Final [[-1 -11 -1] [-1 -1 -1 1] [ 0 -1 0 1]] np.array([[3,2], [-4,7]]) Example: If the threshold value is 3 and M = Original [[ 3 2] [-4 7]] Final [[10] [-1 1]] Example: If the threshold value is 1 and M = np.array([[3,2), (-4,7]]) Original [[ 3 2] [-4_7]] Final [[ 1 1] [-1 1]] a) Write a complete definition for a function named adjust_matrix that meets these specifications: Accepts two input arguments. One is a Numpy 2-D array, and the other is the threshold value Uses a nested for loop to examine the array elements and update their values as described in the problem introduction Returns the modified array to the caller Works for a 2-D array of any size (i.e., number of rows and columns are not fixed) The function should be silent. This means the function itself does not prompt for data, and it does not print anything. All the function should do is manipulate the array. Put your function code in the box below. It will be tested by the grader, so it should be executable. . b) Suppose in the main section of a Python program there is a numeric 2-D NumPy array named myData that is already defined for you. Write a while loop with the body doing the following: Prompts the user to enter a threshold value. Exits the loop if the entered threshold is larger than the value 999. Calls your adjust_matrix function and passes it the myData array and the user's threshold value. Uses a print() statement to display the returned array. Put your function code in the box below. It will be tested by the grader, so it should be executable
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
