I have this assignment that I need to solve:
Interface
def roundGrade(grades):
# Insert your code here
return gradesRounded
Input arguments
grades
: A vector (each element is a number between −3 and 12).
Return value
gradesRounded
: A vector (each element is a number on the 7-step-scale).
Description
The function must round off each element in the vector grades and return the nearest grade on the 7-step-scale:
7-step-scale: Grades 12 10 7 4 02 00 −3
For example, if the function gets the vector [8.2, -0.5] as input, it must return the rounded grades [7, 0] which are the closest numbers on the grading scale.
I have tried the following code:
import numpy as np
def roundGrade(grades):
trueGrades = np.array([12, 10, 7, 4, 2, 0, -3])
matrix = np.array([trueGrades, (len(grades)), 1])
index = np.argmin(np.abs(matrix.T - grades), axis=0)
gradesRounded = trueGrades[index]
return gradesRounded
When I run the code line by line, I get the following error:
index = np.argmin(np.abs(matrix.T - grades), axis=0)
ValueError: operands could not be broadcast together with shapes (3,) (100,)
How can I solve this problem?
matrix
and ofgrades
... – Itamar Mushkinmatrix
inside function definition is 3 rows, 1 column. and thegrades
passed into function is 100, they should be of the same size to make arifmetic operations, like matrix.T - grades – Stanislav Lipovenko