Question: 9. In a 2-d array (matrix), write a function that returns the diagonal elements as an array. Ask your TA to illustrate the diagonal elements

9. In a 2-d array (matrix), write a function that returns the diagonal elements as an array. Ask your TA to illustrate the diagonal elements for a rectangular array before you begin. 10. Populate a 2-d array of shape (10,15) with Os and 1s chosen randomly. To do this, import the module random first, and use randint(0.1) to generate a random bit. Use nditer instead of a loop here. 11. You can reshape arrays using the call np.reshape(a, newshape), where newshape is the tuple corresponding to the new shape desired. Generate an array of 24 increasing numbers starting from O using range(24). Reshape it to get the 3-d matrix a. What happens when the tuple newshape is incompatible with the size of a---ie, here a has size 24, but what if newshape is (5,5)? 12. The function np.where(condition, x, y) evaluates the condition. x,y are matrices of the same size. If condition is TRUE in a given position, it outputs the corresponding element of x in that position, else y. Take the matrix from Problem 10 above, and replace every occurence of O with -1 using np.where(condition, x, y) by choosing the arguments wisely
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
