Numpy Example

This program will take the input of numpy file Grades1.npy and produce an output summarizing the scores within the file and then save the output in separate files.

In [1]:
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 checkNeg(grades):                       #checks for negative values and replaces them with zero
    grades1 = grades                        #returns array with corrected values and student id of record with replaced values
    
    for i in range(len(grades1)):
        
        for j in range(1,30):
            if grades1[i][j] < 0:
                grades1[i][j] = 0
                id0 = grades[i][0]
                
    
    return grades1, id0
   
#------------------------------------------------------------------------------    
def indvScores(grades):                 #returns array with averages and final scores
                                        
    
    arr1 = np.empty((len(grades),8))
    arr2 = np.empty((len(grades),1), dtype = object)
    
    for i in range(1, len(grades)):                    
        sid = grades[i][0]                          #retrieve student id numbers
        arr1[i][0] = sid
                       
                                                    #retrieve and average homework scores
        hw = np.sum(grades[i][1:7])
        hw1 = hw/6
        hpc = ((hw1 * .01) * 20)
        arr1[i][1] = round(hpc,2)
        
        
                                                    #retrieve and average lab scores
        labs = np.sum(grades[i][7:18])
        labs1 = labs/11
        lpc = ((labs1 * .01) * 30)
        arr1[i][2] = round(lpc,2)
                
                                                #retrieve final project score
        final = grades[i][18]
        fpc = ((final * .01) * 10)
        arr1[i][3] = fpc
        
                                                #retrieve midterm 1 score
        midT1 = grades[i][19]
        mpc = ((midT1 * .01) * 10)
        arr1[i][4] = mpc
        
                                                #retrieve and average quiz scores
        qz = np.sum(grades[i][20:29])
        qz1 = qz/9
        qpc = ((qz1 * .1) * 15)
        arr1[i][5] = round(qpc,2)

                                                #retrieve midterm 2 score
        midT2 = grades[i][29]
        mpc2 = ((midT2 * .01) * 15)
        arr1[i][6] = mpc2
        
                                                #compile into a total score
        total = (hpc + lpc + fpc + mpc + qpc + mpc2)
        arr1[i][7] = round(total,2)
        ltrGrd = letterGrade(total)             #find letter grade
        print("Final score for student", sid, "is",round(total,2),"for letter grade", ltrGrd  )
        print()
        arr2[i][0] = ltrGrd
        
    
    return arr1, arr2
#------------------------------------------------------------------------------
def multiRowAvg(grades, ncol, startcol, title, names):          #returns array with averages and scores for grade groups
    
    
    arr2 = np.empty((len(grades) ,ncol))           #create an empty array to populate with homework scores
    
    
    for i in range(0, len(grades)):
        for j in range(ncol):
            arr2[i][j] = round(grades[i][j + startcol],2)
            
    
    
    arr2 = np.delete(arr2, (0), axis = 0)           #delete row with weights
          
    hwav = arr2.mean(axis = 0)                      #averages for each homework assignment

    for m in range(len(hwav)):
        print(title,names[m],"is",round(hwav[m],2))
        
    if ncol > 1:
        hwav2 = hwav.mean(axis = 0)
        print()
        print("The average", title,"score was", round(hwav2,2))
        arr3 = np.empty(ncol)                      
    
        for m in range(len(hwav)):
            arr3[m] = round(hwav[m],2)
    
        arr2 = np.vstack([arr2,arr3])
    elif ncol == 1:
        arr3 = np.empty(ncol)
        arr3 = hwav
        arr2 =np.vstack([arr2,arr3])
       
    
    return arr2
    
