利用附件中的成績數據進行成績統計,根據輸入的同學的名字,輸出其總分和平均分。根據輸入的課程名字,輸出這門課程的平均分、中位數和標準差。(輸出結果中的數值保留2位小數)
輸入格式輸入一個同學的名字
輸入一門課程的名字
輸出格式該同學在數組中的位置序號、平均成績
該門課程在數組中的位置序號、該課程平均成績、中位數和標準差
題目要求用numpy實現,需要先導入numpy庫,這是一個第三方庫,需要用自行安裝,安裝方法是在命令提示符下輸入:
導入numpy後一般為其設置別名為np:
讀文件可以用Python方法:
def readfile(filename): with open('成績單數字.csv', 'r', encoding='utf-8') as file: score = file.read() score = score.replace('\n', ',') score = score.split(',') return score也可以使用numpy中的loadtxt()讀文件
def readfile(filename): score = np.loadtxt(filename, str, delimiter=',', encoding='utf-8') return score此時score為一個列表,內容如下:
['姓名', '學號', '高數', '英語', 'python', '物理', 'java', 'C語言', '羅明', '1217106', '95', '85', '96', '88', '78', '90', '金川', '1217116', '85', '86', '90', '70', '88', '85', '戈揚', '1217117', '80', '90', '75', '85', '98', '95', '羅旋', '1217119', '78', '92', '85', '72', '95', '75', '蔣維', '1217127', '99', '88', '65', '80', '85', '75']
將這個列錶轉為numpy的數組,並按文件中數據的格式轉為6行8列:
def arrayshape(score): scoreArray = np.array(score) scoreArray = scoreArray.reshape(6, 8) return scoreArrayscoreArray 為6行8列的數組,此時成績的數據類型為字符串:
[['姓名' '學號' '高數' '英語' 'python' '物理' 'java' 'C語言']
['羅明' '1217106' '95' '85' '96' '88' '78' '90']
['金川' '1217116' '85' '86' '90' '70' '88' '85']
['戈揚' '1217117' '80' '90' '75' '85' '98' '95']
['羅旋' '1217119' '78' '92' '85' '72' '95' '75']
['蔣維' '1217127' '99' '88' '65' '80' '85' '75']]
根據輸入的學生姓名,返回學生位置序號,可以使用np.argwhere()函數:
indexStudent = np.argwhere(scoreArray == studentName)計算學生平均成績,需要根據學生所在位置序號中的行序號,對該行中的成績數據進行統計,計算平均成績。
def studentInfo(studentName): scoreNum = scoreArray[1:, 2:].astype(int) indexStudent = np.argwhere(scoreArray == studentName) studentRow = indexStudent[0,0] meanStudent = np.mean(scoreNum[studentRow - 1]) return indexStudent,round(meanStudent,2)scoreNum 是僅包含成績的整數數組,因有標題行存在,統計 時,行號要減1:
[[95 85 96 88 78 90]
[85 86 90 70 88 85]
[80 90 75 85 98 95]
[78 92 85 72 95 75]
[99 88 65 80 85 75]]
課程平均成績的統計與前述方法類似,因為姓名和學號存在,列序號要減2:
def courseInfo(courseName): scoreNum = scoreArray[1:, 2:].astype(int) indexcourse = np.argwhere(scoreArray == courseName) courseColumn = indexcourse[0, 1] meancourse = np.mean(scoreNum[0:, courseColumn - 2]) mediancourse = np.median(scoreNum[0:, courseColumn - 2]) stdcourse = np.std(scoreNum[0:, courseColumn - 2]) return indexcourse,round(meancourse,2),round(mediancourse,2),round(stdcourse,2)完整參考程序如下:
import numpy as np
def readfile(filename): score = np.loadtxt(filename, str, delimiter=',', encoding='utf-8') return score
def arrayshape(score): scoreArray = np.array(s).reshape(6, 8) return scoreArray
def studentInfo(studentName): scoreNum = scoreArray[1:, 2:].astype(int) indexStudent = np.argwhere(scoreArray == studentName) studentRow = indexStudent[0,0] meanStudent = np.mean(scoreNum[studentRow - 1]) return indexStudent,round(meanStudent,2)
def courseInfo(courseName): scoreNum = scoreArray[1:, 2:].astype(int) indexcourse = np.argwhere(scoreArray == courseName) courseColumn = indexcourse[0, 1] meancourse = np.mean(scoreNum[0:, courseColumn - 2]) mediancourse = np.median(scoreNum[0:, courseColumn - 2]) stdcourse = np.std(scoreNum[0:, courseColumn - 2]) return indexcourse,round(meancourse,2),round(mediancourse,2),round(stdcourse,2)
if __name__ == '__main__': filename = '成績單數字.csv' score = readfile(filename) scoreArray = arrayshape(score) studentName = input() student = studentInfo(studentName) print(f'{studentName}同學的位置序號為{student[0]}') print(f'{studentName}同學的平均成績為{student[1]}') courseName = input() course = courseInfo(courseName) print(f'{courseName}課程位置序號{course[0]}') print(f'{courseName}平均成績為{course[1]}分') print(f'{courseName}成績中位數為{course[2]}分') print(f'{courseName}成績標準差為{course[3]}')