You are on page 1of 8
‘a1na020 “Test -Jupyler Notebook Test3 Note: Do not rename the notebook Honour Code: By taking the test, you undertake to abide by the following in letter and spirit: | understand that this is an open book test and | am allowed to refer to class notes and documentation. | agree to not receiving any help from others and not giving help to others during the test. Read the instructions carefull 1) Enter your code where indicated: i.e. enter just below the comment line # YOUR CODE HERE. You will not be able to enter in other places. 2) Do NOT delete or modify any line of code. For e.g. the line raise NotimplementedError(). Doing so may result in your fle not being evaluated. 3) If your function does not do exactly what is asked, it will be marked as incorrect. Note whether the question asks the function to print or return. 4) In the rare case that you don't have the module ‘nose’ installed, uncomment and run the cell below: pip install nose In [3]: pip install nose Requirement already satisfied: nose in kages (1.3.7) Note: you may need to restart the kernel to use updated packages. : \users\nitro\anaconda3\1ib\site-pac In [4]: import numpy as np Question 1 Write a function that takes 3 arguments ())a2-D numpy array, myArray, of size m rows xn columns, (ji) num1, an integer, (3) num2, an integer, and does the following: Part(1) return a new array myArray_new by replacing the last-but-one column of myArray with an array of elements comprising the sum of the least value of each row of myArray and numt localhost 8888/notebooks/Test3.pynb 18 ‘a1na020 “Test -Jupyler Notebook Part(2) return anew array myArray_new2 by replacing the last row of myArray_new with an array of elements comprising the sum of the highest value of each column of myArray_new and num2 Part(3) return a 2-D array myArray_new3 of the same shape as myArray i.e. mx n by sorting ALL the elements of myArray_new2 from lowest to highest. The code is broken into parts for your ease. Note: Each part must cumulatively do what is required by the previous parts. ie. for e.g., the function you write in Part 3 will have all the lines of code in Part1, and Part2 also Cumulative marks: Part1: 3, Part2: 6, Part3: 10 Thus, if you get parts 1 and 2 correct, but not part 3, you get 6 marks out of 10 In [3]: def arrayChangerParti (myArray, numl, nun2): do part 1 of the question # YOUR CODE HERE arr=np.array(myArray) mr=arr.min(axis=1) for i in range(Ir): aw=mr[i]+numd arr[i,-2 myArray_new=arr return(myarray_new) raise NotInplementedError() In [ J: In [ J: In [ J: localhost 8888/notebooks/Test3.pynb 28 ‘artra020 “Tests - upyter Notebook In [6]: def arrayChangerPart2(myArray num ,num2) : do part 1 and 2 of the question # YOUR CODE HERE arr=np.array(myArray) ab-arr.shape Ir=ab[2] mr=arr.min(axi: for i in range(Ir): aw=nr[i]+numt arr[i,-2]=aw myArray_new=arr a= [] myArray_new2 = np.array(myArray_new) row = np.shape(myArray_new)[@] col = np.shape(myArray_new)[1] for i in range(®, col)? x= np.max(myArray_new[:,i]) myArray_new2[row-1][i] = x+num2 ) return(myArray_new2) raise NotImplementedérror() In [ J: In [ J: In [ J: localhost 8888/notebooks/Test3.pynb ‘artra020 “Tests - upyter Notebook In [7]: def arrayChangerPart3(myArray num ,num2) do all 3 parts of the question # YOUR CODE HERE arr=np.array(myArray) ab=arr.shape Ir=ab[@] mr=arr.min(axi: for i in range(Ir): awemr[i]+numd arr[i,-2]=aw myArray_new=arr af] myArray_new2 = np.array(myArray_new) row = np.shape(myArray_new)[@] col = np.shape(myarray_new)[1] for o in range(@, col): x= np.max(myArray_new[:,0]) myArray_new2[row-1][o] = x+num2 ab= [] row = np.shape(myArray_new2)[0] col = np.shape(myArray_new2)[1] for io in range(@,row): for jo in range(@, col): ab. append(myArray_new2[io][Jo]) = np.sort (ab) np.array(ab) np.reshape(ab, (row, col)) return ab raise NotInplementedError() ) ab ab ab In [ J: In [ J: In [ J: In[ ]: Question 2 In the cell below, write a function that takes two aruguments: (i) a 2D numpy array, myArray, of size m rows xn columns, containing numbers in the interval [0,1] (ji) a number num in the interval [0, 1], localhost 8888/notebooks/Test3.pynb ‘artra020 “Tests - upyter Notebook and does the following: Part(1) return the column means of myArray Part(2) split into 2 steps for ease: 2a in each column of myArray, find the index (or row number) of the element that i s closest to numl. i.e. The function in Part2a must return a nunpy array with ar ow number fron each column -it has number of elements equal to number of columns. 2b return an array called myArray_new having same elements as myArray, with one el ement in each column of myArray (as identified in 2a by the row number) replaced w ith the respective column's mean found in Part (1) Part(3) return myArray_new2 by subtracting myArray_new from myArray Part(4) return the transpose of myArray_new2 The code is broken into parts for your ease. Note: Each part must cumulatively do what is required by the previous parts. Le. for e.g., the function you write in Part 4 will have all the lines of code in Part1, Part2, and Patt3 also. Cumulative marks: Partt: 1, Part2a: 4, Part2b: 7, Part3: 8, Part4: 10 Thus, if you get parts 1, 2a and 2b correct, but not parts 3 and 4, you get 7 marks out of 10 In [ J: def rayModifierParti (myArray,num1): do part 1 of the question # YOUR CODE HERE ad = np.mean(myArray, axis = 0) return ad raise NotInplementedError() def arrayModifierPart2a(myArray, num): do till part 2a of the question i.e. part 1 and 2a # YOUR CODE HERE ad = np.mean(myArray, axis = 0) ac = myArray - numt for i in range(ac.shape[]): for j in range(ac.shape[1]): ac[i][3] = abs(ac[i][j]) ae = np.argmin(ac, axis=0) return ae raise NotInplementederror() localhost 8888/notebooks/Test3.pynb ‘a1na020 “Test -Jupyler Notebook In[ J: In]: In [ ]: def arrayModifierPart2b(myArray, num): do till part 2b of the question i.e. part 1, 2a, 2b 4 YOUR CODE HERE ad = np.mean(myArray, axis = 0) ac = myArray - numt for i in range(ac.shape[]): for j in range(ac.shape[1]): ac[i][3] = abs(ac[i][j]) ae = np.argmin(ac, axis=0) myArray_new = np.zeros((myArray.shape[@],myArray.shape[1])) for i in range(myArray.shape[0]): for j in range(myArray.shape[1]): imyarray_new[iJ[j] = myArray(i][i] for i in range(myArray.shape[1]): myArray_new(ae[i]][i] = ad[i] return myarray_new raise NotInplementedError() In []: In]: In {]: localhost 8888/notebooks/Test3.pynb ‘a1na020 “Test -Jupyler Notebook In[ J: def arrayModi fierPart3(myarray, num): do till part 3 of the question i.e. part 1, 2a, 2b, 3 # YOUR CODE HERE ad = np.mean(myArray, axis = @) ac = myArray - num for i in range(ac.shape[]): for j in range(ac.shape[1]): ac{i][j] = abs(ac[i][j]) ae = np.argmin(ac, axis=0) myArray_new = np.zeros((myArray.shape[0],myArray.shape[1])) for i in range(myArray.shape[@]): for j in range(myArray.shape[1]): myarray_new[iJ[j] = myArray[i][3] for i in range(myArray.shape[1]): myArray new[ae[i]]{i] = ad{i] return myArray - arrayModifierPart2b(myArray, num) raise NotInplementedError() In[ ]: In [9]: def arrayModifierParta(myArray, num) : do all parts of the question , 1 to 4 # YOUR CODE HERE ad = np.mean(nyArray, axis = 2) fac = myArray - num for i in range(ac.shape[@]): for j in range(ac.shape(1]): acli}[i] = abs(acliJ(j]) ae = np.argmin(ac, axis=@) myArray_new = np. zeros ((myArray.shape[] ,myArray.shape[1])) for i in range(nyArray.shape[@]): for j in range(myArray. shape[1] inyArray_new(i][J] = myArray[i][J] for i in range(nyArray.shape[1]): myArray_new[ae[iJ]][i] = ad[i] return np.transpose(arrayModifierPart3 (myArray, num) ) raise NotImplementedError() In[]: In []: localhost 8888/notebooks/Test3.pynb 718 ‘a1na020 In[ J: localhost 8888/notebooks/Test3.pynb “Test -Jupyler Notebook a8

You might also like