#-----------------------------------------------------------------------------
def main():
    
    print("This program will take the input of numpy file Grades1.npy and produce")
    print("an output summarizing the scores within the file and then save the output")
    print("in separate files. Incorrect file paths will produce errors.")
    print()
    
    grades1 = np.load(input("Please input the path to the file "))
    print()
    
    grades, id0 = checkNeg(grades1)                         #function that checks for negative values
    
    
    
    arr1, arr21 = indvScores(grades)                               #function that returns array with averages and final scores
    
    arr1 = np.delete(arr1,(0),axis=0)
    arr21 = np.delete(arr21,(0),axis=0)
    arr21 = np.hstack([arr1, arr21])
    np.save("letterGrades", arr21)
    np.save("score_avgs", arr1)                             #save averages and final scores in file
    
    names1 = [0,1,2,3,4,5]                                  #print averages and save in separate files for each grade group
    title1 = 'homework'
    print("Average score for each homework")
    print("--------------------------------")
    arr2 = multiRowAvg(grades, 6, 1, title1, names1)
    print()
    print("Homework scores are saved in a file named homework_scores with the averages appended to row number", len(arr2))
    print()
    np.save("homework_scores", arr2)
    
    names2 = [1,2,3,4,5,6,7,8,9,10,11]
    title2 = 'lab'
    print("Average score for each lab")
    print("---------------------------")
    arr4 = multiRowAvg(grades, 11, 7, title2, names2)
    print()
    print("Lab scores are saved in a file named lab_scores with the averages appended to row number", len(arr4))
    print()
    np.save("lab_scores", arr4)
    
    names3 = [1,2,3,5,6,7,8,9,10]
    title3 = 'quiz'
    print("Average score for each quiz")
    print("---------------------------")
    arr6 = multiRowAvg(grades, 9, 20, title3, names3)
    print()
    print("Quiz scores are saved in a file named quiz_scores with the averages appended to row number", len(arr6))
    print()
    np.save("quiz_scores", arr6)
    
    names4 = ['']
    title4 = 'Final Project'
    print("Average score for Final Project")
    print("-------------------------------")
    arr8 = multiRowAvg(grades, 1, 18, title4, names4)
    print()
    print("Final project scores are saved in a file named final_project_scores with the average appended to row number", len(arr8))
    print()
    np.save("final_project_scores", arr8)
    
    names5 = [1]
    title5 = 'Midterm'
    print("Average score for Midterm 1")
    print("---------------------------")
    arr10 = multiRowAvg(grades, 1, 19, title5, names5)
    print()
    print("Midterm 1 scores are saved in a file named midterm1_scores with the average appended to row number", len(arr10))
    print()
    np.save("midterm1_scores", arr10)
    
    names6 = [2]
    title6 = 'Midterm'
    print("Average score for Midterm 2")
    print("---------------------------")
    arr12 = multiRowAvg(grades, 1, 29, title6, names6)
    print()
    print("Midterm 2 scores are saved in a file named midterm2_scores with the average appended to row number", len(arr12))
    print()
    np.save("midterm2_scores", arr12)
    
    
    totals = 0
    for j in range(1,len(arr1)):                        #get average letter grade and print averages and final scores file
        totals += arr1[j][7]
    totalAvg = totals/(len(arr1))
    ltrGavg = letterGrade(totalAvg)
    print()
    print("The average total score for the whole class was a",round(totalAvg, 2), "with the average letter grade being a", ltrGavg)
    print("Student with id #", id0, "had a negative score entered in the original file")
    print("Since negative scores are not possible, any records with a negative score will be changed to zero")
    print()
    print()
    print("The results of this program will be saved in a file named letterGrades")
    
main()
This program will take the input of numpy file Grades1.npy and produce
an output summarizing the scores within the file and then save the output
in separate files. Incorrect file paths will produce errors.

Please input the path to the file Grades1.npy

Final score for student 49647 is 82.35 for letter grade B-

Final score for student 49865 is 87.07 for letter grade B+

Final score for student 88260 is 80.41 for letter grade B-

Final score for student 73216 is 84.05 for letter grade B

Final score for student 62539 is 79.13 for letter grade C+

Final score for student 38180 is 70.42 for letter grade C-

Final score for student 75682 is 87.41 for letter grade B+

