【问题标题】:Creating a 'Letter Grade' array from an array that contains total score从包含总分的数组创建“字母成绩”数组
【发布时间】:2021-03-21 15:08:18
【问题描述】:

我是 numpy 新手,这行代码有问题。我正在尝试从一组最终分数中制作一组字母等级,但不断遇到以下错误消息:

具有多个元素的数组的真值是不明确的。使用 a.any() 或 a.all()

import numpy as np

def letterGrade(score): 
#returns corresponding letter grade for given numerical score
    grade = 'unknown'
    
    if score >= 93 and score <= 100:
        grade = 'A'
    elif score >= 90 and score <= 92.99:
        grade = 'A-'
    elif score >= 86 and score <= 89.99:
        grade = 'B+'
    elif score >= 83 and score <= 85.99:
        grade = 'B'
    elif score >= 80 and score <= 82.99:
        grade = 'B-'
    elif score >= 76 and score <= 79.99:
        grade = 'C+'
    elif score >= 73 and score <= 75.99:
        grade = 'C'
    elif score >= 70 and score <= 72.99:
        grade = 'C-'
    elif score >= 66 and score <= 69.99:
        grade = 'D+'
    elif score >= 60 and score <= 65.99:
        grade = 'D'
    elif score >= 0 and score <= 59.99:
        grade = 'F'        
    elif score < 0 or score > 100:
        grade = 'can not be determined from the input given'
    return grade    



def main():
    
    score_headings = np.load("C:/Users/Javy/Downloads/score_headings.npy")
    total = score_headings[:,1:].sum(axis=1)
    total = np.array(total)
    
    ltrGrd = letterGrade(total)
   
main()

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    发生错误是因为您将 numpy.ndarray 对象传递给需要原子整数的函数。 Python 不会自动将该函数应用于向量的每个元素(例如 R 会)。要解决这个问题,您只需将该功能单独应用于每个项目。这是一种方法:

    ltrGrd = list(map(lambda x: letterGrade(x), total))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2012-11-16
      • 2013-09-30
      • 2017-03-25
      相关资源
      最近更新 更多