Final score for student 53903 is 77.28 for letter grade C+

Final score for student 19860 is 83.46 for letter grade B

Final score for student 40080 is 84.01 for letter grade B

Final score for student 85987 is 65.4 for letter grade D

Final score for student 99792 is 73.61 for letter grade C

Final score for student 92695 is 81.1 for letter grade B-

Final score for student 99656 is 82.16 for letter grade B-

Final score for student 69228 is 91.18 for letter grade A-

Final score for student 37728 is 86.21 for letter grade B+

Final score for student 45564 is 86.99 for letter grade B+

Final score for student 93279 is 82.65 for letter grade B-

Final score for student 60921 is 73.92 for letter grade C

Final score for student 68176 is 74.73 for letter grade C

Final score for student 71930 is 76.31 for letter grade C+

Final score for student 98110 is 91.9 for letter grade A-

Final score for student 58332 is 87.69 for letter grade B+

Final score for student 38015 is 86.82 for letter grade B+

Final score for student 20430 is 90.06 for letter grade A-

Final score for student 31286 is 78.25 for letter grade C+

Final score for student 80032 is 80.35 for letter grade B-

Final score for student 15034 is 84.89 for letter grade B

Final score for student 78329 is 77.1 for letter grade C+

Final score for student 53715 is 86.3 for letter grade B+

Final score for student 72268 is 49.03 for letter grade F

Final score for student 98608 is 73.55 for letter grade C

Final score for student 12240 is 87.94 for letter grade B+

Final score for student 38235 is 85.95 for letter grade B

Final score for student 36756 is 75.14 for letter grade C

Final score for student 98958 is 94.87 for letter grade A

Final score for student 73402 is 83.03 for letter grade B

Final score for student 92712 is 95.29 for letter grade A

Final score for student 60199 is 62.37 for letter grade D

Final score for student 37342 is 88.12 for letter grade B+

Final score for student 73492 is 84.67 for letter grade B

Final score for student 29288 is 87.53 for letter grade B+

Final score for student 34759 is 88.24 for letter grade B+

Final score for student 45920 is 83.4 for letter grade B

Average score for each homework
--------------------------------
homework 0 is 80.0
homework 1 is 88.18
homework 2 is 96.39
homework 3 is 89.91
homework 4 is 90.98
homework 5 is 87.73

The average homework score was 88.86

Homework scores are saved in a file named homework_scores with the averages appended to row number 45

Average score for each lab
---------------------------
lab 1 is 98.66
lab 2 is 95.11
lab 3 is 97.57
lab 4 is 100.0
lab 5 is 94.09
lab 6 is 96.0
lab 7 is 89.55
lab 8 is 93.86
lab 9 is 83.45
lab 10 is 91.75
lab 11 is 93.86

The average lab score was 93.99

Lab scores are saved in a file named lab_scores with the averages appended to row number 45

Average score for each quiz
---------------------------
quiz 1 is 10.0
quiz 2 is 5.18
quiz 3 is 5.07
quiz 5 is 6.98
quiz 6 is 7.59
quiz 7 is 3.36
quiz 8 is 5.45
quiz 9 is 5.25
quiz 10 is 5.64

The average quiz score was 6.06

Quiz scores are saved in a file named quiz_scores with the averages appended to row number 45

Average score for Final Project
-------------------------------
Final Project  is 77.18

Final project scores are saved in a file named final_project_scores with the average appended to row number 45

Average score for Midterm 1
---------------------------
Midterm 1 is 69.57

Midterm 1 scores are saved in a file named midterm1_scores with the average appended to row number 45

Average score for Midterm 2
---------------------------
Midterm 2 is 79.41

Midterm 2 scores are saved in a file named midterm2_scores with the average appended to row number 45


The average total score for the whole class was a 79.77 with the average letter grade being a C+
Student with id # 60921 had a negative score entered in the original file
Since negative scores are not possible, any records with a negative score will be changed to zero


The results of this program will be saved in a file named letterGrades